How to use C printf with format specifiers. Interactive tester for %d / %f / %s / width / precision.
printf("...%d...\n", x); prints a value%d=int, %f=double, %c=char, %s=stringscanf("%d", &x); reads input β don't forget &\n is newline%5d, %.2f)%x (hex), %o (octal)printf displays a string (a sequence of characters) on the screen.\n is a special escape sequence that means "newline."
printf("Hello World!\n"); // Print a string printf("5 x 6 = %d\n", 30); // Embed a number inside the string printf("result = %d\n", value); // Display a variable's value
int a = 5;printf("%d", a); → 5%2d → " 5", %4d → " 5"double b = 5.23;printf("%f", b); → 5.230000%5.2f → " 1.52" (width 5, 2 decimals)printf with %d %d %d for three variables%5d, %.3f, %-10s\n (newline) and \t (tab) to align text%c (char), %x (hex), %o (octal)Check your understanding of this lesson!
%d is the format specifier for integers. The value 10 is printed as-is.
scanf requires the address of the variable, so you must prefix it with & (the address-of operator).
%.2f prints a floating-point number with two digits after the decimal point. 3.14159 becomes 3.14.