Display output in C – printf() function

Preview

Display output in C- printf() function

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 data:

Format Specifier: %d                                                                                

Display int values.

Output

#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  /*0 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:

Format Specifier: %hd                                                                                

Display short type data.

Output

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

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



3. Print long type data:

Format Specifier: %ld                                                                                


Display long type data.

Output

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

long int a=4258
 
printf(“%ld”,a);4258   /*Value is fully shown*/
printf(“%6lhd”,a);   4258  /*2 spaces in the beginning*/
printf(“%06lhd”,a);004258  /*0 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

Format Specifier: %f

Display float data

Output

#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*/
printf(“%e”,n);1.515641E+01; /*Value in exponent form*/
return(0);
}
 

 

 5. Print long double data

Format Specifier: %Lf or %e                              

Display long double data

Output

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

long double n=15.156415;
printf(“%lf”,n);15.156415;
printf(“%12.3lf”,n);    15.156415 /*3 spaces at beginning*/
printf(“%-7.2lf”,n);15.16 /*2 digits after decimal point along with 2 spaces at the end*/
printf(“%7.2lf”,n);   15.16 /*2 digits after decimal point along with 2 spaces at the beginning*/
printf(“%e”,n);1.515641E+01; /*Value in exponent form*/
return(0);
}
 

 


6. Print char data

Format Specifier: %c

Display char type dataOutput
#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

Format Specifier: %s                                                                     

Display string data

Output

#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);
}

 

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