printf.c

/*
 * 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
 *
 * Matt Bishop, Oct. 11, 2019
 * 	original program written
 */
#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 nnarrower 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 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 ignores the .4: >%12.4c<\n", 'A');
	printf("%%012c ignores the 0:   >%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 ignores the 0:   >%012s<\n", "When in the course of human events");

	/*
	 * Th-th-that's all, folks!
	 */
	return(0);
}



UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh