sprintf()

Formats a character string array.

  #include <stdio.h>
  
  int sprintf(char *s, const char *format, ...);    
Parameter

s

A pointer to the character string to write to.

format

A pointer to the format string.

Remarks

The sprintf() function works identically to printf() with the addition of the s parameter. Output is stored in the character array pointed to by s instead of being sent to stdout. The function terminates the output character string with a null character. sprintf() returns the number of characters assigned to s, not including the nul character.

Be careful when using this function. It can be a source for serious buffer overflow bugs. Unlike snprintf(), the programmer cannot specify a limit on the number of characters to store in s.

This facility may not be available on configurations of the EWL that run on platformswithout file systems.

Listing: Example of sprintf() Usage

#include <stdio.h>

int main(void)

{

int i = 1;

char s[] = "woodworking";

char dest[50];

sprintf(dest, "%s is number %d!", s, i);

puts(dest);

return 0;

}

Output:

woodworking is number 1!