UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

8.3.4.  The printf function

The printf function (the name comes from print formatted) prints a string on the screen using a format string that includes the instructions to mix several strings and produce the final string to be printed on the screen. Languages such as Java also include functions similar to this one (see printf method of class PrintStream).

printf is a special function because it receives a variable number of arguments. The first parameter is fixed and is the format string. It includes text to be printed literaly and marks to be replaced by the text obtained from the additional parameters. Thus, printf is invoked with as many parameters as marks are included in the format string, plus one (the format string itself). The following example shows how to print the value of variable counter.

printf("The value is %d\n", counter);

The symbol % denotes the beginning of a format mark. The mark %d is replaced by the value of variable counter and the resulting string is printed. The symbol \n represents a new line. By default, all output is right-justified, that is, is placed on the right edge of the field and, by default, the width of the field is the same width as the string length.

If the format string includes several marks, they are processed in the same order as they appear. The following figure shows and example in which the format string has three marks, %s, %d, %5.2f, that are processed using respectively the string red, the integer 1234567 and the real number 3.14.

No check is performed to verify that the number of marks in the format string and the number of remaining parameters are consistent. In case of an error, the behavior of printf is undetermined.

The marks in the format string must have the following structure (fields in brackets are optional):

%[parameter][flags][width][.precision][length]type

Thus, every mark starts by the % symbol and finishes with a type. Each of the names (parameter, flags, width, precision, length and type) represents a set of possible values that are explained next.

Parameter Description
n$ n is replaced by a number to change the order in which the arguments are processed. For example %3$d refers to the third argument independently of its place within the format string.
Flags Description
number Left-pad the output with spaces (or zeros, see next flag) up to the number value.
0 Use 0 instead of spaces for left-padding up to the value given by the previous flag. For example %03d prints a number left padded with zeros up to three digits.
+ Print the sign of a number
- Align the field to the left (by default it is right aligned)
# Alternative format. For real numbers, it inserts zeroes at the end and always print the comma. For numbers not in base 10, it adds a prefix denoting the base.
Width Description
number Size to left pad the field to print
* Identical to the previous case, but the number to be used is given as parameter right before the value. For example printf("%*d", 5, 10) prints number 10 left-padded up to five digits.
Precision Description
number Size of the decimal part for real numbers. Number of characters to print for strings.
* Identical to the previous case, but the number to be used is given as parameter right before the value. For example printf("%.*s", 3, "abcdef") prints abc.
Length Description
hh Convert a variable of type char to integer and print
h Convert a variable of type short to integer and print
l For integers, a variable of type long is expected.
ll For integers, a variable of type long long is expected.
L For floating point, a variable of type long double is expected.
z For integers, an argument is expected of type size_t.
Type Description
%c Prints the ASCII character passed as parameter
%d, %i Integer signed decimal conversion
%x, %X Unsigned hexadecimal conversion
%p Memory address (pointer)
%e, %E Floating point conversion with sign in scientific notation
%f, %F Signed floating point conversion, using decimal point
%g, %G Floating point conversion, using the notation that requires less space
%o Integer unsigned octal conversion
%u Integer unsigned decimal conversion
%s Text string (finished with '\0')
%% Prints the symbol %

Examples of format marks

The format marks included as part of the first parameter passed to printf offer many possibilities. It follows a description of some examples:

  • Specify the minimum field width: The C language allows you to add an integer between the percent sign (%) and the letter in a format specifier. This ensures that the output reaches the minimum width. For example, %10f ensures that the output is at least 10 character spaces wide. This is especially useful when printing out a column of values. For example, with the next code:

    int num = 12; 
    int num2 = 12345; 
    printf("%d\n",num2); 
    printf("%5d\n",num);

    the following output is printed:

    12345 
       12
  • Align output: By default, all output is right-justified when using the minimum field width. You can change this and force output to be left- justified. To do so, you need to prefix the minimum field specifier with the minus sign (-). For example, %-12d specifies the minimum field width as 12, and justifies the output from the left edge of the field.

  • Specify precision: You can put a period . and an integer right after the minimum field width specifier to have a precision specifier. You can use the precision specifier to determine the number of decimal places for floating-point numbers, or to specify the maximum field width (or length) for integers or strings.

Check with these questions if you understood this document

  1. We want to print the string char string[30]; with the printf function:

    • We use printf("%c\n",string[0]);..

    • We use printf("%s\n",string[0]);.

    • We use printf("%s\n",string);