Example 1: Printing a message. The following statement prints a text string to the screen.
printf("Hello, world!\n");
In this example, the format string is simply printed to the screen.
The character ``\n
'' at the end of the string signifies
end-of-line. When an end-of-line character is printed, the LCD
screen will be cleared when a subsequent character is printed. Thus,
most
printf statements are terminated by a \n
.
Example 2: Printing a number. The following statement prints the value of the integer variable x with a brief message.
printf("Value is %d\n", x);
The special form %d is used to format the printing of an integer in decimal format.
Example 3: Printing a number in binary. The following statement prints the value of the integer variable x as a binary number.
printf("Value is %b\n", x);
The special form %b is used to format the printing of an integer in binary format. Only the low byte of the number is printed.
Example 4: Printing a floating point number. The following statement prints the value of the floating point variable n as a floating point number.
printf("Value is %f\n", n);
The special form %f is used to format the printing of floating point number.
Example 5: Printing two numbers in hexadecimal format.
printf("A=%x B=%x\n", a, b);
The form %x formats an integer to print in hexadecimal.