C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

The printf() controlString controls exactly how your output will appear. The only reason
two spaces appear between the numbers is that the controlString has two spaces between each
%f.


Putting It All Together with a Code Example


Consider the following program listing:


Click here to view code image


/* Absolute Beginner's Guide to C, 3rd Edition
Chapter 4 Example 2--Chapter4ex1.c */
#include <stdio.h>
main()
{
/* Here is some more code to help you with printf(), Escape
Sequences, and Conversion Characters */
printf("Quantity\tCost\tTotal\n");
printf("%d\t\t$%.2f\t$%.2f\n", 3, 9.99, 29.97);
printf("Too many spaces \b\b\b\b can be fixed with the ");
printf("\\%c Escape character\n", 'b');
printf("\n\a\n\a\n\a\n\aSkip a few lines, and beep ");
printf("a few beeps.\n\n\n");
printf("%s %c.", "You are kicking butt learning", 'C');
printf("You just finished chapter %d.\nYou have finished ", 4);
printf("%.1f%c of the book.\n", 12.500, '%');
printf("\n\nOne third equals %.2f or ", 0.333333);
printf("%.3f or %.4f or ", 0.333333, 0.333333);
printf("%.5f or %.6f\n\n\n", 0.333333, 0.3333333);

return 0;
}

Enter this code and compile and run the program. You get the output in Figure 4.2.

Free download pdf