/* * PRINTF -- shows some of the formatting you can do with printf * * Usage: printf (or ./printf) * * Inputs: none * Outputs: various formatted values * Exit Code: EXIT_SUCCESS (0) because all goes well * * IMPORTANT NOTE: * When you compile this with gcc and -Wall, you get * warnings about bad printfs; those are there for * demonstration * * written for ECS 030, Fall 2015 * * Matt Bishop, Oct. 1, 2015 * original program written */ #include #include /* * various values to print */ #define INT1 1274699 /* an integer */ #define FLOAT1 3568.234987 /* float with small integer part */ #define FLOAT2 12746993568.124987 /* float with large integer part */ #define CHAR1 'A' /* character */ /* long string */ #define STRING1 "When in the course of human events" /* * the program */ int main(void) { /* * basic integers */ printf("Here we use %%d to print an integer: %d\n", INT1); printf("We can use %%x to print it in base 16 (hexadecimal): %x\n", INT1); printf("\t and %%#x to put 0x in front (customary): %#x\n", INT1); printf("We can use %%o to print it in base 8 (octal): %o\n", INT1); printf("\t and %%#o to put 0 in front (customary): %#o\n", INT1); /* * let's get fancy * * first, a leading sign */ printf("\n\nNow let's get fancy\nFirst comes a sign\n"); printf("%%+d always produces a sign: %+d, %+d\n", INT1, -INT1); /* * next, field width */ printf("\nNow we'll give a field width \t 123456789012\n"); printf("-- by %%12d, which is wider than the number:\t>%12d<\n", INT1); printf("-- by %%2d, which is nnarrower than the number:\t>%2d<\n", INT1); /* * now, alignment (left or right) */ printf("\nNow comes alignment\n"); printf("By default the number is right-aligned: >%12d<\n", INT1); printf("But a '-' makes the number left--aligned: >%-12d<\n", INT1); /* * now pad it with zeros */ printf("\nNow comes padding\n"); printf("%%012d pads the field with leading 0s: >%012d<\n", INT1); printf("%%-012d ignores the 0 (no leading space): >%-012d<\n", INT1); /* * okay, now come the non-integers */ printf("\n\nNow, let's look at some non-integers\n"); printf("%%f prints a floating point number: %f\n", FLOAT1); printf("%%10.4f prints it with 4 decimal places and in a width of 10: >%10.4f<\n", FLOAT1); printf("%%10.0f prints it without a decimal point in a width of 10: >%10.0f<\n", FLOAT1); printf("%%e prints a floating point number: %e\n", FLOAT1); printf("%%10.4e prints a floating point number: %10.4e\n", FLOAT1); printf("%%10.0e prints it without a decimal point: >%10.0e<\n", FLOAT1); printf("%%g uses %%e or %%f, whichever is more appropriate\n"); printf(" If the number of integer digits is small (%f), you get: %g\n", FLOAT1, FLOAT1); printf(" But if that number is large (%f), you get: %g\n", FLOAT2, FLOAT2); /* * now come characters and strings */ printf("\n\nCHaracters and strings\n"); printf("%%c prints a character: %c\n", CHAR1); printf("\nNow we'll give a field width 123456789012\n"); printf("%%12c prints a character in a field of width 12: >%12c<\n", CHAR1); printf("%%12.4c ignores the .4: >%12.4c<\n", CHAR1); printf("%%012c ignores the 0: >%012c<\n", CHAR1); printf("%%s prints a string: %s\n", STRING1); printf("\nNow we'll give a field width 123456789012\n"); printf("%%12s prints a string in a field of width 12: >%12s<\n", STRING1); printf("%%12.4s prints the first 4 characters in a field of width 12: >%12.4s<\n", STRING1); printf("%%-12.4s right-justifies it: >%-12.4s<\n", STRING1); printf("%%012s ignores the 0: >%012s<\n", STRING1); /* * Th-th-that's all, folks! */ return(EXIT_SUCCESS); }