CodeSutraHub

Format Specifiers in C Language

Format specifiers can be divided into seven groups.
  1. Integer Format Specifiers
  2. Floating-Point Format Specifiers
  3. Character & String Format Specifiers
  4. Pointer Format Specifier
  5. Special Format Specifiers
  6. Format Specifiers for scanf()
  7. Field Width & Precision

Integer Format Specifiers in C Language

Specifier Description Example
%d Signed decimal integer int a = 10;
%i Signed integer (decimal, octal, hex) int b = 015;
%u Unsigned decimal integer (positive integer) unsigned int a = 20;
%o Octal integer 15 → 17
%x Hexadecimal (lowercase) 255 → ff
%X Hexadecimal (uppercase) 255 → FF
%ld Long signed decimal long d = 999999;
%lld Long long signed decimal long long e = 123456789;
%lu Unsigned long unsigned long f = 12345;
%llu Unsigned long long unsigned long long g = 987654321;

Floating-Point Format Specifiers in C Language

Specifier Description Example
%f Float 10.25
%lf Double double d = 20.123;
%Lf Long double long double ld = 30.456;
%e Scientific notation (lowercase) 3.14e+03
%E Scientific notation (uppercase) 3.14E+03
%g Uses %f or %e based on value length printf("%g", 123456.0);
%G Uses %f or %E based on value length printf("%G", 123456.0);

Character & String Format Specifiers in C Language

Specifier Description Example
%c Single character char ch = 'A';
%s String char str[] = "Hello";
Pointer Format Specifier
Specifier Description Example
%p Address of a variable int a = 10; printf("%p", &a);

Special Format Specifiers in C Language

Specifier Description Example
%n Stores number of printed characters into a variable printf("Hello%n", &x);
%% Prints % symbol printf("50%%");

Format Specifiers for scanf() in C Language

Specifier Description Example
%d Read integer scanf("%d", &a);
%f Read float scanf("%f", &b);
%lf Read double scanf("%lf", &c);
%c Read character scanf("%c", &ch);
%s Read string (up to space) scanf("%s", str);

Field Width & Precision

Format Meaning Example
%5d Print integer in width 5 printf("%5d", 10);
%05d Pad with leading zeros printf("%05d", 10);
%.2f Print 2 digits after decimal printf("%.2f", 10.256);
%8.3f Width 8, precision 3 printf("%8.3f", 10.25678);