printf() Library Function with Examples

printf() is formatted output function which is used to display some information on standard output unit. It is defined in standard header file stdio.h. So, to use printf(), we must include header file stdio.h in our program.

printf() Syntax

Syntax for printf() is :


printf("String");

	or
	
printf(“format_string”, var1, var2, var3, …, varN);

Where format_string may contain :

  • Characters that are simply printed as they are.
  • Format specifier that begin with a % sign.
  • Escape sequences that begin with \ sign.

The format string indicates how many arguments follow and what their types are. The arguments var1, var2, …, varN are the variables whose values are formatted and printed according to format specifications of the format string. The arguments must match in number, order and type with the format specifications.

printf() Syntax Explanation

When we want to display some text or string to standard output unit then we can use printf() as printf("Welcome to the world of C programming");. This statement simply prints Welcome to the world of C programming to the standard output unit.

Example #1 : printf() Simple Example


#include<stdio.h>
void main()
{
  printf (“Hello, world!”);
}

Output of the above program :

	
Hello, World!

Here, header file stdio.h is included because the prorotype of function printf() is defined in this library. So to use printf() in our program we must include stdio.h using #include directive. Once stdio.h is included then we can use all the function that are defined in this header file.

While programming, we often come to situation in which we need to print value of variable. In those situation we use format specifier that starts with % sign. Think of format specifier as a placeholder for some variable.

Example #2 : Format Specifier ( % ) in printf()


#include<stdio.h>

void main()
{
	int a;
	float b;
	a=2;
	b=4.5;
	printf("Value of a = %d and b = %f.", a, b);
}

Output of the above program :

	
Value of a = 2 and b = 4.500000.

In the above program, we first declared variable a as integer using int and b as floating or fractional number using float. Then we assigned value 2 to variable a and value 4.5 to variable b. After that in printf() statement everything is printed as it is but in place of %d value of a is assigned since %d is placeholder for integer and in place of %f value of b is assigned since %f is placeholder for floating number. And by default when using %f, six digit after decimal point is printed. We can control this behaviour by using %0.2f, this tells compiler to print only two digit after decimal point.

Similarly, while using printf(), we often see use of \t and \n frequently. So what are these actually? In C, anything that begins with \ are known as Escape Sequences. When esacape sequences are encountered within double quotation of printf() then they are not printed as it is because they have special meaning and compiler interprets their meaning. For example \t is used for printing TAB and \n is used for printing NEWLINE.

For more details about esacape sequences read this (Escape sequences) article. Following program illustrates this concept.

Example #3 : Escape Sequences ( \ ) in printf()


#include<stdio.h>

void main()
{
	printf("Hello, World!");
	printf("\nNow control is on new line and this \t prints tab.");
}

Output of the above program :

	
Hello, World!
Now control is on new line and this 		prints tab.

Notice the gap between this and prints in above output.