Printing with right and left justified using printf() function in C programming

 

 

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 with right and left justified using printf() function in C programming

To show: How to print integers with right and left justifying using printf() C function

 

 

// Right and left justifying integer printing

#include <stdio.h>

 

int main(void)

{

printf("Right justifying and left justifying values.\n");

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

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

printf("%10s%10d%10c%10f\n\n", "hello", 7, 'a', 1.23);

printf("%-10s%-10d%-10c%-10f\n", "hello", 7, 'a', 1.23);

return 0;

}

 

Output example:

 

Right justifying and left justifying values.

Compare the output with the source code.

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

hello 7 a 1.230000

hello 7 a 1.230000

Press any key to continue . . .

 

 

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