Format Specifiers in C
Format Specifiers in C
Format Specifiers in C
Format specifiers in C are certain special symbols used in the formatted console IO
functions such as printf() and scanf(), as well as formatted file IO functions such as
fprintf() and fscanf().
The computer receives data from the stream in a text form, however you may want
to parse it in variables of different data types such as int, float or a string. Similarly,
the data stored in int, float or char variables has to be sent to the output stream in a
text format. Format specifier symbols are used exactly for this purpose.
The first argument of this function is a string that is interspersed with one or more
format specifiers. There may be one or more expressions as argument after the first.
The compiler substitutes each format specifier with the value of its successive
expression. The resultant formatted string is then passed to the output stream.
Example
In the following code, we have an int variable age and a float variable percent. The
printf() function prints the values of both as below −
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 1/8
6/16/24, 11:02 AM Format Specifiers in C
#include <stdio.h>
int main(){
return 0;
}
Output
When you run the code, it will produce the following output −
Age: 18
Percent: 67.750000
The value of the first variable replaces the first format specifier %d. Similarly, %f is
substituted by the value of the percent variable.
Example
In this example, the program asks the user to input age and percent values. They
are stored in the int and float variables, respectively.
#include <stdio.h>
int main(){
int age;
float percent;
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 2/8
6/16/24, 11:02 AM Format Specifiers in C
return 0;
}
Output
%c Character
%d Signed integer
%f Float values
%g or %G Similar as %e or %E
%i Unsigned integer
%lf Double
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 3/8
6/16/24, 11:02 AM Format Specifiers in C
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
A number after % specifies the minimum field width. If a string is less than
the width, it will be filled with spaces.
Example
The following example highlights how integer format specifiers are used in C −
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 4/8
6/16/24, 11:02 AM Format Specifiers in C
return 0;
}
Output
When you run this code, it will produce the following output −
Signed integer: 20
Unsigned integer: 20
Long integer: 20
Octal integer: 24
Hexadecimal integer: 14
Floating-point Formats
C uses the %f format specifier for single precision float number, %lf for double
precision, %Lf for long double number. To represent a floating point number in
scientific notation, C uses the %e or %E specifier symbol.
You can specify the width and the precision in the form of number of places after the
decimal point. For example, to state the width of number to 4 digits with 2 digits
after the decimal point, use the form %4.2f.
Example
#include <stdio.h>
int main(){
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 5/8
6/16/24, 11:02 AM Format Specifiers in C
Output
float: 5.347000
double: 5.347000
Scientific notation: 5.347000e+000
width and precision: 5.35
String Formats
The char data type in C is actually a subset of int data type. Hence, a char variable
with %c format specifier corresponds to the character in single quotes. On the other
hand, if you use the %d specifier, then the char variable will be formatted to its
ASCII value.
Example
#include <stdio.h>
int main(){
char ch = 'D';
char word[]="Hello World";
return 0;
Output
When you run this code, it will produce the following output −
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 6/8
6/16/24, 11:02 AM Format Specifiers in C
As character: D
As its ASCII value: 68
String format: Hello World
Example 1
The following code opens a file in write mode and saves the values of three variables
in it.
#include <stdio.h>
int main(){
int x,y,z;
fclose(fp);
return 0;
}
Output
The fprintf() function uses the file represented by the file pointer fp to write the data.
The next example shows how you can open the same file in read mode to read the
formatted data.
Example 2
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 7/8
6/16/24, 11:02 AM Format Specifiers in C
The following program reads back the data from the file by opening it in read mode.
#include <stdio.h>
int main(){
int x,y,z;
fclose(fp);
return 0;
}
Output
The fscanf() function reads the formatted input from fp which is the pointer to the
file opened. Here, you will get the following output −
10, 20, 30
https://www.tutorialspoint.com/cprogramming/c_format_specifiers.htm 8/8