Input/Output in C programming

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().

file handling in C

Formatted Input: scanf()


Scanf() function obtains data from input source, keyboard.  

Syntax:

scanf( format-control-string, other-arguments );

Example:

double num;
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.

Syntax:

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.


Conversion Specifier                                                                            
Description
dSigned decimal Integer
iSigned decimal integer
oAn unsigned octal integer
uAn unsigned decimal integer
x or XAn unsigned hexadecimal integer
h, I or IIWhen 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 EFloating-point values in exponential notation
f or FFloating-point values in fixed notation
g or GFloating-point value in either floating-point form or exponential form based on length of the value
LIndicates a long double floating-point value if placed before floating-point conversion specifier

Conversion Specifier for strings and character


Conversion
Specifier                                        
Description
cPrint individual character
sPrint string until terminating null character \0 is encountered and requires a pointer to char as an argument