Format specifiers are special symbols used with functions such as
printf() and scanf() to specify the type of data
being displayed or read. They usually begin with the % symbol.
A format specifier is a special sequence beginning with % that
tells a formatted I/O function how to interpret an argument.
printf("%d", age);
Here, %d tells printf() to format the corresponding
argument as an integer.
Format specifiers are useful because they tell formatted input and output functions what type of data is being processed.
scanf().
The %d conversion specifier is commonly used with an
int value.
int age = 20;
printf("%d", age);
Output:
20
The %i conversion specifier can also be used with an integer
argument in printf().
int number = 100;
printf("%i", number);
In scanf(), %i has additional behavior: the input
base can be detected from prefixes such as 0 or 0x.
The %u conversion specifier is used for an unsigned integer.
unsigned int count = 50;
printf("%u", count);
The %f conversion specifier is used to format a
double argument in printf(). A float
argument is automatically promoted to double when passed to
printf().
float price = 99.50f;
printf("%f", price);
When using scanf(), %lf is used to read a
double.
double salary;
scanf("%lf", &salary);
The & provides the address where the input value should be
stored.
In scanf(), %f is used to read a
float.
float price;
scanf("%f", &price);
The %c conversion specifier is used for a single character.
char grade = 'A';
printf("%c", grade);
Output:
A
The %s conversion specifier is used to print a null-terminated
string.
char name[] = "Rahul";
printf("%s", name);
Output:
Rahul
The %x conversion specifier displays an unsigned integer in
lowercase hexadecimal notation.
int number = 255;
printf("%x", number);
Output:
ff
The %X conversion specifier displays an unsigned integer in
uppercase hexadecimal notation.
int number = 255;
printf("%X", number);
Output:
FF
The %o conversion specifier displays an unsigned integer in
octal notation.
int number = 64;
printf("%o", number);
Output:
100
The %e conversion specifier displays a floating-point value
using scientific notation with a lowercase e.
double number = 12345.67;
printf("%e", number);
The %E conversion specifier is similar to %e,
but uses an uppercase E in the exponent.
double number = 12345.67;
printf("%E", number);
The %g conversion specifier chooses between normal floating-point
notation and scientific notation, using the more appropriate representation
for the value and precision.
double number = 123.456;
printf("%g", number);
The sequence %% is used in printf() to display a
percent sign.
printf("Success: 90%%");
Output:
Success: 90%
A field width can be used to specify a minimum number of character positions for formatted output.
printf("%5d", 25);
The value is printed in a field with a minimum width of 5 characters.
Precision can control the number of digits displayed after the decimal point
when using printf() with %f.
double price = 99.5678;
printf("%.2f", price);
Output:
99.57
The - flag can left-align a value within the specified field
width.
printf("%-10d", 25);
This places the value toward the left side of a field of at least 10 characters.
The 0 flag can be used to pad certain numeric output with zeros.
printf("%05d", 25);
Output:
00025
The l length modifier can be used with d to format
a long int.
long int population = 1000000L;
printf("%ld", population);
The ll length modifier can be used with d for a
long long int.
long long int number = 9000000000LL;
printf("%lld", number);
The %lu conversion specification can be used to format an
unsigned long value.
unsigned long population = 1000000UL;
printf("%lu", population);
The %zu conversion specification is used with a
size_t value, such as the result of sizeof.
printf("%zu", sizeof(int));
This is the standard way to print a size_t value.
The scanf() function also uses format specifiers to determine
what kind of input should be read.
int age;
scanf("%d", &age);
The address operator & is commonly used with scalar
variables so that scanf() can store the input value.
char grade;
scanf(" %c", &grade);
The space before %c tells scanf() to skip leading
whitespace characters such as a leftover newline.
For a character array, %s can read a word until whitespace is
encountered.
char name[20];
scanf("%19s", name);
The width limit helps prevent input from exceeding the array's capacity.
| Specifier | Common Use |
|---|---|
%d |
int |
%u |
unsigned int |
%f |
Floating-point output / float input |
%lf |
double input with scanf() |
%c |
Character |
%s |
String |
%x |
Hexadecimal |
%o |
Octal |
%zu |
size_t |
#include <stdio.h>
int main()
{
int age = 20;
unsigned int students = 50;
float percentage = 85.5f;
double salary = 25000.75;
char grade = 'A';
char name[] = "Rahul";
printf("Name = %s\n", name);
printf("Age = %d\n", age);
printf("Students = %u\n", students);
printf("Percentage = %.1f\n", percentage);
printf("Salary = %.2f\n", salary);
printf("Grade = %c\n", grade);
printf("Hex = %x\n", age);
return 0;
}
This example demonstrates several commonly used format specifiers with different types of data.
% symbol.%d is commonly used for int values.%u is used for unsigned integers.%f is used for floating-point output.%lf is used to read a double with
scanf().%c is used for characters.%s is used for strings.%x and %X are used for hexadecimal output.%o is used for octal output.%zu is used for size_t values.Question: Which format specifier is commonly used with printf() to display an integer of type int?