Dremendo Tag Line

printf() Function and its Use in C Program

Input / Output in C

In this lesson, we will understand what printf() function is and how it works in C programming with the help of some examples.

What is printf() Function

The printf() function is used in the C program to print text as well as the value of the variables on the screen.

Let's see some examples for more understanding.

Example 1

C program to print Hello World text on the screen.

#include <stdio.h>
#include <conio.h>

int main()
{
    printf("Hello World");
    return 0;
}

Output

Hello World

Here you can see that we have printed the text Hello World on the screen using printf() function. So whatever you write inside the brackets ( ) of printf function it will be printed on the screen.

Example 2

C program to print Sunday and Monday on separate lines on the screen.

#include <stdio.h>
#include <conio.h>

int main()
{
    printf("Sunday\n");
    printf("Monday");
    return 0;
}

Output

Sunday
Monday

You can see that we have used \n (escape sequence character for new line) to break the line so that Monday can be printed on the next line. You can use more than one \n if you want to break more lines.

Example 3

C program to print integer data type values on the screen.

#include <stdio.h>
#include <conio.h>

int main()
{
    int a=5, b=26;
    printf("a=%d b=%d\n\n",a,b);
    printf("a=%d\nb=%d",a,b);
    return 0;
}

Output

a=5 b=26

a=5
b=26

In the above program, we have printed two integer data type values in two different ways. First, in the same line with a space between them and second on separate lines.

To print integer data type value, we have used %d (format specifier of int) inside printf() function and variable names (a, b) are used outside of double quotes to print their values in place of %d. The \n is used to break the lines.

Example 4

C program to print float data type values on the screen.

#include <stdio.h>
#include <conio.h>

int main()
{
    float a=5.23, b=26.14;
    printf("a=%f b=%f\n\n",a,b);
    printf("a=%.2f\nb=%.2f",a,b);
    return 0;
}

Output

a=5.230000 b=26.139999

a=5.23
b=26.14

Just like the previous example this time, we have printed two float data type values on the screen. The only difference is that this time we have used %f (format specifier of float) to print the float values of the variables (a,b) on the screen.

Please note that we have used the %.2f to round up the number up to 2 decimal places.

Example 5

C program to print double data type values on the screen.

#include <stdio.h>
#include <conio.h>

int main()
{
    double a=18.413, b=57.273;
    printf("a=%lf b=%lf\n\n",a,b);
    printf("a=%.2lf\nb=%.2lf",a,b);
    return 0;
}

Output

a=18.413000 b=57.273000

a=18.41
b=57.27

Just like the previous example this time, we have printed two double data type values on the screen. The only difference is that this time we have used %lf (format specifier of double) to print the double data type values of the variables (a,b) on the screen.

Please note that we have used the %.2lf to round up the number up to 2 decimal places.

Example 6

C program to print a char data type value on the screen.

#include <stdio.h>
#include <conio.h>

int main()
{
    char a='#';
    printf("%c",a);
    return 0;
}

Output

#

This time we have used %c (format specifier of char) to print the char data type value of the variable a on the screen.

Escape Sequence Character in C

An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence.

Escape Sequence Character Description
\n Insert a linefeed (new line)
\r Insert a carriage return
\f Insert a form feed
\b Insert a backspace
\t Insert a tab
\\ Insert a backslash
\" Insert a double quote
\' Insert a single quote
video-poster

Test Your Knowledge


You Scored :

Percentage :


Loading