Broad Network


The printf Function in the C Computer Language

Part 41 of the Complete C Course

Foreword: The printf() Function in the C Computer Language

By: Chrysanthus Date Published: 22 Jun 2024

Introduction

The predefined printf() function in the stdio library, allows the programmer to send a string output to the terminal (monitor). Within this string, there can be special short text specifiers at different positions. The synopsis for the printf() function is:

    #include <stdio.h>
    int printf(const char * restrict format, ...);

The first argument of the printf() function is the output string. The second, third and the rest of the arguments, are variables for text values to be inserted into the string. The second argument corresponds to the first specifier; the third argument corresponds to the second specifier; the fourth argument corresponds to the third specifier; and so on. The second argument and the rest of the arguments ahead, are optional.

A specifier begins with the % character, followed by one or more flags, without spaces. Note: in order to use the printf() predefined function call, the stdio.h header file has to be included.

Characters and Strings
To output a string without specifiers, do something like;

            printf("I have seen it.\n");

Notice that there is no second, third, fourth argument, etc. in this case. And so there are no corresponding specifiers within the string, "I have seen it." . The '\n' inserted just after the full stop, is a control character. It sends the cursor at the output to the next line. In its absence, the command prompt will appear at the right of the string, "I have seen it.", at the output; and not below.

Character
To have a character within the string, do something like,

            char ch = 'A';
            printf("The letter, %c comes first.\n", ch);

The flag for character is, c. The argument, ch corresponds to %c. The output is, "The letter, A comes first." , without the quotes.

String
To include a sub-string into the output string, do something like:

            char *const str = "John";
            printf("The boy, %s is here.\n", str);

The output is, "The boy, John is here." . The flag for string is, s. The second argument is str, which corresponds to %s.

Arithmetic Types
Arithmetic types are integer and floating types.

Integer
The following code segment will output two integers, within the output string:

            int var1 = 25, var2 = 30;
            printf("The first variable is %i and the second variable is %i. Thanks.\n", var1, var2);

The output is, "The first variable is 25 and the second variable is 30. Thanks." . The second argument is var1 and the third argument is var2. The first argument to the printf() function call, is the string. The first %i corresponds to var1, and the second %i corresponds to var2. The flag for integer is, i.

Float
Consider the following code segment:

            float var = 2.5381;
            printf("The answer, %f is good.\n", var);

The output is, "The answer, 2.538100 is good." . The flag for float is f. Notice that two extra zeros have been added, to the float number. To limit the number of decimal digits to 1, the specification has to be, %.1f. To limit the number of decimal digits to 2, the specification has to be, %.2f, To limit it to 3 digits, the specification has to be %.3f; and so on. There is rounding with this limitation. The following code segment rounds the float number to 2 decimal digits:

            float var = 2.5381;
            printf("The answer, %.2f is good.\n", var);

The output is, "The answer, 2.54 is good.", with 2.5381 rounded to two decimal places, in order to have 2.54.

Double
The flags for a double number is lf, for "Long Float". Consider the following code segment:

            double var = 2.5341;
            printf("The answer, %lf is good.\n", var);

The output is, "The answer, 2.534100 is good.", with two extra zeros added to the double number. The following code segment rounds the double number to 2 decimal digits:

            double var = 2.5341;
            printf("The answer, %.2lf is good.\n", var);

The output is, "The answer, 2.53 is good.", with 2.5341 rounded to two decimal places, to have 2.53.

The % as Character
Since % is a special character in the first argument string, in order to represent, % as an ordinary character in the string, do %%. Consider the code segment,

            int var = 80;
            printf("I scored %i%% in the test.\n", var);

The output is, "I scored 80% in the test." .



Related Links

More Related Links

Cousins

BACK NEXT

Comments