printf() Format used to output to the Screen - C Language Basics

C examples for Language Basics:printf

Introduction

The following table shows how the optional output flag characters affect the output.

Character Use
+ Ensures there's always a plus or minus sign preceding a signed output value. Only negative values have a sign by default.
- left justify the output value and padded with blanks on the right. The default is right justified.
0 Sets that integer or floating point values should be padded with zeros instead of spaces to fill out the field width to the left.
Space Sets that positive or zero values are preceded by a space rather than a plus sign.

# Ensures that:

  • 0 is to precede an octal output value,
  • 0x or 0X is to precede a hexadecimal output value,
  • a floating-point output value will contain a decimal point,
  • for g or G floating-point conversion characters, trailing zeros will not be omitted.

The optional field_width specifies the minimum field width for the output value.

The optional precision specifier is used with floating-point output values and consists of a period followed by an integer.

A specifier of .n indicates that n decimal places are to be output for a floating-point value.

Size Flag Characters in an Output Specification

Flag Effect
hA following integer conversion specifier applies to a short or unsigned short argument.
hh A following integer conversion specifier applies to a signed char or unsigned char argument.
l (lowercase L) A following integer conversion specifier applies to a long or unsigned long argument.
ll (lowercase LL)A following integer conversion specifier applies to a long long or unsigned long long argument.
jA following integer conversion specifier applies to an intmax_t or uintmax_t argument.
zA following integer conversion specifier applies to a size_t argument.
tA following integer conversion specifier applies to a ptrdiff_t argument.
LA following floating-point conversion specifier applies to a long double argument.

Conversion Characters in an Output Specification

Conversion character Output produced
d or i Signed decimal integer
o Unsigned octal integer
u Unsigned decimal integer
x Unsigned hexadecimal integer with lowercase hexadecimal digits a, b, c, d, e, f
X As x but with uppercase hexadecimal digits A, B, C, D, E, F
f or F Signed decimal value
eSigned decimal value with exponent
E As e but with E for exponent instead of e
g As e or f depending on size of value and precision
G As g but with E for exponent values
a or A Presents a double value in hexadecimal preceded by 0x or 0X and any exponent prefixed with p or P, thus: 0xh.hhhhp+/-d where h is a hexadecimal digit.
pOutput the value of the argument as a pointer and the value is presented in an implementation-defined way. The argument should be of type void*.
cSingle character or precision characters
s All characters until '\0' is reached or precision characters have been output

Escape Sequences

You can include whitespace characters in the format control string for printf().

Whitespace characters are the newline, carriage return, form-feed, space, and tab.

Some of these are represented by escape sequences and these are shown in following Table.

Escape sequenceDescription
\b Backspace
\f Form-feed or page eject
\n Newline
\r Carriage return (for printers) or move to the beginning of the current line for output to the screen
\t Horizontal tab
\\ output the backslash character, \

To write a % character to stdout, you use %%.


Related Tutorials