Displaying integers in various formatted output using conversion specifiers of the 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: Displaying integers in various formatted output using conversion specifiers of the printf() C function

To show: How to print integers in various formatted output using conversion specifiers of the C printf() function

 

 

// Using integer conversion specifiers to format the outputs

#include <stdio.h>

 

int main(void)

{

printf("Various formatted output\n");

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

printf("%%d = %d\n", 455);

// i same as d in printf()

printf("%%i = %i\n", 455);

printf("%%d = %d\n", +455);

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

printf("%%hd = %hd\n", 32000);

printf("%%ld = %ld\n", 2000000000L);

printf("%%o = %o\n", 455);

printf("%%u = %u\n", 455);

printf("%%u = %u\n", -455);

// -455 is read by %u and converted to the unsigned

// value 4294966841 by 4 bytes integer

printf("%%x = %x\n", 455);

printf("%%X = %X\n", 455);

 

return 0;

}

 

Output example:

 

Various formatted output

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

%d = 455

%i = 455

%d = 455

%d = -455

%hd = 32000

%ld = 2000000000

%o = 707

%u = 455

%u = 4294966841

%x = 1c7

%X = 1C7

Press any key to continue . . .

 

 

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