Format Specifiers in C
The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character
The commonly used format specifiers in printf() function are:
Format Specifier | Type |
---|---|
%c | Character |
%d | Signed integer |
%e or %E | Scientific notation of floats |
%f | Float values |
%g or %G | Similar as %e or %E |
%hi | Signed integer (short) |
%hu | Unsigned integer (short) |
%i | Unsigned integer |
%l or %ld or %li | Long |
%lf | Double |
%Lf | Long double |
%lu | Unsigned int or unsigned long |
%lli or %lld | Long long |
%llu | Unsigned long long |
%o | Octal representation |
%p | Pointer |
%s | String |
%u | Unsigned int |
%x or %X | Hexadecimal representation |
Let's understand the format specifiers in detail through an example.
%u: Unsigned Integer
#include <stdio.h>
int main()
{
int b = 10;
int c = -10;
printf("Value of b is: %u\n", b);
printf("nValue of c is: %u\n", c);
return 0;
}
Output
Value of b is: 10
Value of c is: 4294967286 (representation of -10 in unsigned format)
Value of c is: 4294967286 (representation of -10 in unsigned format)
%o: Octal Format
#include <stdio.h>
int main()
{
int a = 0100; // Octal value
printf("Octal value of a is: %o\n", a);
printf("nInteger value of a is: %d\n", a);
return 0;
}
Output
Octal value of a is: 100
Integer value of a is: 64
Integer value of a is: 64
%x and %X: Hexadecimal Format
#include <stdio.h>
int main()
{
int y = 0xA; // Hexadecimal value
printf("Hexadecimal value of y is: %x\n", y);
printf("nHexadecimal value of y is: %X\n", y);
printf("nInteger value of y is: %d\n", y);
return 0;
}
Output
Hexadecimal value of y is: a
Hexadecimal value of y is: A
Integer value of y is: 10
Hexadecimal value of y is: A
Integer value of y is: 10
%f: Floating Point
#include <stdio.h>
int main()
{
float y;
printf("Enter a floating point value: ");
scanf("%f", &y);
printf("Floating point value of y is: %f\n", y);
return 0;
}
Output
Enter a floating point value: 5.6
Floating point value of y is: 5.600000
%e and %E: Exponential Format
#include <stdio.h>
int main()
{
float y = 3;
printf("Exponential value of y is: %e\n", y);
printf("nExponential value of y is: %E\n", y);
return 0;
}
Output
Exponential value of y is: 3.000000e+00
Exponential value of y is: 3.000000E+00
Exponential value of y is: 3.000000E+00
%g: General Floating Point
#include <stdio.h>
int main()
{
float y = 3.8;
printf("Float value of y is: %g\n", y);
return 0;
}
Output
Float value of y is: 3.8
%p: Pointer (Address in Hexadecimal)
#include <stdio.h>
int main()
{
int y = 5;
printf("Address value of y in hexadecimal form is: %p\n", &y);
return 0;
}
Output
Address value of y in hexadecimal form is: 0x7ffeeafb1a0c (example address)
Minimum Field Width
#include <stdio.h>
int main()
{
int x;
// Prompting user for input
printf("Enter an integer value: ");
scanf("%d", &x);
// Printing with 8 spaces, right-aligned
printf("%8d\n", x);
// Printing with 8 spaces, left-aligned
printf("%-8d\n", x);
return 0;
}
Output
Enter an integer value: 800
800
800
Specifying Precision
#include <stdio.h>
int main()
{
float x = 12.2;
printf("%.2f", x);
return 0;
}
Output
12.20