samedi 18 avril 2015

What is proper size of buffer when using sprint()?

What is proper size of an char array (buffer) when i want to use sprintf function?


I dont know why this part of code is working if buffer can hold only 1 char? I put a lot more chars inside than 1.



/* sprintf example */
#include <stdio.h>

int main ()
{
char buffer[1];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a string %d chars long\n",buffer,n);
return 0;
}


Results:



[5 plus 3 is 8] is a string 13 chars long

Aucun commentaire:

Enregistrer un commentaire