← The C Language Lesson 2 of 77 · 10 XP

Output: printf

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

printf means "print formatted": printing by a template. In the template string, %… specifiers are replaced by the values that come after the comma:

int age = 20;
double pi = 3.14159;
printf("I am %d years old\n", age);   // I am 20 years old
printf("pi = %.2f\n", pi);            // pi = 3.14
printf("%d + %d = %d\n", 2, 3, 2 + 3);

The main specifiers:

type example
%d int 42
%f double 3.140000
%.2f double, 2 decimal places 3.14
%c char A
%s string hello
%x int in hexadecimal ff
%% the % sign itself %

Field width: %5d pads the number with spaces on the left up to 5 characters, %-5d pads on the right, %05d pads with zeros.

The specifier must match the value's type. The compiler lets printf("%d", 3.5) through with a warning, but it prints garbage. Read the compiler's warnings — they often save you.

Special characters: \n — a new line, \t — a tab, \" — a quote, \\ — a backslash.

Task. Print exactly this line (with the quotes and the percent sign):

Progress: "75%" done

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.