Introduction to Programming

printf


Each time you call printf, you need to give it at least one argument. The first argument will be the text that you put on the screen. You can put a number of "format codes" in it that will be translated into something else. The format codes start with a percent % and then a letter. For every format code you put in the first argument, you need to add one more argument to printf containing the value you want put onto the screen.

%d
 An int (whole number)
%f
 A float (real number)
%c
 A char (single character)
\n
 A new-line (no new argument needed)

For example:
printf("The value of a is %d\n", a);
printf("One %d  Two %d  Three %d\n", 1, 2, 3);
printf("integer: %d  floatint point: %f  character: %c\n", my_int, my_float, my_char);
printf("integer: %d  floatint point: %f  character: %c\n", 5, 3.14159, 'j');
printf("%d%d%d%d%d%d%d\n", a, a, a, a, a, a, a);


Next: Loops

Previous: Variables


Introduction to Programming

Home