Using the C sprintf()/sprintf_s() function

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

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:

To do: Displaying a formatted output using C standard output sprintf()/sprintf_s() function

To show: Using the sprintf()/sprintf_s() C function for standard output programming

 

 

// Using sprintf()/sprintf_s()

#include <stdio.h>

 

int main(void)

{

char s[80];

int x;

float y;

 

printf("Using sprint()\n");

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

printf("Enter an integer and a float, separated by space: \n");

// scanf("%d%f", &x, &y); - using as secure version

scanf_s("%d%f", &x, &y);

// sprintf(s, "Integer:%6d\nFloat:%8.2f", x, y); - using a secure version

sprintf_s(s, 80, "Integer:%6d\nFloat:%8.2f", x, y);

printf("\n%s\n%s\n", "The formatted output stored in array s is: ", s);

return 0;

}

 

Output example:

 

Using sprint()

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

Enter an integer and a float, separated by space:

100 212.45

The formatted output stored in array s is:

Integer: 100

Float: 212.45

Press any key to continue . . .

 

 

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