What is the purpose of the printf statement


A2.1 The function printf

The function printf is used to output information on the screen. In addition to simple lines of text, all elementary data types of the C language can be output in formatted form.

The function printf expects one or more parameters. The first, mandatory parameter is a character string that formats the output. The following optional parameters contain the data to be displayed:

    printf (format string, ...);

The format string can be a constant or a variable. It contains normal text, with some placeholders for the data to be output. A placeholder is introduced with a percent sign '%' and ends with a letter that represents the data type.

Example: The variable v stands for the speed of a car and is of the type double. The speed is output with the following instruction:

    double v = 57.3; printf ("Speed:% f km / h \ n", v);

The placeholder for the variable v is behind the word speed: introduced with the percent sign. The letter f stands for the data type double.

The function printf can also be used with just one parameter for pure text output. The instruction

gives the text hello world off the screen.

In addition to the letters for the data type, the placeholder can contain information on the formatting of the date. In general, a placeholder has the following appearance:

    % [Flags] [width] [. Precision] [h | l | L] type

The individual parts of the placeholders are listed in the following tables.

    placeholderData typepresentation
    % d or% isigned intdecimal
    % uunsigned intdecimal
    %Ounsigned intoctal
    % xunsigned inthexadecimal (with small letters)
    % Xunsigned inthexadecimal (with capital letters)
    % fdoublealways without an exponent
    % edoublealways with exponent (indicated by 'e')
    % Edoublealways with exponent (indicated by 'E')
    %Gdoubleas required with exponent (indicated by 'e')
    %Gdoubleas required with exponent (indicated by 'E')
    % ccharsingle character
    % schar []String
    % pvoid *Memory address
    % nsigned int *writes the number of previous characters in the specified address
    %%-Output of the character '%'
    Table A2-1: Placeholders for the elementary data types.
    Modifierapplied toInterpretation / effect
    Hd, i, o, u, x, Xshort int
    nshort int *
    ld, i, o, u, x, Xlong int
    nlong int *
    e, E, f, g, Gdouble (only scanf)
    L.e, E, f, g, Glong double
    Table A2-2: Modifiers for the placeholders of the elementary data types.
    widtheffect
    nmin. n characters, preceded by spaces
    0nmin. n characters, preceded by zeros
    *the next argument from the list is the width
    Table A2-3: Information for the width of the output.
    precisionapplied to typeprecision
    .ne, E, fn decimal places
    g, Gmax. n decimal places
    .0e, E, fno decimal point
    <nix>e, E, f6 decimal places
    .* Precision from the next argument in the list
    Table A2-4: Information on the number of decimal places.
    Flagsapplied to typeimportance
    <Keins> right justified
    - left-justified with the following spaces
    + Always output sign (also +)
     only negative signs
    #Ofor values! = 0 a '0' is introduced
    x, Xfor values! = 0 '0x' or '0X' is put in front
    e, E, f, g, GAlways output decimal point
    Table A2-5: Some more information about the output format.

Here some examples:

    printf statementOutput result
    Table A2-6. Some examples of using the printf function.

Problems can arise if incorrect placeholders are used for the data to be displayed. Consider the following section of the program:

    double d = 5.2; int i = 3; printf ("% d% d \ n", d, i);

When calling the function printf all parameters are copied to memory in sequence. In our example it looks like this:


    Figure A2-1: Arrangement of the call parameters in the memory.

The placeholder % d is incorrectly referenced to the variable d of type double applied. That is, the variable d is here as int interpreted. The programming environment in which the program was tested is used for double 8 bytes and for int only 4 bytes. The command printf therefore takes from the variable d only the first four bytes and interprets them as int.

The next placeholder again stands for the type int. But since the previous placeholder contains the variable of type double has only read in half, the second placeholder now tries the second half of the variable d as int to interpret. Obviously, the result is nonsense. In the test environment it looked like this:

To cut a long story short: It is extremely important to ensure that the placeholders match the associated data types.