C sprintf converts formatted data to string

Syntax

C sprintf function has the following format.

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

C sprintf function is from header file stdio.h.

Description

C sprintf function is identical to printf() except that the output is a char array. It returns the number of characters actually placed into the array.

sprintf() provides no bounds checking. See snprintf for an alternative.

Example

Use C sprintf function to format data to string.


#include <stdio.h>
//from ww  w.j a v a  2s  .c o m
int main(void){
  char str[80];

  sprintf(str,"%s %d %c", "one", 2, '3');

  printf("%s", str);
}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h