Display output in C – printf() function

Display output in C- printf() function in C Language

printf() function is used to display output in C. Syntax of printf function() is:

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

Control string specifies the format to enter the data.

arg1,arg2,….. are constants, expression or variables whose values should be displayed.



1. print int type data in C Language

Format Specifier: %wd

% is the conversion character.

w is an integer value which specifies the 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 the value to be displayed contains lesser number of digits than the field width, the value will be displayed right justified and blank spaces will appear before the value.

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

                                                                                

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

 


2. print short type data in C Language

Format Specifier: %whd          

% is the conversion character.

w is an integer value which specifies the 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 the value to be displayed contains lesser number of digits than the field width, the value will be displayed right justified and blank spaces will appear before the value.

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

Display short type data.
#include<stdio.h>
int main()
{

int a=4258;
printf(“%d”,a);       /*4258 (Value is fully shown) */
printf(“%6d”,a);    /*  4258 (2 spaces in the beginning)*/
printf(“%06d”,a);  /* 004258  /*zeros in the beginning*/
printf(“%-6d”,a);   /*4258   2 spaces at the end*/
printf(“%3d”,a);   /* 4258   Value is fully shown*/
return(0);
}



3. Print long type data in C Language

Format Specifier: %wld

% is the conversion character.

w is an integer value which specifies the 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 the value to be displayed contains lesser number of digits than the field width, the value will be displayed right justified and blank spaces will appear before the value.

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

Display long type data.
#include<stdio.h>
int main()
{

long int a=4258
printf(“%ld”,a);       /*4258 (Value is fully shown) */
printf(“%6ld”,a);    /*  4258 (2 spaces in the beginning)*/
printf(“%06ld”,a);  /* 004258  /*zeros in the beginning*/
printf(“%-6ld”,a);   /*4258   2 spaces at the end*/
printf(“%3ld”,a);   /* 4258   Value is fully shown*/
return(0);
}

 

4. Print float data in C Language

Format Specifier: %W.Pf

% 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 value.

P is called precision. It specifies the number of digits to be displayed after the decimal point.

f is the data type specifier that specifies that a float type value is to be displayed.

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

float n=15.156415;
printf(“%f”,n);          /*15.156415*/
printf(“%12.3f”,n);  /*15.156415 (3 spaces at beginning)*/
printf(“%-7.2f”,n);  /*15.16 (2 digits after decimal point along with 2 spaces at the end)*/
printf(“%7.2f”,n);    /*15.16 (2 digits after decimal point along with 2 spaces at the beginning)*/
return(0);
}

 

 5. Print double/long double type  data in C Language

Format Specifier:%W.Plf

% 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 value.

P is called precision. It specifies the number of digits to be displayed after the decimal point.

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

Display double type data
#include<stdio.h>
int main()
{

double n=15.156415;
printf(“%f”,n);          /*15.156415*/
printf(“%12.3f”,n);  /*15.156415 (3 spaces at beginning)*/
printf(“%-7.2f”,n);  /*15.16 (2 digits after decimal point along with 2 spaces at the end)*/
printf(“%7.2f”,n);    /*15.16 (2 digits after decimal point along with 2 spaces at the beginning)*/
return(0);
}

 


6. Print char data in C Language

Format Specifier: %Wc

% 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.

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

Display char type data
#include<stdio.h>
int main()
{

char gender=’M’;
printf(“%c”,gender);     /*M*/
printf(“%4c”,gender);  /* M (3 spaces before M)*/
printf(“%-4c”,gender);  /*M   (3 spaces after M)*/
return(0);
}

 7. Print string data in C Language

Format Specifier:%W.Ps

% 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.

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.     

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

char Bname[ ]=”CLanguage”;
printf(“%s”,Bname);  /*CLanguage*/
printf(“%11s”,Bname); /*CLanguage (2 spaces in beginning)*/
printf(“%-11s”,Bname); /*CLanguage (2 spaces at end)*/
printf(“%5.3s”,Bname);  /*CLa (Total 5 spaces with first three characters shown)*/
return(0);
}

 

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