Input and Output Function II
Formatting Your OutputEach specifier can be preceded by a modifier which determines how the value will be printed. The most general modifier is of the form:
flag width.precisionThe flag can be any of:
| flag | meaning |
|---|---|
| - | left justify |
| + | always display sign |
| space | display space if there is no sign |
| 0 | pad with leading zeros |
| # | use alternate form of specifier |
The width specifies the number of characters used in total to display the value and precision indicates the number of characters used after the decimal point.
The specifier %-1Od will display an int left justified in a ten character space. The specifier %+5d will display an int using the next five character locations and will add a + or - sign to the value.
If you print a string using the %s specifier then all of the characters stored in the array up to the first null will be printed. If you use a width specifier then the string will be right justified within the space. If you include a precision specifier then only that number of characters will be printed.
For example:
printf("%s,Hello")will print Hello,
printf("%25s ,Hello")
will print 25 characters with Hello right justified and
printf("%25.3s,Hello")
will print Hello right justified in a group of 25 spaces.
Also notice that it is fine to pass a constant value to printf as in printf("%s,Hello").
scanf
The scanf function works in much the same way as the printf. That is it has the general form: scanf(control string,variable,...)
The most obvious is that scanf has to change the values stored in the parts of computers memory that is associated with parameters (variables).
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. This means you can type:
3 4 5or
3
4
5
and it doesn't matter how many spaces are included between items.
For example:
scanf("%d %d",&i,&j);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.
For example:
scanf("%lOd",&i)would use at most the first ten digits typed as the new value for i.
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.
Example program:
#includemain()
{
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);
}
The first instruction declares three integer variables: a, b and c.
The first two printf statements simply display message on the screen asking the user for the values.
The scanf functions then read in the values from the keyboard into a and b.
These are added together and the result in c is displayed on the screen with a suitable message. Notice the way that you can include a message in the printf statement along with the value.