printf

printf
printf is so useful that you probably should spend some time with the man page. It's "man 3 printf ", not "man printf ". The manual is dense and long, so here are some of the tricks that students in the class have found useful so far:

code format="c" int sign=1, exponent=34; float fraction=1.34568 printf("%+d * %10.8f * 2^%03d\n"); code

Will output "+1 * 1.34568000 * 2^034 ".
 * the "+" flag indicates that a leading "+" should be printed for positive numbers (with "-" printed for negatives as usual)
 * the 10.8 indicates that a minimum of 10 characters should be printed, with a minimum of 8 after the decimal
 * the 03 means to print 3 characters wide, with leading zeros (if there was no zero in the format string, it would print leading spaces)

Data sizes:
 * "h" and "l" mean half and long:
 * %hhd expects / prints a half half int (that's a signed or unsigned char)
 * %lld expects / prints a long long int (that's 64 bits)
 * 32 bit ints are generally the same as long ints on most platforms, so ld = d
 * "L" works for doubles:
 * %ld is a long double

Conversions:
 * %o is octal. %x is hex; you can use h's and l's with these
 * %X is also hex, but just prints uppercase letters
 * %e prints a double in exponential form [-]d.ddde±dd. %E does the same, but prints [-]d.dddE±dd
 * %a prints a double or a float in hex, as [-]0xh.hhhhp±d. This is helpful for debugging the difference between decimal and binary representations
 * %p is for printing pointers in hex notation, and prints them as if by using "%#x". (the # flag prefixes the number with "0x")