printf is used to O/P data to STDOUT (usually the screen). It has many formatting options and is most useful to display binary data as ASCII e.g. Radix Conversion.
This is an example of printf in its simplest form.
#include <stdio.h> main() { printf("This text will appear on the screen\n"); } |
printf is passed one formatting argument. The unusual thing about the example (in my mind) is \n, this is actually an escape sequence that signals a new line. Without it, any printf's that follow would O/P to the same line. printf also takes extra arguments which are inserted into the format string at locations marked with a %. To printf a % itself use %% rather than \%.
#include <stdio.h> main() { int number=42; printf("The answer is %i\n", number); } |
What happens here is the %i is seen as a formatting identifer for the next argument (number). In this case an integer is expected.
Top | Master Index | Keywords | Functions |