Format specifiers are used in C to specify the type of data that should be displayed or read during input and output operations. They begin with the % symbol and are commonly used with functions like printf() and scanf().
- Format specifiers tell the compiler how to interpret and process different data types.
- They are used to print or read values such as integers, characters, floats, and strings.
#include <stdio.h>
int main(){
int age = 25;
float salary = 45000.50;
char grade = 'A';
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
}
Output
Age: 25 Salary: 45000.50 Grade: A
In the above example:
%dprints an integer.%.2fprints a floating-point number with two decimal places.%cprints a character.
Why are Format Specifiers Used?
Format specifiers are used to tell the compiler how a value should be interpreted during input and output operations. They help to:
- Print values of different data types correctly.
- Read user input according to the required data type.
- Control the formatting of output, such as width, precision, and alignment.
- Improve the readability of program output.
Commonly Used Format Specifiers in C
1. Character Format Specifier - %c in C
The %c is the format specifier for the char data type in C language. It can be used for both formatted input and formatted output in C language.
#include <stdio.h>
int main(){
char c;
// Using %c for character input
scanf("%c", &c);
// Using %c for character output
printf("The entered character: %c", c);
return 0;
}
Output
The entered character:
Output
R (Enter by user) The entered character: R
2. Integer Format Specifier (signed) - %d in C
We can use the signed integer format specifier %d in the scanf() and printf() functions for signed integer type values.
#include <stdio.h>
int main() {
int x;
// taking integer input
scanf("%d", &x);
// printing integer output
printf("Printed using %%d: %d\n", x);
printf("Printed using %%i: %i", x);
return 0;
}
Output
Printed using %d: 0 Printed using %i: 0
Output
3 (Enter by user) Printed using %d: 3 Printed using %i: 3
3. Unsigned Integer Format Specifier - %u in C
The %u is the format specifier for the unsigned integer data type. If we specify a negative integer value to the %u, it converts the integer to its 2's complement.
#include <stdio.h>
int main(){
unsigned int var;
scanf("%u", &var);
printf("Unsigned Integer: %u\n", var);
// trying to print negative value using %u
printf("Printing -10 using %%u: %u\n", -10);
return 0;
}
Output
Unsigned Integer: 0 Printing -10 using %u: 4294967286
Output
3 (Enter by user) Unsigned Integer: 3 Printing -10 using %u: 4294967286
4. Floating-point format specifier - %f in C
The %f is the floating-point format specifier in C language that can be used inside the formatted input and output of float data type. Apart from %f, we can use %e or %E format specifiers to print the floating-point value in the exponential form.
#include <stdio.h>
int main(){
float a = 12.67;
printf("Using %%f: %f\n", a);
printf("Using %%e: %e\n", a);
printf("Using %%E: %E", a);
return 0;
}
Output
Using %f: 12.670000 Using %e: 1.267000e+01 Using %E: 1.267000E+01
5. Unsigned Octal number for integer - %o in C
We can use the %o format specifier in the C program to print or take input for the unsigned octal integer number.
#include <stdio.h>
int main() {
int a = 67;
printf("%o\n", a);
return 0;
}
Output
103
6. Unsigned Hexadecimal for integer - %x in C
The %x format specifier is used in the formatted string for hexadecimal integers. In this case, the alphabets in the hexadecimal numbers will be in lowercase. For uppercase alphabet digits, we use %X instead.
Example:
#include <stdio.h>
int main() {
int a = 15454;
printf("%x\n", a);
printf("%X", a);
return 0;
}
Output
3c5e 3C5E
7. String Format Specifier - %s in C
The %s in C is used to print strings or take strings as input.
#include <stdio.h>
int main(){
char a[] = "Hi Geeks";
printf("%s", a);
return 0;
}
Output
Hi Geeks
Example: The working of %s with scanf() is a little bit different from its working with printf(). Let's understand this with the help of the following C program.
#include <stdio.h>
int main(){
char str[50];
// taking string as input
scanf("%s", str);
printf("Entered String: %s", str);
return 0;
}
Output
Entered String:
Output
Hi Geeks (Enter by user) Hi
As we can see, the string is only scanned till a whitespace is encountered. We can avoid that by using scansets in C.
8. Address Format Specifier - %p in C
The C language also provides the format specifier to print the address/pointers. We can use %p to print addresses and pointers in C.
#include <stdio.h>
int main(){
int a = 10;
printf("The Memory Address of a: %p", &a);
return 0;
}
Output
The Memory Address of a: 0x7ffe6f71e60c
Input and Output Formatting
C language provides some tools using which we can format the input and output. They are generally inserted between the % sign and the format specifier symbol Some of them are as follows:
- A minus(-) sign tells left alignment.
- A number after % specifies the minimum field width to be printed if the characters are less than the size of the width the remaining space is filled with space and if it is greater then it is printed as it is without truncation.
- A period( . ) symbol separates field width with precision.
Precision tells the minimum number of digits in an integer, the maximum number of characters in a string, and the number of digits after the decimal part in a floating value.
Example of I/O Formatting
#include <stdio.h>
int main()
{
char str[] = "geeksforgeeks";
printf("%20s\n", str);
printf("%-20s\n", str);
printf("%20.5s\n", str);
printf("%-20.5s\n", str);
return 0;
}
Output
geeksforgeeks
geeksforgeeks
geeks
geeks
List of C Format Specifiers
The below table contains the most commonly used C format specifiers:
Format Specifier | Description |
|---|---|
%c | For character type. |
%d | For signed integer type. |
%e or %E | For scientific notation of floats. |
%f | For float type. |
%g or %G | For float type with the current precision. |
%i | signed integer |
%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 |
%n | Prints nothing |
%% | Prints % character |