/*
* PRINTF -- shows some of the formatting you can do with printf
*
* Usage: printf (or ./printf)
*
* Inputs: none
* Outputs: various formatted values
* Exit Code: 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 36A, Fall 2019
* modified for ECS 36A, Spring 2025 to fix incompatibilities with earlier version
*
* Matt Bishop, Oct. 11, 2019
* original program written
* Matt Bishop, Apr. 6m,2025
* modified for C11 to fix incompatibilities with earlier version of C
*/
#include <stdio.h>
#include <stdlib.h>
/*
* the program
*/
int main(void)
{
/*
* basic integers
*/
printf("Here we use %%d to print an integer: %d\n", 1274699);
printf("We can use %%x to print it in base 16 (hexadecimal): %x\n", 1274699);
printf("\t and %%#x to put 0x in front (customary): %#x\n", 1274699);
printf("We can use %%o to print it in base 8 (octal): %o\n", 1274699);
printf("\t and %%#o to put 0 in front (customary): %#o\n", 1274699);
/*
* 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", 1274699, -1274699);
/*
* 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", 1274699);
printf("-- by %%2d, which is narrower than the number:\t>%2d<\n", 1274699);
/*
* now, alignment (left or right)
*/
printf("\nNow comes alignment\n");
printf("By default the number is right-aligned: >%12d<\n", 1274699);
printf("But a '-' makes the number left--aligned: >%-12d<\n", 1274699);
/*
* now pad it with zeros
*/
printf("\nNow comes padding\n");
printf("%%012d pads the field with leading 0s: >%012d<\n", 1274699);
printf("%%-012d produces results that depend on the system (the '0' causes it to be undefined)\n");
printf("%%-012d on this system ignores the 0 (no leading space): >%-012d<\n", 1274699);
/*
* 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", 3568.234987);
printf("%%10.4f prints it with 4 decimal places and in a width of 10: >%10.4f<\n", 3568.234987);
printf("%%10.0f prints it without a decimal point in a width of 10: >%10.0f<\n", 3568.234987);
printf("%%e prints a floating point number: %e\n", 3568.234987);
printf("%%10.4e prints a floating point number: %10.4e\n", 3568.234987);
printf("%%10.0e prints it without a decimal point: >%10.0e<\n", 3568.234987);
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", 3568.234987, 3568.234987);
printf(" But if that number is large (%f), you get: %g\n", 12746993568.124987, 12746993568.124987);
/*
* now come characters and strings
*/
printf("\n\nCHaracters and strings\n");
printf("%%c prints a character: %c\n", 'A');
printf("\nNow we'll give a field width 123456789012\n");
printf("%%12c prints a character in a field of width 12: >%12c<\n", 'A');
printf("%%12.4c produces results that depend on the system (the '.4' causes it to be undefined)\n");
printf("%%12.4c on this system ignores the .4: >%12.4c<\n", 'A');
printf("%%012c produces results that depend on the system (the '0' causes it to be undefined)\n");
printf("%%012c on this system pads with 0s: >%012c<\n", 'A');
printf("%%s prints a string: %s\n", "When in the course of human events");
printf("\nNow we'll give a field width 123456789012\n");
printf("%%12s prints a string in a field of width 12: >%12s<\n", "When in the course of human events");
printf("%%12.4s prints the first 4 characters in a field of width 12: >%12.4s<\n", "When in the course of human events");
printf("%%-12.4s right-justifies it: >%-12.4s<\n", "When in the course of human events");
printf("%%012s produces results that depend on the system (the '0' causes it to be undefined)\n");
printf("%%012s on this system ignores the 0: >%012s<\n", "When in the course of human events");
/*
* Th-th-that's all, folks!
*/
return(0);
}
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |