C - Formatting output

Formatting output is use to format the output and aligns the number and string.

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

#include<stdio.h>
#include<conio.h>

void main()
{
   int a=12, b=123, c=1234;
   clrscr();
   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);
   getch();
}
Without Formatting:
12
123
1234

With Formatting:
        12
      123
    1234

Example 2: Program to show floating data type in required format.

#include<stdio.h>
#include<conio.h>

void main()
{
   float a=12, b=123.456, c=1234.56;
   clrscr();
   printf("Without Formatting:\n");
   printf("A = %f\n", a);
   printf("B = %f\n", b);
   printf("C = %f\n", c);

   printf("\nWith Formatting:\n");
   printf("A = %10.2f\n", a);
   printf("B = %10.2f\n", b);
   printf("C = %10.2f\n", c);
   getch();
}

C - Formatting output: Exercise

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

How to format user output