printf and Format Specifiers
printf prints formatted text. Specifiers: %d int, %u unsigned, %ld long, %f double, %c char, %s string, %p pointer, %x hex and %% a percent sign. Width and precision look like %5d and %.2f.
Example
int n = 42; double pi = 3.14159; char c = 'A'; char *s = "text";
printf("%d %f %c %s\n", n, pi, c, s);
printf("[%5d] [%-5d] [%05d] [%.2f]\n", n, n, n, pi);
printf("%x %o %c%%\n", 255, 8, 66);Result
42 3.141590 A text [ 42] [42 ] [00042] [3.14] ff 10 B%
Exercise
Print a double.
printf("%", 2.5);
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.