Conversion Specification Type | Output |
%c | single character |
%d | signed decimal integer |
%e | floating-point number, e-notation |
%E | floating-point number, E-notation. |
%f | floating-point number, decimal notation. |
%g, %G | use %f or %e, whichever is shorter. |
%i | signed decimal integer |
%o | unsigned octal integer. |
%p | pointer |
%s | character string |
%u | unsigned decimal integer |
%x | unsigned hexadecimal integer, using hex digits a,b,c,d,e,f. |
%X | unsigned hexadecimal integer, using hex digits A,B,C,D,E,F. |
Modifier | Meaning | ||
flag | There are five flags that can be used: | ||
- | the item is printed left-justified | ||
+ | signed values are displayed with a plus if positive and with a minus sign if negative. | ||
space | a blank space will precede each positive signed numerical data item. This flag is overridden by the + flag if both are present. | ||
# | causes data items printed in octal form to
be preseced by a 0 and data items printed in hexadecimal form to be preceded
by a 0x or 0X.
causes a decimal point to be printed in all floating-point numbers even if the data item is a whole number. Also prevents the truncation of trailing zeros in g-type conversion. |
||
%g, %G | 0 | For numeric data item, pad the output field with loading zeroes instead of blank spaces. This flag is overridden by the - flag if both are present or if, for an integer form, a precision is specified. | |
width | The minimum output field width. A wider output
field will be used if the printed data item won't fit into the specified
field width.
A * means to take the next argument as the output field width. |
||
.precision | The precision of the output.
For e, E, and f conversions, it is the number digitis to be printed to the right of the decimal point. For g and G conversions, it is the maximum number of significant digits. For s conversions, it is the maximum numner of characters to be printed. For integer conversions, it is the minimum number of digits to appear and leading zeroues are used if necessary to meet this minimum. A .* means to take the next argument as the precision size. Using the . (period) alone is the same as using .0 |
||
h | Used with an integer conversion to indicate
a short int or unsigned short int value.
eg: %hu, %hx, 6.4hd |
||
l | Used with an integer conversion to indicate
a long int or unsigned long int.
eg. %ld, %8lu |
||
L | Used with floating point conversion to indicate
a long double value.
eg. %Lf, %10.4Le |
#include <stdio.h> main() { int value, count; printf("Enter the value and count :"); scanf("%d%d",&value, &count); }
Conversion Specification Type | Interpret input as |
%c | a single character |
%d. %i | signed decimal integer |
%e, %E, %f | floating-point number |
%o | unsigned octal integer. |
%p | pointer |
%s | character string |
%u | unsigned decimal integer |
%x, %X | unsigned hexadecimal integer, using hex digits a,b,c,d,e,f. |
%[..]s | Character enclosed within the brackets indicate that a
string is to be read. The characters within the brackets indicate the permissible
characters in the string. If any character other than that specified in
the brackets is encountered, then the string will be terminated.
By placing a ^ as the first character inside the brackets, then if any of the characters is found in the input, then the string will be terminated. |
Modifier | Meaning |
* | Suppress assignment.
eg: %*d |
integer-string | The maximum field width, Input stops when the maximum field
width is reached or when the first whitespace character is encountered
whichever comes first.
eg: %10s |
h,l, or L | %hd and %hi indicate the value will be stored
in a short int.
%ho, %hx, and %hu indicate the value will be stored in an unsigned short int. %ld and %li indicate the value will be stored in a long int. %lo, %lx, %lu indicate the value will be stored in unsigned long. %le, %lf, and %lg indicate the value will be stored in a double. Using L instead of l with e, f and g indicates the value will be stored in type long double. In the absence of these modifiers, d, i, e, o, and x indicate type int; e,f, and g indicate type float. |
#include <stdio.h> main() { int items; float prices; printf ("Enter the number of items "); printf("and price below: \n"); scanf("%d %f", &item, &price); printf("Number of items = %d", items); printf(" and the price = RM%.2f\n", price); }Program output:
#include <stdio.h> main() { int n; printf("Please enter three integers:\n"); scanf("%*d %*d %d", &n); printf("The last integer was %d\n",n); }Output: