











Format Specifiers in C
What are Format Specifiers in C ?
Format Specifiers in C help the compiler in understanding the nature of the data, that is being entered by the user through scanf, or being printed by the programmer using printf. Format Specifiers also helps in printing the correct format of the data, i.e; if we want to print decimal, than we’ll use a different specifier, and if we want to print a integer then a different specifier than the previous one


List of All the Format Specifiers in C
Format Specifier | Use for | Supported datatypes |
---|---|---|
%c | Reads a single character. | char, unsigned char |
%d | Reads a decimal integer. | short, unsigned short, int , long |
%i | Reads an integer in either decimal, octal, or hexadecimal format. | short, unsigned short, int , long |
%e | Reads a floating -point number. | float, double |
%f | Reads a floating -point number. | float |
%g | Reads a floating -point number. | float, double |
%o | Reads an octal number. | short, unsigned short, int, unsigned int, long |
%s | Reads a string. | char* |
%x | Reads a hexadecimal number. | short, unsigned short, int, unsigned int, long |
%p | Reads a pointer. | void* |
%n | Receives an integer value equal to the number of characters read so far. | |
%u | Reads an unsigned decimal integer. | unsigned int, unsigned long |
%[ ] | Scans for a set of characters. | |
%% | Reads a percent sign. | |
%hi | Reads a signed integer(short). | short |
%hu | Reads a un-signed integer(short). | unsigned short |
Use of a few Format Specifiers
Use of %f
#include <stdio.h> int main() { float a = 147.259; printf("%f\n", a); printf("%e\n", a); return 0; }
Output 147.259003 1.472590e+02
Use of %e
#include <stdio.h> int main() { float a = 147.259; printf("%f\n", a); printf("%e\n", a); return 0; }
Output 147.259003 1.472590e+02
Use of %x
#include <stdio.h> int main() { int a = 147.259; printf("%x\n", a); return 0; }
Output 93
Use of %0
#include <stdio.h> int main() { float a = 147.259; printf("%0\n", a); return 0; }
Output %0
Login/Signup to comment