Display output in C – printf() function

Preview

Display output in C- printf() function

To Display output in C, we need to use printf() function. It is also known as formatted output function. Formatted output means the output which is arranged in a particular format.

The syntax of printf function() is:

printf(“Control string”, arg1,arg2……);

Control string specifies the format in which data is to be entered. Control string consists of following things:

  • Characters which should be printed as it is on the screen.
  • Conversion character % followed by by the format specifier which indicates the data type of values which should be displayed on the screen.
  • Escape sequences such as \n, \t, \b etc

Control string also indicates how many arguments should follow and what should be their data types.

arg1,arg2,….. are known as arguments and they refer to the constants, expression or variables whose values are to be displayed. There must be a comma between control string and the argument list.

There are different format specifiers for displaying different types of data. They are as follows:

1. Display int type data:

Data type: int            

Format Specifier: %wd                                                                                

Description                

% is the conversion character.

w is an integer value which specifies the minimum field width of numeric value to be displayed.

If the value to be displayed is larger than the field width, the number will be completely printed.

If value contains lesser number of digits than the field width, the value will be displayed right justified and blank spaces will appear before the value.

We can make a value left justified by prefixing a minus sign before field width.

If we place 0 before field width, zeroes will appear before the value at the place of blank spaces.

d is the data type specifier which specifies that an int  type value is to be displayed. 

Example:

int a=10;

printf(“%d”,a);

printf(“%d”,125); 


Program to display int type variable with different formattings

Output

(Line Wise)

#include<stdio.h>
int main()
{
int a=1256;
printf(“%d”,a);1256   /*Value is fully shown*/
printf(“%7d”,a);    1256  /*3 spaces in the beginning*/
printf(“%07d”,a);0001256  /*3 zeros in the beginning*/
printf(“%-7d”,a);1256    /*3 spaces at the end*/
printf(“%2d”,a);1256   /*Value is fully shown*/
return(0);
}

 

2. Display short type data:

Data type: short

Format Specifier: %whd                                                                                

Description                

% is the conversion character.

w is an integer value which specifies the minimum field width of numeric value to be displayed.

hd is the data type specifier which specifies that a short  type value is to be displayed. 

Example:

int a=10;
printf(“%hd”,a);
printf(“%hd”,25); 

Program to display short type variable.

Output

(Line Wise)

#include<stdio.h>
int main()
{

int a=1256;
 
printf(“%hd”,a);1256   /*Value is fully shown*/
printf(“%7hd”,a);    1256 /*3 spaces in the beginning*/
printf(“%07hd”,a);0001256  /*3 zeros in the beginning*/
printf(“%-7hd”,a);1256    /*3 spaces at the end*/
printf(“%2hd”,a);1256   /*Value is fully shown*/
return(0);
}
 



3. Display long type data:

Data type: long

Format Specifier: %wld                                                                                

Description                

% is the conversion character.

w is an integer value which specifies the minimum field width of numeric value to be displayed.

ld is the data type specifier which specifies that a long type value is to be displayed. 

Example:

int a=10;

printf(“%ld”,a);

printf(“%ld”,125l);   /*l can be suffixed with number for long value*/

Program to display long type variable.

Output

(Line Wise)

#include<stdio.h>
int main()
{

int a=1256
 
printf(“%d”,a);1256   /*Value is fully shown*/
printf(“%7d”,a);    1256 /*3 spaces in the beginning*/
printf(“%07d”,a);0001256  /*3 zeros in the beginning*/
printf(“%-7d”,a);1256    /*3 spaces at the end*/
printf(“%2d”,a);1256   /*Value is fully shown*/
return(0);
}
 

 

4. Displaying float/double type data

Data type : float

Format Specifier: %W.pf or %W.pe                              

Description            

% is the conversion character.

W is known as width, It is an integer value which specifies the minimum number of positions that are to be used for displaying value.

By default, real values are right justified. We can make them left justified by prefixing the field width with a minus sign.

P is known as precision. It is an integer value which specifies the number of digits which should be displayed after decimal point.

f is the data type specifier that specifies that a float type value is to be displayed. Precision is of six digits.

e is the data type specifier that specifies that a real type value is to be displayed in exponent form.

 Example

float a=45.456445;

printf(“%f”,a);

 

