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:

SpecifierValue
%dDecimal integer
%fFloating-point number
%lfDouble-precision floating-point number, particularly for scanf
%cA character
%sA string

Today, we will use these specifiers to print values.

  1. printf("%d", 3); prints the decimal integer 3.
  2. printf("%lf", 3.14); prints the floating-point value 3.14 using floating-point formatting.
  3. printf("%c", 'A'); prints the single character A.
  4. printf("%s", "Hello"); prints the string Hello.

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 as double, and %f is the usual choice. For scanf, %f reads into a float and %lf reads into a double; 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
← Back to all articles