C Format Specifier

The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character.

The commonly used format specifiers in printf() function are:

Format specifierDescription
%d or %iIt is used to print the signed integer value where signed integer means that the variable can hold both positive and negative values.
%uIt is used to print the unsigned integer value where the unsigned integer means that the variable can hold only positive value.
%oIt is used to print the octal unsigned integer where octal integer value always starts with a 0 value.
%xIt is used to print the hexadecimal unsigned integer where the hexadecimal integer value always starts with a 0x value. In this, alphabetical characters are printed in small letters such as a, b, c, etc.
%XIt is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase such as A, B, C, etc.
%fIt is used for printing the decimal floating-point values. By default, it prints the 6 values after '.'.
%e/%EIt is used for scientific notation. It is also known as Mantissa or Exponent.
%gIt is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output.
%pIt is used to print the address in a hexadecimal form.
%cIt is used to print the unsigned character.
%sIt is used to print the strings.
%ldIt is used to print the long-signed integer value.

Let's understand the format specifiers in detail through an example.

  • %d

In the above code, we are printing the integer value of b and c by using the %d specifier.

Output

C Format Specifier
  • %u

In the above program, we are displaying the value of b and c by using an unsigned format specifier, i.e., %u. The value of b is positive, so %u specifier prints the exact value of b, but it does not print the value of c as c contains the negative value.

Output

C Format Specifier
  • %o

In the above code, we are displaying the octal value and integer value of a.

Output

C Format Specifier
  • %x and %X

In the above code, y contains the hexadecimal value 'A'. We display the hexadecimal value of y in two formats. We use %x and %X to print the hexadecimal value where %x displays the value in small letters, i.e., 'a' and %X displays the value in a capital letter, i.e., 'A'.

Output

C Format Specifier
  • %f

The above code prints the floating value of y.

Output

C Format Specifier
  • %e

Output

C Format Specifier
  • %E

Output

C Format Specifier
  • %g

In the above code, we are displaying the floating value of y by using %g specifier. The %g specifier displays the output same as the input with a same precision.

Output

C Format Specifier
  • %p

Output

C Format Specifier
  • %c

Output

C Format Specifier
  • %s

Output

C Format Specifier

Minimum Field Width Specifier

Suppose we want to display an output that occupies a minimum number of spaces on the screen. You can achieve this by displaying an integer number after the percent sign of the format specifier.

In the above program, %8d specifier displays the value after 8 spaces while %-8d specifier will make a value left-aligned.

Output

C Format Specifier

Now we will see how to fill the empty spaces. It is shown in the below code:

In the above program, %08d means that the empty space is filled with zeroes.

Output

C Format Specifier

Specifying Precision

We can specify the precision by using '.' (Dot) operator which is followed by integer and format specifier.

Output

C Format Specifier