{ }codelper

Reference › C

C printf(): Format Specifiers and Output

printf takes the values you list and slots them, in order, into places like %d inside the format string, then prints the result to the screen. It does not start a new line on its own, so you write \n yourself. To use it, you need #include <stdio.h> at the top.

printf("format string", value1, value2, ...);

Format specifiers %d %f %c %s

The pieces that start with % are called format specifiers. Pick the one that matches the type of the value: %d for an integer int, %f for a floating-point double or float, %c for a single character char, and %s for a string.

#include <stdio.h> int main(void) {    int age = 17;    double height = 172.5;    char grade = 'A';    char name[] = "Mina";    printf("%d\n", age);    printf("%f\n", height);    printf("%c\n", grade);    printf("%s is %d\n", name, age);    return 0;}
Output
17
172.500000
A
Mina is 17

Edit and run it →

If you don't set the number of digits, %f prints six digits after the decimal point. When there are several format specifiers, the values are matched to them in order from the first.

Setting decimal places with %.2f

Write something like .2 between the % and the f to print only that many digits after the decimal point. The value is rounded, not cut off. %.0f prints with no decimal point.

#include <stdio.h> int main(void) {    double pi = 3.14159;    printf("%.2f\n", pi);    printf("%.3f\n", pi);    printf("%.0f\n", pi);    printf("%.1f\n", 2.0 / 3);    return 0;}
Output
3.14
3.142
3
0.7

Edit and run it →

Setting the width with %5d %-5d %02d

The number right after % is the minimum width. %5d takes five columns and prints the value against the right edge, and %-5d puts it against the left. Put a 0 in front, as in %05d, to fill with zeros instead of spaces. If the value is longer than the width, it is not cut off. All of it is printed.

#include <stdio.h> int main(void) {    printf("[%5d]\n", 42);    printf("[%-5d]\n", 42);    printf("[%05d]\n", 42);    printf("[%8.2f]\n", 3.14159);    printf("[%-6s][%6s]\n", "ab", "cd");    printf("%02d:%02d\n", 9, 5);    printf("[%3d]\n", 12345);    return 0;}
Output
[   42]
[42   ]
[00042]
[    3.14]
[ab    ][    cd]
09:05
[12345]

Edit and run it →

The 8 in %8.2f is the total width, including the decimal point. Use %02d to print a time like 09:05.

Printing long long, a % sign, and quotes

A long long, a large integer that doesn't fit in an int, is printed with %lld. Write %% for the % character itself, \" for a double quote, and \\ for a backslash.

#include <stdio.h> int main(void) {    long long big = 3000000000LL;    printf("%lld\n", big);    printf("%d%%\n", 80);    printf("\"hi\"\n");    printf("C:\\temp\n");    return 0;}
Output
3000000000
80%
"hi"
C:\temp

Edit and run it →

Common mistakes

Leaving out \n, so the output runs together on one line

#include <stdio.h>

int main(void) {
    printf("apple");
    printf("banana");
    return 0;
}

→ One line, applebanana, not two lines

printf does not start a new line for you. Write \n where the line should end: printf("apple\n");

The format specifier and the type of the value don't match

double x = 3.14;
printf("%d\n", x);      /* a double with %d */
printf("%f\n", 5 / 2);  /* an int with %f */

→ What gets printed is not defined (undefined behavior). Often a strange number comes out

printf does not convert values for you. Use the format specifier that matches the type (%f, %d), or convert the value before you pass it: printf("%f\n", 5 / 2.0);

Printing the result of integer division as a decimal

#include <stdio.h>

int main(void) {
    int sum = 7, n = 2;
    double avg = sum / n;
    printf("%.1f\n", avg);
    return 0;
}

→ 3.0, not 3.5

sum / n divides an integer by an integer, so the part after the decimal point has already been thrown away. You have to fix the calculation, not the format: (double)sum / n.

Fewer values than format specifiers

int a = 3, b = 4;
printf("%d + %d = %d\n", a, b);  /* one value is missing */

→ What gets printed in the third place is not defined (undefined behavior)

Pass as many values as there are format specifiers: printf("%d + %d = %d\n", a, b, a + b);. If you compile with -Wall turned on, you get a warning when they don't match up like this.

Practice problems

See also