Formatting the standard output using printf() C function for character, string, integer and float

 

 

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: How to format the standard output using printf() C function for character, string, integer and float

To show: Formatting the standard output using printf() C function for character, string, integer and float

 

 

/* C format specifier example */

#include <stdio.h>

 

int main(void)

{

/* string and integer */

printf("My name is %s and I am %d years old.\n", "John", 25);

 

/* floating point numbers */

printf("Examples of the decimal points are: %f\t%.3f\t%.2f\t\n",1.7885,1.7885,1.7885);

 

/* characters */

printf("Examples of characters are: \n");

printf(" %c \n %c \n %c\n", 'A', 'B', 'a');

 

return 0;

}

 

Output example:

 

My name is John and I am 25 years old.

Examples of the decimal points are: 1.788500 1.789 1.79

Examples of characters are:

A

B

a

Press any key to continue . . .

 

 

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