C & C++ fundamentals / 02
Format specifiers
Use format specifiers to print integers, floating-point numbers, characters, and strings.
On this page
C and C++ have format specifiers for formatted input and output. The introductory set used in this series is:
| Specifier | Value |
|---|---|
%d | Decimal integer |
%f | Floating-point number |
%lf | Double-precision floating-point number, particularly for scanf |
%c | A character |
%s | A string |
Today, we will use these specifiers to print values.
printf("%d", 3);prints the decimal integer3.printf("%lf", 3.14);prints the floating-point value3.14using floating-point formatting.printf("%c", 'A');prints the single characterA.printf("%s", "Hello");prints the stringHello.
Characters and strings need quotation marks: single quotes for a character such as 'A', and double quotes for a string such as "Hello". Numeric literals do not need quotation marks.
Editorial note: for
printf, floating-point arguments are passed asdouble, and%fis the usual choice. Forscanf,%freads into afloatand%lfreads into adouble; this distinction matters.
The next lesson will cover input.
Source examples
Print a single character:
// Print the character A
#include <stdio.h>
int main()
{
printf("%c",'A');
return 0;
}
Print a string:
// Print Hello World!
#include <stdio.h>
int main()
{
printf("%s","Hello World!");
return 0;
}
Print an integer:
// Print 3
#include <stdio.h>
int main()
{
printf("%d",3);
return 0;
}
Print a floating-point number:
// Print 3.141592
#include <stdio.h>
int main()
{
printf("%lf",3.141592);
return 0;
}
Moved here from Tistory
I migrated this post from my Korean Tistory blog, I am Jason Lee, to this website and translated it into English. My writing and projects now live together in one place.
The original publication date, code examples, and screenshots are preserved. Editorial notes clarify known issues in the original material.
Original post on Tistory English edition · Aug 30, 2026