This section introduces some of the more common input and output functions provided in the C standard library.
The printf() function is used for formatted output and uses a control string which is made up of a series of format specifiers to govern how it prints out the values of the variables or constants required. The more common format specifiers are given below
%c -> character
%f -> floating point
%d -> signed integer
%lf -> double floating point
%i -> signed integer
%e -> exponential notation
%u -> unsigned integer
%s -> string
%ld -> signed long
%x -> unsigned hexadecimal
%lu -> unsigned long
%o -> unsigned octal
%% -> prints a % sign
For Example :-
int i ;
printf( "%d", i ) ;
The printf() function takes a variable number of arguments. In the above example two arguments are required, the format string and the variable i. The value of i is substituted for the format specifier %d which simply specifies how the value is to be displayed, in this case as a signed integer.
Some further examples :-
int i = 10, j = 20 ;
char ch = 'a' ;
double f = 23421.2345 ;
printf( "%d + %d", i, j ) ; /* values are substituted from the variable list in order as required */
printf( "%c", ch ) ;
printf( "%s", "Hello World\n" ) ;
printf( "The value of f is : %lf", f ) ;/*Output as : 23421.2345 */
printf( "f in exponential form : %e", f ) ; /* Output as : 2.34212345e+4
Field Width Specifiers:
Field width specifiers are used in the control string to format the numbers or characters output appropriately .
int i = 15 ;
float f = 13.3576 ;
printf( "%3d", i ) ; /* prints "_15 " where _ indicates a space
printf( "%6.2f", f ) ; /* prints "_13.36" which has a total width
of 6 and displays 2 decimal places */
printf( “%*.*f”, 6,2,f ) ; /* prints "_13.36" as above. Here * is used as replacement character for field widths */
There are also a number of flags that can be used in conjunction with field width specifiers to modify the output format. These are placed directly after the % sign. A - (minus sign) causes the output to be left-justified within the specified field, a + (plus sign) displays a plus sign preceding positive values and a minus preceding negative values, and a 0 (zero) causes a field to be padded using zeros rather than space characters.
This function is similar to the printf function except that it is used for formatted input. The format specifiers have the same meaning as for printf() and the space character or the newline character are normally used as delimiters between different inputs.
For Example :-
int i, d ;
char c ;
float f ;
scanf( "%d", &i ) ;
scanf( "%d %c %f", &d, &c, &f ) ; /* e.g. type "10_x_1.234RET" */
scanf( "%d:%c", &i, &c ) ; /* e.g. type "10:xRET" */
The & character is the address of operator in C, it returns the address in memory of the variable it acts on. (Aside : This is because C functions are nominally call--by--value. Thus in order to change the value of a calling parameter we must tell the function exactly where the variable resides in memory and so allow the function to alter it directly rather than to uselessly alter a copy of it. )
Note that while the space and newline characters are normally used as delimiters between input fields the actual delimiters specified in the format string of the scanf statement must be reproduced at the keyboard faithfully as in the case of the last sample call. If this is not done the program can produce somewhat erratic results!
The scanf function has a return value which represents the number of fields it was able to convert successfully.
For Example :- num = scanf( “%c %d”, &ch, &i );
This scanf call requires two fields, a character and an integer, to be read in so the value placed in num after the call should be 2 if this was successful. However if the input was “a bc” then the first character field will be read correctly as ‘a’ but the integer field will not be converted correctly as the function cannot reconcile “bc” as an integer. Thus the function will return 1 indicating that one field was successfully converted. Thus to be safe the return value of the scanf function should be checked always and some appropriate action taken if the value is incorrect.
getchar() and putchar():
These functions are used to input and output single characters. The getchar() function reads the ASCII value of a character input at the keyboard and displays the character while putchar() displays a character on the standard output device i.e. the screen.
NOTE : The input functions described above, scanf() and getchar() are termed buffered input functions. This means that whatever the user types at the keyboard is first stored in a data buffer and is not actually read into the program until either the buffer fills up and has to be flushed or until the user flushes the buffer by hitting RET whereupon the required data is read into the program. The important thing to remember with buffered input is that no matter how much data is taken into the buffer when it is flushed the program just reads as much data as it needs from the start of the buffer allowing whatever else that may be in the buffer to be discarded.
For Example :-
char ch1, ch2;
printf( "Enter two characters : " ) ;
ch1 = getchar() ;
ch2 = getchar() ;
printf( "\n The characters are %c and %c\n", ch1, ch2 ) ;
In the above code segment if the input is 'abcdefRET' the first two characters are read into the variables all the others being discarded, but control does not return to the program until the RET is hit and the buffer flushed. If the input was “aRET” then a would be placed in ch1 and RET in ch2.
The _flushall function writes the contents of all output buffers to the screen and clears the contents of all input buffers. The next input operation (if there is one) then reads new data from the input device into the buffers.
This function should be used always in conjunction with the buffered input functions to clear out unwanted characters from the buffer after each input call.
getch() and getche():
These functions perform the same operation as getchar() except that they are unbuffered input functions i.e. it is not necessary to type RET to cause the values to be read into the program they are read in immediately the key is pressed. getche() echoes the character hit to the screen while getch() does not.