scanf format specifier quick reference

scanf format specifier quick reference

scanf specifiers set conversions, pointer types, and read limits. Use matching pointers, allow for null terminators, and check returned assignment counts.

CategoryCheat sheets
KindCheat sheets
Header<stdio.h>
C standardC99
Category Form Argument type or input behavior Example
Integer conversion %d int *; reads a signed integer strictly as decimal. %d
Integer conversion %i int *; auto-detects the base: 0x introduces hexadecimal, a leading 0 introduces octal, and otherwise input is decimal. %i
Integer conversion %u unsigned int *; reads unsigned decimal. %u
Integer conversion %o unsigned int *; reads octal. %o
Integer conversion %x / %X unsigned int *; reads hexadecimal with an optional 0x prefix; uppercase and lowercase forms are equivalent. %x
Floating-point conversion %f / %e / %g float *; all three forms accept decimal floating-point input and do not accept double *. %f
Floating-point conversion %a / %A float * (C99); accepts the same floating-point input syntax as conversions such as %f, including hexadecimal floating-point input, and does not accept double *. %a
Floating-point length l %lf / %le / %lg double *; the l modifier is required in scanf. %lf
Floating-point length L %Lf / %Le / %Lg long double *; uppercase L selects a long double object. %Lf
Integer length h %hd / %hu short int * / unsigned short int *. %hd, %hu
Integer length hh (C99) %hhd / %hhu signed char * / unsigned char *; the pointer types must match, with no printf-style integer promotion for the pointed-to objects. %hhd, %hhu
Integer length l %ld / %lu long int * / unsigned long int *. %ld, %lu
Integer length ll (C99) %lld / %llu long long int * / unsigned long long int *. %lld, %llu
Integer length j (C99) %jd / %ju Accepts intmax_t * / uintmax_t * respectively; the types are defined in stdint.h, and inttypes.h also provides format macros such as SCNdMAX and SCNuMAX. %jd, %ju
Integer length z (C99) %zu Accepts size_t *; size_t is defined in stddef.h and must not be assumed to be unsigned int * or unsigned long * on a particular platform. %zu
Integer length t (C99) %td / %tu %td accepts ptrdiff_t *; %tu accepts a pointer to the unsigned integer type corresponding to ptrdiff_t, which must not be assumed to be size_t *. ptrdiff_t is defined in stddef.h. %td, %tu
Character %c char *; reads exactly one character by default, including whitespace, and appends no null terminator; width can specify a character count. %c
String %s char *; skips leading whitespace, reads a sequence of non-whitespace characters, and appends a null terminator. The destination must be large enough. %31s
scanset %[abc] char *; reads only characters in the set, does not skip leading whitespace, and appends a null terminator. %31[abc]
Inverted scanset %[^...] char *; reads characters outside the set; %[^\n] can read the rest of a line including spaces. %31[^\n]
Pointer %p A pointer to a void pointer object (commonly written void **); the input representation is implementation-defined. %p
Character count %n int *; consumes no input, stores the number of characters consumed so far, and is not counted as a successful assignment. %n
Literal percent sign %% Matches a literal % in the input; consumes no pointer argument and is not counted as an assignment. %%
Field width %5d Reads at most 5 input characters for this conversion; for strings, use a width one less than the array size. %5d, %31s
Assignment suppression * %*d Reads and discards a matching item, takes no pointer argument, and does not increase the returned assignment count. %*d, %*31s
Whitespace in the format " %d" A whitespace directive in the format skips consecutive input whitespace, including spaces, tabs, and newlines; numeric and %s conversions also skip leading whitespace themselves. "%d %d"

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

Sample session (the second line is user input):
Enter name, decimal, based, hex, temperature:
Ada 42 052 2a 21.5
name=Ada
decimal=42
based=42
hex=42
temperature=21.5
⚠️ Warning Always bound %s or a scanset when writing to a character array: for example, use %31s for char name[32]. The width excludes the automatically appended null terminator.
ℹ️ Info scanf returns the number of conversions successfully completed and assigned; suppressed items and %n do not count. It returns EOF if input failure occurs before the first successful conversion; earlier successful assignments still count if a later match fails.
💡 Tip Most numeric conversions and %s skip leading whitespace, while %c and scansets do not. A newline may remain buffered, so handle it deliberately when mixing input methods.

📚 Related Tutorials

❓ FAQ

QWhy can scanf read 010 differently with %d and %i?
A%d always interprets input as decimal, so 010 means 10. %i infers the base from the prefix: a leading 0 selects octal, so 010 means 8, while 0x10 means 16.
QShould scanf use %32s to read a string into char name[32]?
ANo: one position is needed for the terminating null character. Use scanf("%31s", name), which stores at most 31 non-whitespace characters and then appends the terminator as the 32nd character.
QCan a scanf scanset such as %[...] read text containing spaces?
AYes. A scanset reads characters allowed or excluded by the brackets instead of stopping at whitespace as %s does, and it does not skip leading whitespace. Set a maximum field width that leaves room for the null terminator. The caller must still handle any newline left unread.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