sprintf Function

Prints formatted text to a string.

Include

<stdio.h>

Prototype

int sprintf(char *s, const char *format, ...);

Arguments

s
storage string for output
format
format control string
...
optional arguments

Return Value

Returns the number of characters stored in s excluding the terminating null character.

Remarks

The format argument has the same syntax and use that it has in printf.

Example

#include <stdio.h>

int main(void)
{
  char sbuf[100], s[]="Print this string";
  int x = 1, y;
  char a = '\n';

  y = sprintf(sbuf, "%s %d time%c", s, x, a);

  printf("Number of characters printed to "
         "string buffer = %d\n", y);
  printf("String = %s\n", sbuf);
}

Example Output

Number of characters printed to string buffer = 25
String = Print this string 1 time