CS211: (Appendix) Using printf
Table of Contents
Introduction
One of the easiest ways for beginngers to get output from a C program is to use the standard library printf function. The major advantage is that it is relatively easy to use and comes for free with an include of stdio.h. The major disadvantage is new programmers often don't understand functions yet and find the formatted string argument a bit arcane. This appendix exists to try to minimize confusion.
The Anatomy of printf
int printf(char *format, arg1, arg2, ...);
printf is a variable argument function which prints out a string of characters to standard out—which for this class is pretty much always linked to the terminal screen. The first argument is always a string of characters, and every subsequent argument is a value that corresponds to a format string variable. For example, printf("%d - %d = %d", 3, 2, 1) will print out "3 - 2 = 1" because the first %d maps to 3, the second %d mapes to 2, and the final %d maps to 1.
Formatted String
The first argument taken in printf is a formatted string. The formatting comes in the form of special characters and symbols peppered throughout the character sequence. For example, above you saw the special characters %d which is programmed to represent an integer value. Here is a table of conversion characters:
| Character | Argument Type | Printed As |
|---|---|---|
| d, i | int | Decimal number |
| o | int | Unsigned octal number (without leading zero) |
| x, X | int | Unsigned hexadecimal number (without leading 0x |
| or 0X), using abcdef or ABCDEF for 10, …, 15 | ||
| u | int | Unsigned decimal number |
| c | int | Single character |
| s | char * | Print characters from the string until \0 or the |
| number of characters given by the precision | ||
| f | double | [-]m.dddddd, where the number of d's is given by |
| the precision (default 6) | ||
| e, E | double | [-]m.dddddde+/-xx or [-]m.ddddddE+/-xx, where |
| the number of d's is given by the precision | ||
| (default 6) | ||
| g, G | double | Use %e or %E if the exponent is less than -4 or |
| greater than or equal to the precision; otherwise | ||
| use %f. Trailing zeros and a trailing decimal | ||
| point are not printed | ||
| p | void * | Pointer (implementation-dependent) |
| % | no argument | Print a % |
printf Examples
Here are a few examples using printf that you can learn from and try out yourself:
1: #include <stdio.h> 2: int main(){ 3: 4: /* 1. Basic integer and string */ 5: printf("Name: %s, Age: %d\n", "Alice", 30); 6: 7: /* 2. Padded integer right-aligned in field of width 10 */ 8: printf("Score: %10d\n", 42); 9: 10: /* 3. Padded integer left-aligned in field of width 10 */ 11: printf("Score: %-10d|\n", 42); 12: 13: /* 4. Zero-padded integer */ 14: printf("ID: %08d\n", 1234); 15: 16: /* 5. Hexadecimal upper and lower case */ 17: printf("Hex: %x HEX: %X\n", 255, 255); 18: 19: /* 6. Hexadecimal with 0x prefix */ 20: printf("Hex with prefix: %#x\n", 255); 21: 22: /* 7. Float with default precision */ 23: printf("Pi: %f\n", 3.14159265); 24: 25: /* 8. Float with custom precision */ 26: printf("Pi (2dp): %.2f\n", 3.14159265); 27: 28: /* 9. Float in scientific notation */ 29: printf("Scientific: %e\n", 123456789.0); 30: 31: /* 10. Float using %g (auto selects %e or %f) */ 32: printf("Auto: %g Auto: %g\n", 0.00001, 123456.0); 33: 34: /* 11. Character and its decimal value */ 35: printf("Char: %c Dec: %d\n", 'A', 'A'); 36: 37: /* 12. String with precision (truncate after 5 chars) */ 38: printf("Truncated: %.5s\n", "Hello, World!"); 39: 40: /* 13. String padded to width 20, left-aligned */ 41: printf("%-20s|\n", "left"); 42: 43: /* 14. Pointer address */ 44: int x = 42; 45: printf("Address of x: %p\n", (void *)&x); 46: 47: /* 15. Mixed: octal, unsigned, padded float, and percent sign */ 48: printf("Oct: %o Uint: %u Float: %8.3f Pct: 100%%\n", 49: 255, 4294967295u, 3.14159); 50: 51: return 0; 52: }