|
|
|
Scanf()
|
|
Now that we have mastered the intricacies of printf you should find scanf very easy. The scanf function works in much the same way as the printf. That is it has the general form:
scanf(control string,variable,variable,...)
In this case the control string specifies how strings of characters, usually typed on the keyboard, should be converted into values and stored in the listed variables. However there are a number of important differences as well as similarities between scanf and printf.
The most obvious is that scanf has to change the values stored in the parts of computers memory that is associated with parameters (variables).
To understand this fully you will have to wait until we have covered functions in more detail. But, just for now, bare with us when we say to do this the scanf function has to have the addresses of the variables rather than just their values. This means that simple variables have to be passed with a preceding >&. (Note for future reference: There is no need to do this for strings stored in arrays because the array name is already a pointer.)
The second difference is that the control string has some extra items to cope with the problems of reading data in. However, all of the conversion specifiers listed in connection with printf can be used with scanf.
The rule is that scanf processes the control string from left to right and each time it reaches a specifier it tries to interpret what has been typed as a value. If you input multiple values then these are assumed to be separated by white space - i.e. spaces, newline or tabs
will read in two integer values into i and j. The integer values can be typed on the same line or on different lines as long as there is at least one white space character between them.
The only exception to this rule is the %c specifier which always reads in the next character typed no matter what it is. You can also use a width modifier in scanf. In this case its effect is to limit the number of characters accepted to the width.
|
|
scanf("%lOd",&i)
|
|
would use at most the first ten digits typed as the new value for i.untile maximum value match data type.
There is one main problem with scanf function which can make it unreliable in certain cases. The reason being is that scanf tends to ignore white spaces, i.e. the space character. If you require your input to contain spaces this can cause a problem. Therefore for string data input the function getstr() may well be more reliable as it records spaces in the input text and treats them as an ordinary characters.
A program to demonstrate input,output task.
|
#include <stdio.h>
main()
{
int a,b,c;
printf("\nThe first number is ");
scanf("%d",&a);
printf("The second number is ");
scanf("%d",&b);
c=a+b;
printf("The answer is %d \n",c);
}
|
|
|
gets(),puts()
|
|
Where
we are not too interested in the format of our data, or perhaps we cannot
predict its format in advance, we can read and write whole lines as
character strings. This approach allows us to read in a line of input, and
then use various string handling functions to analyse it at our leisure.
gets
reads a whole line of input into a string until a newline or EOF is
encountered. It is critical to ensure that the string is large enough to
hold any expected input lines. When all input is finished, NULL as defined
in stdio.h is returned.
puts writes a string to the output, and follows it with a newline
character.Example: Program which uses gets and puts to double space typed
input.
#include <stdio.h>
main()
{
char line[80]; /* Define string sufficiently
large to
store a line of input */
while(gets(line) !=
NULL) /* Read
line */
{ puts(line);
/* Print line */
printf("\n"); /* Print
blank line */
}
}
|
|
|
Back
|
|
Next
|
|
|