In this tutorial, we will learn about the ‘input output operation in C’. We will focus on the most commonly used functions of some standard library scanf() and printf().
Formatted Input: scanf()
Scanf() function obtains data from input source, keyboard.
scanf( format-control-string, other-arguments );
scanf("%lf", num); /* Wrong */
scanf("%lf", &num); /* Correct, store input in num. */
scanf() reads the data provided from standard input and process them to the specified format. scanf() is a good choice whenthe exact format of the input is known,
Formatted Output: printf()
The function printf() is a print function that converts and formats its arguments to a character string, and prints the result to standard output i.e Screen.
printf( format-control-string, other-arguments );
Here, the first arguments describe the format of text. While printing the text is check if it needs any conversion. Conversion is specified by a % character followed by fields. For example:
printf("Ram studies in class %d", 9);
Where the ordinary character “Ram studies in class” are printed as it is and the conversion %d insert the supplied arguments. Hence final output of above example is :
Ram studies in class 9
Integer conversion specifiers
An integer is a whole number that can be displayed in several formats.
|d||Signed decimal Integer|
|i||Signed decimal integer|
|o||An unsigned octal integer|
|u||An unsigned decimal integer|
|x or X||An unsigned hexadecimal integer|
|h, I or II||When placed before conversion specifier indicates short, long or long long integer. These are called length modifiers.|
Floating-point conversion specifier
A floating-point value contains decimal point.
|Conversion Specifier ||Description|
|e or E||Floating-point values in exponential notation|
|f or F||Floating-point values in fixed notation|
|g or G||Floating-point value in either floating-point form or exponential form based on length of the value|
|L||Indicates a long double floating-point value if placed before floating-point conversion specifier|
Conversion Specifier for strings and character
|c||Print individual character|
|s||Print string until terminating null character |
\0 is encountered and requires a pointer to
char as an argument