printf
(man 3 printf)
!1
type format sizeof (x86_64)
char %c 1
short %hd 2
unsigned short %hu 2
int %d 4
base types unsigned int %u 4
long %ld 8
unsigned long %lu 8
float %f 4
double %f 8
typedef’d
size_t %zu 8
ssize_t %zd 8
long x = 5;
Example: printf(“x = %ld, sizeof(x) = %zu\n”, x, sizeof(x));
!2
Other Common Format Specifiers
type format note
char, short, int %x print as hexidecimal
long %lx print as hexadecimal
char *s %s Print a nul-terminated
string
Example:
int i = 10;
printf(“%x\n”, i); // a
printf(“0x%x\n”, i); // 0xa
printf(“%#x\n”, i); // 0xa
printf(“0x%02x\n”, i); // 0x0a
!3
Related Functions
Print target
printf(“i = %d\n”, i); stdout
fprintf(stderr, “i = %d\n”, i); stderr
int len;
char s[32] = { 0 };
len = snprintf(s, sizeof(s), “i = %d\n”, i); string
!4