0% found this document useful (0 votes)
4 views4 pages

printf Format Specifiers Guide

The document provides a reference for the printf function in C, detailing format specifiers for various data types and their corresponding sizes on x86_64 architecture. It includes examples of how to use printf with different types, such as integers and strings, as well as related functions like fprintf and snprintf. Additionally, it highlights common format specifiers for hexadecimal representation.

Uploaded by

hili dimbeya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

printf Format Specifiers Guide

The document provides a reference for the printf function in C, detailing format specifiers for various data types and their corresponding sizes on x86_64 architecture. It includes examples of how to use printf with different types, such as integers and strings, as well as related functions like fprintf and snprintf. Additionally, it highlights common format specifiers for hexadecimal representation.

Uploaded by

hili dimbeya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like