Use snprintf function to format data into string

Syntax

C snprintf function has the following format.

int snprintf(char * restrict buf, size_t num, const char * restrict format, ...)

C snprintf function is from header file stdio.h.

Description

C snprintf function is identical to sprintf() except that a maximum of num-1 characters will be stored.

Example

Use C snprintf function to convert data to string with format.


#include <stdio.h>
// ww  w .j  a  va 2  s.  com
int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=snprintf (buffer, 5,"%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a %d char long string\n",buffer,n);
  return 0;
}




















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