Something more about printf() and Scanf()
Consider the following two printf statements
printf(“\t %d\n”, num);
printf(“%5.2f”, fract);
in the first printf statement \t requests for the tab displacement on the screen the argument %d tells the compiler that the value of num should be printed as decimal integer. \n causes the new output to start from new line.
In second printf statement %5.2f tells the compiler that the output must be in floating point, with five places in all and two places to the right of the decimal point. More about the backslash character has been shown in the following table:
Constant |
Meaning |
‘\a’
‘\b’
‘\f’
‘\n’
‘\r’
‘\t’
‘\v’
‘\’’
‘\”’
‘\?’
‘\\’
‘\0’ |
Audible alert (bell)
Backspace
Form feed
New line
Carriage return
Horizontal tab
Vertical tab
Single quote
Double quote
Question mark
Backslash
Null |
Let us consider the following scanf statement
scanf(“%d”, &num);
The data from the keyboard is received by scanf function. In the above format, the & (ampersand) symbol before each variable name is an operator that specifies the address of variable name.
By doing this, the execution stops and waits for the value of the variable num to be typed. When the integer value is entered and return key is pressed, the computer proceeds to the next statement. The scanf and printf format codes are listed in the following table:
Code |
Reads… |
%c
%d
%e
%f
%g
%h
%i
%o
%s
%u
%x |
Single character
Decimal integer
Floating point value
Floating point value
Floating point value
Short integer
Decimal, hexadecimal or octal integer
Octal integer
String
Unsigned decimal integer
Hexadecimal integer |
|