printf & scanf Function in C

So far, we have learned some basic of C program. Let’s drive into some details. We have learned how to print Hello World on the display using printf() function. In this lesson, we will learn what is actually and what does mean by printf and its working terminology.

In this lesson, we will also introduce another function called scanf which is also repeatedly used while writing code in programming along with the printf function. Let’s study them respectively.

The printf function

printf is a built-in library function that is available everywhere in your c programming source code. Here the default library of C programming is the <stdio.h>. In the Hello world program in the previous lesson, the first line we wrote #include <stdio.h>, by writing it, we just told the C compiler to include the default library in the source file.

So What is the C library?
In C programming, there are some module files you will find. Which are actually made for creating high-level features or you can name every feature as a function. It is much like frameworks. For example, “stdio.h” is the default library of C and it includes the printf function. If we want to display something, then we must include the “stdio.h” module in your source file. There are many other modules/library in C like “math.h”. We will learn more about the C standard library in further lessons.

C standard and non-standard libraries also called header files. In short, because the library modules are generally added at the top of the source file. And remember, to include the header file or C library, you must use the following syntax:

#include <header_file/c_library>
eg:
#include <stdio.h> //standard library
#include <math.h> // Mathematical library

Let’s see an example of printf that will cover the complete functionality of printf in C program;

Before Example, things to know:

  • The printf function is used to display specific kinds of data to your computer monitor or where the code is implemented (eg: Smartphones, embedded devices, IoT devices)
  • The printf function can output five different types of data that we will see in the example below.
  • C is a case sensitive language. So that, printf and Printf is not the same. For printf always use lowercase letter.
  • To generate a new line we will use escape sequence “\n”. We will learn more about scape sequence.

Example:

#include <stdio.h>

int main() {
    // Declaring variable
    char a = 'A';
    char b[120] = "Hello, PrograCoding!";
    int c = 15;
    float d = 24.274;
    double e = 276.3753783;

    // Printing variables using printf() function
    printf("Character is: %c \n", a);
    printf("String is: %s\n", b);
    printf("Integer is: %d \n", c);
    printf("Floating num is: %f \n", d);
    printf("Double num: %lf\n", e);

    return 0;
}

To print any direct text or value of a variable, follow the above example. Note that you can use multiple placeholders in a single string. But you have to also use the corresponding variable name for the place holder. Let’s see an example. The above code also can be written as follows:

Example: V2

#include <stdio.h>

int main() {
    // Declaring variable
    char a = 'A';
    char b[120] = "Hello, PrograCoding!";
    int c = 15;
    float d = 24.274;
    double e = 276.3753783;

    // Printing variables using printf() function
    printf(" Character is: %c \n String is: %s\n Integer is: %d \n Floating num is: %f \n Double num: %lf\n", a, b, c, d, e);

    return 0;
}

Output:

c printf function

For both snippets of code, output will be the same. You can implement your code with both procedures. Try to compile the above code with some changes randomly. You will fully understand the terminology of printf function.

In the above example, we have used some different placeholder like “%c, %s, %d” etc. What does actually mean by them? Okay, let me say. printf can display file different kinds of data to the display. And for the five individual data types, printf uses five different placeholders, that already we saw in the above example:

  • %c” used for the character data type. To print out a single character the “char” variable is responsible for “%c” placeholder in printf function.
  • %s” used for the string character data type. To print out multiple characters the “char[num of ch] data type responsible for “%s” placeholder.
  • %d” used for the integer data type. To print out integer number the “int” data type is responsible for “%d” placeholder.
  • %f” used for the floating-point number data type. To print out float number the “float” data type is responsible for “%f” placeholder.
  • %lf” used for the double data type. To print out double number the “double” data type is responsible for “%lf” placeholder.

The scanf function

We saw, printf function used for printing out the result to the monitor. But the scanf() function used for taking input from the user’s keyboard and the put it in a variable. It is the only basic difference between printf and scanf function.

Like the printf function, scanf function is also a built-in function of standard default library or “stdio.h”. That means, if you want to use the scanf function then you must include the default “stdio.h” library at the top of your source code.

Let’s see an example program of scanf, that will take input from the user’s keyboard and the printf those input to the display:

Before example, things to know:

  • scanf used to take input from the user’s keyboard
  • Like printf, scanf can take five different kinds of data from the users
  • scanf is also case sensitive. always use the lowercase letter for scanf function.

Example:

#include <stdio.h>

int main() {
    // Declaring variable
    char a;
    char b[120];
    int c;
    float d;
    double e;

    //taking input from keyboard
    printf("Enter Single character, String, Integer, floating num, double num respectively\n");
    scanf("%c %s %d %f %lf", &a, &b, &c, &d, &e);

    // Printing variables using printf() function
    printf("\n\nCharacter is: %c \n", a);
    printf("String is: %s\n", b);
    printf("Integer is: %d \n", c);
    printf("Floating num is: %f \n", d);
    printf("Double num: %lf\n", e);

    return 0;
}

Output:

c scanf function

Notice that the scanf and printf function implementation methods are nearly the same. In the above example, notice that we have used just “&” character before every variable. But in the printf function, we did not use that. It is actually the basic difference of printf and scanf function.  The uses of placeholders are the same as printf.