Program to display float/double type variable

Output

(Line Wise)

#include<stdio.h>
int main()
{

float n=45.456445;
Empty Boxes represent
Blank Spaces
printf(“%f”,n);
printf(“%9.3f”,n);
printf(“%-9.2f”,n);
printf(“%9.2f”,n);
printf(“%e”,n);
return(0);
}
 

 

 5. Displaying long double type data

Data type : long double

Format Specifier: %W.pLf or %W.pe                              

Description            

% is the conversion character.

W is known as width, It is an integer value which specifies the minimum number of positions that are to be used for displaying value.

By default, real values are right justified. We can make them left justified by prefixing the field width with a minus sign.

P is known as precision. It is an integer value which specifies the number of digits which should be displayed after decimal point.

Lf is the data type specifier that specifies that a long double type value is to be displayed.

e is the data type specifier that specifies that a real type value is to be displayed in exponent form.

 Example

float a=45.456445;

printf(“%lf”,a);

 

Program to display long double type variable

Output

(Line Wise)

#include<stdio.h>
int main()
{

float n=45.456445;
Empty Boxes represent
Blank Spaces
printf(“%Lf”,n);
printf(“%9.3Lf”,n);
printf(“%-9.2Lf”,n);
printf(“%9.2Lf”,n);
printf(“%e”,n);
return(0);
}
 

 


6. Displaying char type data

Data type: char

Format Specifier: %Wc

Description:

% is the conversion character.

W is known as width, It is an integer value which specifies the minimum number of positions that are to be used for displaying a particular character value. By default, character values are right justified. We can make them left justified by prefixing the field width with a minus sign.

c is the data type specifier that specifies that a  char type value is to be displayed.      

Example

char choice=’Y’;
printf(“%c”,choice);

 

Program to display char type variable

Output

(Line Wise)

#include<stdio.h>
int main()
{

char choice=’Y’;
 
printf(“%c”,choice);
printf(“%9c”,choice);
printf(“%-9c”,choice);
return(0);
}
 

 

7. Displaying string type data

Data type: String

Format Specifier: %W.Ps                                                                     

Description:

% is the conversion character.

W is known as width, It is an integer value which specifies the minimum number of positions that are to be used for displaying a particular character value. By default, string values are right justified. We can make them left justified by prefixing the field width with a minus sign.

P is known as precision, It is an integer value which specifies that how many characters from the string should be displayed

s is the data type specifier that specifies that a  string type value is to be displayed.   

Example

char name[]=”Lovejot”;

printf(“%s”,name);

 

Program to display string type variable

Output

#include<stdio.h>
int main()
{

char name[ ]=”Lovejot”;
printf(“%s”,name);  
printf(“%9s”,choice); 
printf(“%-9s”,choice);
printf(“%9.5s”,choce);  
return(0);
}

 

 

Tips and tricks

  • %d format specifier is used to print the int value.
  • %hd format specifier is used to print the short value.
  • %ld format specifier is used to print the Hexadecimal value.
  • %f format specifier is used to print the float type value.
  • %Lf format specifier is used to print the long double type value.
  • %c format specifier is used to print the char type value.
  • %s format specifier is used to print the string type value.
  • %x format specifier is used to print the Hexadecimal value.
  • %u format specifier is used to print the unsigned int value.
  • %p format specifier is used to print the address of a variable using & operator.
  • & sign in scanf() function denotes the address of the variable where value is to be stored.
  • While reading a string variable using scanf(), we don’t specify & sign because the name of string variable itself specifies the address of very first element of string variable.
  • printf() function is formatted output function defined in header file stdio.h to display something on the screen.
  • scanf() function is formatted input function defined in header file stdio.h read value of some variable from the standard input device keyboard.
  • In scanf() function, while reading more than one variable in single statement, there should be no space in between to format specifiers. Example: In scanf(“%d%d”,&a,&b); there should be no space between %d and %d defined within pair of double quotes.
  • Keyboard is defined to be standard input device in C. It is denoted by stdin.
  • Monitor is defined to be standard output device in C. It is denoted by stdout.

 

Best Books of C

Lesson tags: conversion character for int, display output in c, format specifier for data type, printf() function of c, show output in c, use printf()
Back to: C Programming Language