Page 173 - C-Language
P. 173
between the parentheses.
printf("%d is the answer to life, the universe, and everything.", 42);
// 42 is the answer to life, the universe, and everything.
int x = 3;
char y = 'Z';
char* z = "Example";
printf("Int: %d, Char: %c, String: %s", x, y, z);
// Int: 3, Char: Z, String: Example
Alternatively, integers, floating-point numbers, characters, and more can be printed using the
escape character %, followed by a character or sequence of characters denoting the format, known
as the format specifier.
All additional arguments to the function printf() are separated by commas, and these arguments
should be in the same order as the format specifiers. Additional arguments are ignored, while
incorrectly typed arguments or a lack of arguments will cause errors or undefined behavior. Each
argument can be either a literal value or a variable.
After successful execution, the number of characters printed is returned with type int. Otherwise,
a failure returns a negative value.
Length modifiers
The C99 and C11 standards specify the following length modifiers for printf(); their meanings are:
Modifier Modifies Applies to
hh d, i, o, u, x, or X char, signed char or unsigned char
h d, i, o, u, x, or X short int or unsigned short int
l d, i, o, u, x, or X long int or unsigned long int
a, A, e, E, f, F, g, or
l double (for compatibility with scanf(); undefined in C90)
G
ll d, i, o, u, x, or X long long int or unsigned long long int
j d, i, o, u, x, or X intmax_t or uintmax_t
size_t or the corresponding signed type (ssize_t in
z d, i, o, u, x, or X
POSIX)
t d, i, o, u, x, or X ptrdiff_t or the corresponding unsigned integer type
a, A, e, E, f, F, g, or
L long double
G
https://riptutorial.com/ 149

