Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Variables  ›  Lesson

Printing Variables

Printing Variables 12 minprintf builds text from a format string in which every conversion specifier must be matched, left to right, by an argument of the right type.

Printing Variables

At the end of each day the logger prints a summary line: how many readings, the average, the unit. printf builds that line from a template and a list of values. It is fast and flexible, but it trusts you completely: it cannot see the types of your values, so if the template and the values disagree, the output is garbage or worse.

The rule

Every % in printf needs one matching value of the matching type.

The first argument is the format string. Each conversion specifier, starting with %, is replaced by the next argument, left to right:

int count = 3;
double avg = 21.466;
char unit = 'C';

printf("%d readings, average %.1f %c\n", count, avg, unit);
/* prints: 3 readings, average 21.5 C */

The specifiers you will use most:

Specifier Type Example output
%d int 42
%u unsigned int 42
%ld / %lld long / long long 5256000
%zu size_t (from sizeof) 96
%f double (a float is promoted to double) 21.466000
%c a character C
%s a string ready
%x unsigned int in hexadecimal 2a
%% a literal percent sign %

Between % and the letter you can add a width and a precision:

  • %5d pads the number to at least 5 characters, right-aligned; %-5d left-aligns.
  • %.1f shows one digit after the decimal point, rounding the value.
  • %8.2f does both.

If a specifier does not match its argument's type, for example %d given a double, the behaviour is undefined. -Wall turns on GCC's and Clang's format checks, which catch most mismatches at compile time.

The shortcut

Pair them up. Draw an imaginary line from each % to its argument, left to right, and check two things: the counts match, and each type fits its letter.

Checking the summary lineStep 1 of 4
  1. Count

    %d, %.1f, %c: three specifiers. count, avg, unit: three arguments. Good.

Drills

Answer fast — trust the rule.

quick drill

Which specifier prints a size_t, such as the result of sizeof hourly?

quick drill

What does printf("%.1f", 19.27); print?

quick drill

What does printf("[%4d]", 42); print?

quick drill

What does printf("%d%%\n", 75); print?

quick drill

What does printf("%d\n", 21.5); do?

Remember

  • Every % in printf needs one matching value of the matching type.
  • Shortcut: pair each % with its argument, left to right; check the count and the types.
  • Trap: a mismatch is undefined behaviour, not a conversion. Build with -Wall to catch it.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainAuditing a printf

The line printf("%d readings, average %d C\n", count, avg); prints nonsense when avg is a double. Explain what is wrong, how to fix it to show one decimal place, and how you could have caught it before running.