Snprintf confusion

I am confused with snprintf function. First of all, I did not find the snprintf function in the turbo C version compiler under stdio.h Secondly, in the GNU compiler, snprintf returns -1 when the buffer size is smaller than the formatted string, although it should return the number of characters that would be printed. if the buffer size was big enough. I have the following source:

#include<stdio.h>
int main()
{
  char str[100];
  int numchar = snprintf(str,2,"ello jdj");
  printf("%d\n",numchar);
  return 0;
}

This code should output 8 according to what I know about snprintf. but it returns -1 in my GNU compiler. What are the facts?

+5
source share
1 answer

It looks like you are using an old version of glibc. On the man page for snprintf:

glibc snprintf() vsnprintf() C99, , , glibc 2.1. glibc 2.0.6 -1, .

+5

All Articles