4.2 – Formatting output using printf()

by subbu on September 11, 2013

Exploring printf()

C language has no native statement to present the formatted output on to the monitor. printf() is a function defined in stdio.h, generally used to print the text, constants and values of variables onto the monitor in a required format.
It has the format string to design the output and the list of variables whose values need to be printed.
The format string has text which will be printed as it is, format specifiers to represent the values of variables and escape sequences to control the output format.

Formatting integers:

integer format spacifier

  • Here ‘w’ is the field width in which the number must be printed.
  • If ‘w’ is the positive number then the number will be right aligned.
  • If ‘w’ is the negative number then the number will be left aligned.
  • If we specify 0 before w then leading 0s are added.
  • If flag # is used with %x or %o then leading base 0 is printed for octal number and 0x is printed for hexadecimal numbers
  • If the symbol “+” is used then the positive number is printed with the sign +
#include<stdio.h>
int main()
{
int x=2453;
printf("%10d",x);/*right aligned in field width 10*/
printf("\n%8d",x);/*right aligned in field width 8*/
printf("\n%08d",x);/*right aligned in field width 8 with leading 0's*/
printf("\n%-10d",x);/*left aligned in field width 10 */
printf("\n%#8x",x);/*hexadecimal equal with base 0x in field width 8*/
printf("\n%#8o",x); /*Octal equal with base 0 in field width 8*/
printf("\n%+8d",x);/*with symbol + in field width 8*/
return 0;
}

Output:
integer formatting output

Formatting floating point numbers:

floating point format specifier

  • Here ‘w’ specifies the filed width in which the number must printed.
  • ‘p’ is the number of decimal places to which the number must rounded to.
  • If ‘w’ is the positive number then the number will right aligned.
  • If ‘w’ is the negative number then the number will left aligned.
  • A flag * can be used to supply the field width and the number of decimal places from outside
  • If we use “g” or “G” instead of “f” then it will control the number of significant figures displayed. It will not only impact the value after decimal point but impacts whole number.
#include<stdio.h>
int main()
{
float x=75.256785;
printf("%10.3f",x); /* printing up to 3 decimal places in field width 10 */
printf("\n%8.2f",x);/* printing up to 2 decimal places in field width 8 */
printf("\n%08.3f",x);/* with leading zeros*/
printf("\n%-10.2f",x);/* left align */
printf("\n%+10.2f",x); /*leading + symbol and right aligned*/
printf("\n%*.*f",8,2,x); /* first * is replaced with 8, second * is replaced with 2*/
return 0;
}

Output:
formatting floating point output

Quiz:

1) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=12;
printf("%#x",a);
printf("\n%#o",a);
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
float a=75.656785;
printf("%.3f",a);
printf("\n%.0f",a);
printf("\n%6.2f",a);
printf("\n%6.2f",345.98756);
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
float a=100.4567;
float b=3.6758;
printf("%7.3g",a);
printf("\n%.3g",a);
printf("\n%.3g",b);
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
double d=(double)'a';/*Assigns 97 */
printf("%.01lf",d);
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
float f=24.25;
int i=(int)f;
printf("%.4f",f);
printf("\n%.2f",i);
printf("\n%d",i);
return 0;
}
Show Answers
1)
0xc                  //Hexadecimal equivalent
014                 //Octal equivalent

2)
75.657             //Rounding to 3 decimal places
76                   // Rounding to 0 decimal places
75.66              //Rounding to 2 decimal places in 6 field width
345.99            // Rounding to 2 decimal places in 6 field width

3)
100             //Displays 3 significant digits in field width 7
100                 // Displays 3 significant digits
3.68                // Displays 3 significant digits

4)
97.0               // .01 is same as .1, displays single decimal place

5)
Output in MinGW gcc
24.2500
24.25              //printing decimal places even by printing an int variable with %f    24

Previous post:

Next post: