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
Explanation: %d is used to print the integer age, %.2f prints the floating-point value salary with two decimal places, and %c prints the character grade.
Uses of Format Specifiers
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
The following are some of the commonly used format specifiers in C for handling different data types:
1. Character Format Specifier - %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;
}
Input
ROutput
The entered character: R
Explanation: %c reads a single character using scanf() and prints it using printf().
2. Signed Integer Format Specifier - %d and %i
The %d and %i format specifiers are used with signed integer values.
#include <stdio.h>
int main() {
int x;
scanf("%d", &x);
printf("Using %%d: %d\n", x);
printf("Using %%i: %i", x);
return 0;
}
Input
3Output
Using %d: 3
Using %i: 3
Explanation: Both %d and %i can be used with int values in printf(). In scanf(), %i additionally interprets integer input according to its prefix, allowing decimal, octal, or hexadecimal input.
3. Unsigned Integer Format Specifier - %u
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
3 (Enter by user)
Unsigned Integer: 3
Printing -10 using %u: 4294967286
Explanation: %u formats an unsigned int as a decimal number.
4. Floating-point format specifier - %f
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
Explanation: %f prints the value in decimal notation, while %e and %E print it in scientific notation. The e and E versions differ in the case of the exponent character.
5. Unsigned Octal number for integer - %o
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
Explanation: The decimal value 67 is represented as 103 in octal notation.
6. Unsigned Hexadecimal for integer - %x
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.
#include <stdio.h>
int main() {
int a = 15454;
printf("%x\n", a);
printf("%X", a);
return 0;
}
Output
3c5e 3C5E
Explanation: %x prints hexadecimal digits using lowercase letters, while %X uses uppercase letters.
7. String Format Specifier - %s
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
Explanation: %s prints characters from the string until the terminating null character ('\0') is encountered.
Using %s with scanf()
#include <stdio.h>
int main() {
char str[50];
scanf("%49s", str);
printf("Entered String: %s", str);
return 0;
}
Input
Hi GeeksOutput
Entered String: HiExplanation: %s in scanf() reads characters until whitespace is encountered. The field width 49 prevents more than 49 characters from being stored, leaving space for the terminating '\0'.
To read a string containing spaces, functions such as fgets() or an appropriate scansets can be used.
8. Address Format Specifier - %p
The %p format specifier is used to print a pointer value.
#include <stdio.h>
int main() {
int a = 10;
printf("Address of a: %p", (void *)&a);
return 0;
}
Output
Address of a: 0x7fffffffe9cc
Explanation: %p prints the value of a pointer, which is the address stored in the pointer. The argument is converted to void * for portable use with %p.
Input and Output Formatting
Format specifiers can be combined with formatting options to control how output is displayed. Common formatting options include:
- - flag: Left-aligns the output within the specified field width.
- Width: Specifies the minimum number of characters used for the output.
- Precision: Controls the number of digits for floating-point values, the minimum number of digits for integers, or the maximum number of characters printed for strings.
- 0 flag: Pads numeric output with leading zeros when applicable.
- + flag: Displays a plus sign for positive signed numbers.
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
Explanation: %20s right-aligns the string within a minimum width of 20 characters. %-20s left-aligns it. The .5 precision limits the string to at most five characters.
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 |