C printf()

Formatted Output and the printf function

One of the common task in every program is the printing of output. We use output to request input from a user and later display the status/result, computations etc. In C programming there are several functions for printing formatted output. Here we discuss the printf() function, which writes output to the computer monitor. To use the printf() function we must include the stdio library in the source code. To do this just place the following code at the beginning of your program.

#include <stdio.h>

To print a simple message in computer screen you might call printf() function as follows :

  1. #include <stdio.h>  
  2. main()  
  3. {  
  4. printf ("You are learning printf() function");  
  5. }  


You are learning printf() function

In the above examples, the cursor will remain at the end of the printed output. If you repeat the above code in the following way the second message would appear immediately after the first one.

  1. #include <stdio.h>  
  2. main()  
  3. {  
  4. printf("You are learning printf() function");  
  5. printf("You are learning printf() function");  
  6. }  


You are learning printf() function You are learning printf() function

If we want to print the second output in a new line we must need different way, which has discussed in following section.

Escape sequences in C

An escape sequence is a series of characters that represents a special character. It begins with a backslash character (\), which indicates that the character(s) that follow the backslash character should be treated in a special way. C uses escape sequences within a format string to print certain special characters. For example \n moves the output position to the beginning of the next line. The following is a list of escape sequences.

Escape sequence Action
\n prints a new line
\b backs up one character
\t moves the output position to the next tab stop
\\ prints a backslash
\" prints a double quote
\' prints a single quote

You will get an idea of using the above escape sequences from the following example.

  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. printf("Create a new line\n");  
  5. printf("Print a double quotes (\") within a string\n");  
  6. printf("Print a single quotes (\') within a string\n");  
  7. printf("Print a Backslash\\ within a string\n");  
  8. printf("Using Backspace\b within a string\n");  
  9. printf("Using\tTab within a string\n");  
  10. }  

Output :

Create a new line
Print a double quotes (") within a string
Print a single quotes (') within a string
Print a Backslash\ within a string
Using Backspace within a string
Using     Tab within a string

Using printf() to print values

In the above section we have discussed how to print a new line, single quote, double quote etc. In this section we will discuss how to print the value of a variable. There are various format available to print different type values. Let start with these codes :

int x;
x = 150;
printf("Number of students in Class V is %d",x);

In the printf() function the format parameter '%d' is replaced with the value of the parameter x. Therefore the output looks like : Number of students in Class V is 150. In addition to %d, there are quite a number of format specifiers, each having different meaning. Here are the basic ones (several others exist) :

Parameter Meaning
%d Print an integer number printed in decimal (preceded by a minus sign if the number is negative).
%f Print a floating point number ( in the form dddd.dddddd).
%E Print a floating point number ( in scientific notation: d.dddEddd).
%g Print a floating point number (either as f or E, depending on value and precision).
%x Print an integer number in hexadecimal with lower case letters.
%X Print an integer number printed in hexadecimal with upper case letters.
%c Print a character.
%s Print a string.

The following code prints two numbers one is positive another is negative.

  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. int x,y;  
  5. x = 5;  
  6. y= -5;  
  7. printf("The value of x is %d and value of y is %d",x,y);  
  8. }  


The value of x is 5 and value of y is -5

You can do some calculation within printf. See the following example :

  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. int x;  
  5. x = 5;  
  6. printf ("%d + %d = %d\n",x,y,x+y);   
  7. printf ("%d - %d = %d\n",x,y,x-y);   
  8. printf ("%d x %d = %d\n",x,y,x*y);    
  9. printf ("%d / %d = %d\n",x,y,x/y);    
  10. }  


5 + 5 = 10
5 - 5 = 0
5 x 5 = 25
5 / 5 = 1

The following code uses the other parameters of printf mentioned in the parameter list :

  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. char a;  
  5. float x,y;  
  6. a = 'G';  
  7. x = 5.23;  
  8. y = 76000000.00;  
  9. printf("%c %f %g %E %s\n", a, x, y, y,"String");   
  10. printf("Hexadecimal(lower case) of 12 is %x\n",12);  
  11. printf("Hexadecimal(upper case) of 12 is %X\n",12);  
  12. }  


G 5.230000 7.6e+007 7.600000E+007 String
Hexadecimal(lower case) of 12 is c
Hexadecimal(upper case) of 12 is C

Using format specifier options in printf

You can specify the width and precision of numbers and strings as they are inserted. See the syntax :


For example %4c means the entire field (three blanks and one non-blank character) occupies 4 columns, %7.2f means to print a float or double in a field at least 7 spaces wide, with two places to the right of the decimal.