Formating Input/Output
printf() and scanf() functions:
printf() and scanf() functions are inbuilt library functions in C language which are available in C library by default. These functions are declared and defined in "stdio.h" which is a header file.
printf() function:
printf() function is used to print the "character, string, float, integer, octal and hexadecimal values" onto the output screen.
We use printf() function with the %d format specifier to display the value of an integer variable.
Similary %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
To generate a newline, we use"\n" in C printf() statement.
scanf() function:
scanf() function is used to read character, string, numeric data from the keyboard
Consider the program example given below, where the user enters a character:
Then, the user enters a string and this value is assigned to the variable "str" and then displayed.
Example:
{
char ch;
char str[100];
printf("Enter anyt character \n");
scanf("%c",&ch);
}
Note: All functions in C, are case sensitive.
Format Specifiers:
The "Format Specifier" is the sequence passed as the formatting string argument.
Format Specifier used by scanf and printf
The size and range of these data types may vary among processor types and compilers.
Data type qualifiers modify the behavior of variable type to which they are applied.
Data type qualifiers can be classified into two types:
Size Qualifiers
Sign Qualifiers
Size Qualifiers:
Size Qualifiers alter the size of the basic data types. There are two size qualifiers that can be applied to integers: short and long.
The minimum size of "short int" is 16 bit.
The size of 'int' must be greater than or equal to that of 'short int'. The size of 'long int' must be greater than or equal to a 'short int'. The minimum size of 'long int' is 32 bits.
Signed Qualifiers:
The keywords signed and unsigned are the two sign qualifiers that specify whether a variable can hold both the negative and positive numbers, or only positive numbers. These qualifiers can be applied to the data types int and char only.
Example: unsigned int i;
Data type qualifiers modify the behavior of variable type to which they are applied.
Data type qualifiers can be classified into two types:
Size Qualifiers
Sign Qualifiers
Size Qualifiers:
Size Qualifiers alter the size of the basic data types. There are two size qualifiers that can be applied to integers: short and long.
The minimum size of "short int" is 16 bit.
The size of 'int' must be greater than or equal to that of 'short int'. The size of 'long int' must be greater than or equal to a 'short int'. The minimum size of 'long int' is 32 bits.
Signed Qualifiers:
The keywords signed and unsigned are the two sign qualifiers that specify whether a variable can hold both the negative and positive numbers, or only positive numbers. These qualifiers can be applied to the data types int and char only.
Example: unsigned int i;