C - Formatting output

Formatting output is use to format and align the output. It can be number, float number or a string. Also we can limit the decimal value.

S.No. Format Description
%7d Create 7 blocks space in memory, and start displaying number from right align. Example
%-7d Same as above, but alignment will be from left. Example
%10f Create 10 blocks space in memory, including numbers before and after decimal. Display from right align. Example
%-10f Same as above, but alignment will be from left. Example
%9.2f Create total 10 blocks, only 2 decimal places will be displayed. Example
%20s Create 20 blocks space in memory, and start displaying string from right align. Example
%-20s Same as above, but alignment will be from left. Example
%20.5s Create 20 blocks, start displaying string from right align, limit only 5 characters.
%i To display integer value. Same as %d
%e or %E To display scientific notation of float value.
%o To display Octal value
%x or %X To display Hexadecimal value
%p To display the Memory Address
%% To display '%' character

Example 1.1: Program to show integer output in required format. 1

#include<stdio.h>
void main()
{
   int a=12, b=123, c=1234;
   printf("Without Formatting:\n");
   printf("%d\n", a);
   printf("%d\n", b);
   printf("%d\n", c);
   
   printf("\nWith Formatting:\n");
   printf("%7d\n", a);
   printf("%7d\n", b);
   printf("%7d\n", c);
}
Without Formatting:
12
123
1234

With Formatting:
.....12
....123
...1234

Example 1.2: Program to show integer output, Difference between %d and %-7d. 1

#include<stdio.h>
void main()
{
    int a=12, b=123, c=1234;
    printf("Without Formatting:\n");
    printf("%d %d\n", a, a);
    printf("%d %d\n", b, b);
    printf("%d %d\n", c, c);    
    printf("\nWith Formatting:\n");
    printf("%-7d %d\n", a, a);
    printf("%-7d %d\n", b, b);
    printf("%-7d %d\n", c, c);
}
Without Formatting:
12 12
123 123
1234 1234

With Formatting:
12 12
123 123
1234 1234

Example 2.1: Program to format floating data type in required format. 1

#include<stdio.h>
void main()
{
   float a=12, b=123.456, c=1234.56;
   printf("Without Formatting:\n");
   printf("A = %f\n", a);
   printf("B = %f\n", b);
   printf("C = %f\n", c);

   printf("\nUsing %%10f Formatting:\n");
   printf("A = %10f\n", a);
   printf("B = %10f\n", b);
   printf("C = %10f\n", c);

   printf("\nUsing %%10.2f Formatting:\n");
   printf("A = %10.2f\n", a);
   printf("B = %10.2f\n", b);
   printf("C = %10.2f\n", c);
}
Without Formatting:
A = 12.000000
B = 123.456001
C = 1234.560059

Using %10f Formatting:
A = 12.000000
B = 123.456001
C = 1234.560059

Using %10.2f Formatting:
A = 12.00
B = 123.46
C = 1234.56

Example 2.2: Program to format floating data type. Difference between %f and %-10f. 1

#include<stdio.h>
void main()
{
    float a=12, b=123.456, c=1234.56;
    printf("Without Formatting:\n");
    printf("A = %f %f\n", a, a);
    printf("B = %f %f\n", b, b);
    printf("C = %f %f\n", c, c); 
	  
    printf("\nWith Formatting:\n");
    printf("A = %-10.2f %-10.2f\n", a, a);
    printf("B = %-10.2f %-10.2f\n", b, b);
    printf("C = %-10.2f %-10.2f\n", c, c);
}
Without Formatting:
A = 12.000000 12.000000
B = 123.456000 123.456000
C = 1234.560000 1234.560000

With Formatting: (* denotes space)
A = 12.00 12.00
B = 123.46 123.46
C = 1234.56 1234.56

Example 3: String formatting. 1

#include<stdio.h>
void main()
{
    printf("%10s","Item Code");
    printf("%20s","Description");
    printf("%16s","Price");
    printf("%20s","Quantity");
    printf("%20s","Total Price\n");

    printf("%3s%-10s","","101");
    printf("%8s%-15s","","Pen");
    printf("%10s","20");
    printf("%18s","12");
    printf("%20s","40\n");

    printf("%3s%-10s","","1020");
    printf("%8s%-15s","","Pendrive");
    printf("%10s","500");
    printf("%18s","3");
    printf("%20s","1500\n");

    printf("%3s%-10s","","11103");
    printf("%8s%-15s","","Printer");
    printf("%10s","15000");
    printf("%18s","1");
    printf("%20s","15000\n");
    printf("-------------------------------------------------\n");
    printf("%14s","Final Amount");
    printf("%69s","16540");
	printf("\n-------------------------------------------------\n");
}
Item Code      Description           Price            Quantity        Total Price
  101            Pen                    20                12                240
  1020           Pendrive              500                 3               1500
  11103          Printer             15000                 1              15000
--------------------------------------------------------------------------------
Final Amount                                                              16740
--------------------------------------------------------------------------------

C - Formatting output: Exercise

Exercise 1: Try to create the shopping list using formatting characters as shown in the below figure. 1