
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Align Output Using Justifications in C Language
By using justifications in the printf statement, we can format the data in various ways. To implement the right justification, insert a minus sign before the width value in the %s character.
printf("%-15s",text);
Program 1
The example below prints a table with name and amounts, formatted in two columns with fixed widths for better alignment, using printf() for row and column-wise output.
#include<stdio.h> int main(){ char a[20] = "Names", b[20]="amount to be paid"; char a1[20] = "Bhanu", b1[20]="Hari",c1[20]="Lucky",d1[20]="Puppy"; int a2 = 200, b2 = 400, c2 = 250, d2 = 460; printf("%-15s %-15s
", a, b); printf("%-15s %-15d
", a1, a2); printf("%-15s %-15d
", b1, b2); printf("%-15s %-15d
", c1, c2); printf("%-15s %-15d
", d1, d2); return 0; }
Output
The result is generated as follows ?
Names amount to be paid Bhanu 200 Hari 400 Lucky 250 Puppy 460
Program 2
This C program prints a table with names and amounts, formatting the data into two columns using printf() with determined alignment. It demonstrates basic integer and string formatting in C.
#include<stdio.h> int main(){ char a[20] = "Names", b[20]="amount to be paid"; char a1[20] = "Bhanu", b1[20]="Hari",c1[20]="Lucky",d1[20]="Puppy"; int a2=200,b2=400,c2=250,d2=460; printf("%2s %2s
", a, b); printf("%5s %5d
", a1,a2); printf("%2s %2d
", b1,b2); printf("%5s %5d
", c1, c2); printf("%2s %2d
", d1, d2); return 0; }
Output
The output is obtained as follows ?
Names amount to be paid Bhanu 200 Hari 400 Lucky 250 Puppy 460Note: Alignment is not proper if we do not use the correct justification
Advertisements