Format specifiers in C

Format specifiers define the type of data that will be printed on standard output. Whether you’re printing formatted output with printf() or accepting input with scanf(), format specifiers are required.

C format specifiers cover the various data formats of variables read with the scanf() function to read data and printed with the printf() function. There are several data formats for data types such as char, string, int, float, double, long, short, long int, short int, unsigned, octal, hexadecimal, and so on in the list of format specifiers in C. The scanf() and printf() functions in C are used to format data using format specifiers.

There are many format specifiers in C. Multiple format specifiers can be used to print a variable with a data type. An integer variable, for example, can be printed as a decimal number, an octagonal number, or a hexadecimal number.



List of Format Specifiers in C

The following example illustrates how to print a variable with different format specifiers.

#include <stdio.h>

int main(int argc, char** argv) {
	int a = 15;
	float b = 12345.12345;
	printf("int value   : %d\n", a); 
	printf("oct value   : %o\n", a); 
	printf("hex value   : %x\n", a); 
	printf("hex value   : %X\n\n", a); 
	
	printf("float value : %f\n", b); 
	printf("float value : %e\n", b); 
	printf("float value : %E\n", b); 
	printf("float value : %G\n", b); 
	
	return 0;
}

Output

int value   : 15
oct value   : 17
hex value   : f
hex value   : F

float value : 12345.123047
float value : 1.234512e+004
float value : 1.234512E+004
float value : 12345.1


Table – Format Specifiers

The table below lists all format specifiers that are provided in C

Sr. No.Data TypeFormat
1char%c
2string%s
3int%d
4float%f
5 octal  int%o
6hexadecimal int lowercase%x
7 hexadecimal int uppercase %X
8unsigned int%u
9short int%hd
10unsigned short int%hu
11long int%ld
12unsigned long int%lu
13long long int%lld
14unsigned long long int%llu
15double%lf
16long double%LF
17 float scientific notation lowercase%e
18 float scientific notation uppercase %E
19 float or double scientific notation lowercase %g
20 float or double scientific notation uppercase %G
21signed int%i
22signed short int%hi
23signed long int%li
24Address of the pointer void*%p
25Print nothing %n
26Print %%%



Leave a Reply

Your email address will not be published. Required fields are marked *