Introduction to C / C++ Programming
Formatted Input and Output

Accreditation

The content of the tables included on this page were copied from cplusplus.com pages on printf and scanf

printf

			printf( "format string " [ , expression ] ... );

printf format specifiers

			%[flags][width][.precision][length]specifier

printf specifiers

Note: CS 107 students are not responsible for knowing the a, A, n, or p specifiers.

specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n Nothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.
 
% A % followed by another % character will write a single % to the stream. %

 

printf length sub-specifiers

Note: CS 107 students are not responsible for knowing j, z, or t length specifiers, or wide character types

  specifiers
length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char         signed char*
h short int unsigned short int         short int*
l long int unsigned long int   wint_t wchar_t*   long int*
ll long long int unsigned long long int         long long int*
j intmax_t uintmax_t         intmax_t*
z size_t size_t         size_t*
t ptrdiff_t ptrdiff_t         ptrdiff_t*
L     long double        

 

scanf

			printf( "format string " [ , expression ] ... );

Scanf format specifiers

			%[*][width][length]specifier

scanf specifiers

Note: CS 107 students are not required to know the a,p, [ ], [^ ], or n specifiers

specifier Description Characters extracted
i, u Integer Any number of digits, optionally preceded by a sign (+ or -).
Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f).
d Decimal integer Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
o Octal integer Any number of octal digits (0-7), optionally preceded by a sign (+ or -).
x Hexadecimal integer Any number of hexadecimal digits (0-9, a-f, A-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -).
f, e, g Floating point number A series of decimal digits (or hexadecimal, if preceded by 0x or 0X), optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or p or P and an hexadecimal integer). Or any other sequence supported by strtod.
a
c Character The next character. If a width other than 1 is specified, the function reads exactly width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.
s String of characters Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
p Pointer address A sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %p in fprintf.
[characters] Scanset Any number of the characters specified between the brackets.
A dash (-) that is not the first character may produce non-portable behavior in some library implementations.
[^characters] Negated scanset Any number of characters none of them specified as characters between the brackets.
n Count No input is consumed.
The number of characters written so far is stored in the pointed location.
% % A % followed by another % matches a single %.

 

scanf length specifiers

Note: CS 107 students are not required to know the j, z, or t specifiers, or wide ( long ) character types

specifiers
length d i u o x f e g a c s [] [^] p n
(none) int* unsigned int* float* char* void** int*
hh signed char* unsigned char*

 

 

 

signed char*
h short int* unsigned short int*

 

 

 

short int*
l long int* unsigned long int* double* wchar_t*

 

long int*
ll long long int* unsigned long long int*

 

 

 

long long int*
j intmax_t* uintmax_t*

 

 

 

intmax_t*
z size_t* size_t*

 

 

 

size_t*
t ptrdiff_t* ptrdiff_t*

 

 

 

ptrdiff_t*
L

 

 

long double*

 

 

 

 

Related functions