Printing integers right-justified using printf() C function

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Printing integers right-justified using printf() C function

To show: How to print integers with right justified using printf() C function

 

 

// Printing integers right-justified

#include <stdio.h>

 

int main(void)

{

printf(" Printing integers right-justified.\n");

printf("Compare the output with the source code\n");

printf("---------------------------------------\n\n");

printf("%%4d, 1 =%4d\n", 1);

printf("%%4d, 12 =%4d\n", 12);

printf("%%4d, 123 =%4d\n", 123);

printf("%%4d, 1234 =%4d\n", 1234);

printf("%%4d, 12345 =%4d\n\n", 12345);

printf("%%4d, -1 =%4d\n", -1);

printf("%%4d, -12 =%4d\n", -12);

printf("%%4d, -123 =%4d\n", -123);

printf("%%4d, -1234 =%4d\n", -1234);

printf("%%4d,-12345 =%4d\n", -12345);

 

return 0;

}

 

Output example:

 

Printing integers right-justified.

Compare the output with the source code

---------------------------------------

%4d, 1 = 1

%4d, 12 = 12

%4d, 123 = 123

%4d, 1234 =1234

%4d, 12345 =12345

%4d, -1 = -1

%4d, -12 = -12

%4d, -123 =-123

%4d, -1234 =-1234

%4d,-12345 =-12345

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index