Format specifiers are used for input-output (I/O) operations. With the help of a format specifier, the compiler can understand what type of data is in I/O operation.
There are some elements that affect the format specifier. They are as follows −
A minus symbol (-): Left alignment.
The number after % specifies the minimum field width. If the string is less than the width, it will be filled with spaces.
Period (.) − Separate field width and precision.
Format specifiers
Here is the list of some format specifiers −
| Specifier | Used for |
|---|---|
| %c | a single character |
| %s | a string |
| %hi | short (signed) |
| %hu | short (unsigned) |
| %Lf | long double |
| %n | prints nothing |
| %d | a decimal integer (assumes base 10) |
| %i | a decimal integer (detects the base automatically) |
| %o | an octal (base 8) integer |
| %x | a hexadecimal (base 16) integer |
| %p | an address (or pointer) |
| %f | a floating point number for floats |
| %u | int unsigned decimal |
| %e | a floating point number in scientific notation |
| %E | a floating point number in scientific notation |
| %% | the % symbol |
Example
Given below is the C program for %o octal integer format specifier −
#include <stdio.h>
int main() {
int num = 31;
printf("%o\n", num);
return 0;
}Output
When the above program is executed, it produces the following result −
37