snprintf() wrong return value for %c with \0 char

Francesco Montorsi f18m_cpp217828@yahoo.it
Wed Jan 24 11:00:00 GMT 2007


Hi all,

    This is my first msg to this mailing list. If this is the wrong ML 
please let me know.

I've found a "problem" with GNU printf() function family. I'm not sure 
it's a bug but consider the following code:


int main(int argc, char **argv)
{
     char test[64];
     int res = snprintf(test, 64, "hello %c world", '\0');
     printf("String generated: %s\n", test);
     printf("Return value of snprintf(): %d\n", res);
     printf("Lenght of the string: %d\n", strlen(test));

     return 0;
}


if executed on my Ubuntu Edgy (which uses GNU libc 2.4.1) it outputs:

String generated: hello
Return value of snprintf(): 13
Lenght of the string: 6

Now, I'm not sure if the return value of snprintf() is supposed to be 6 
or 13 in this case. I'd say the return value of snprintf() should always 
be the same of that returned by strlen()....

I've looked at page:

http://www.gnu.org/software/libc/manual/html_node/Other-Output-Conversions.html

but I couldn't find any "specification" about using %c with '\0'... so 
maybe returning 13 is the 'expected' behaviour (or maybe noone ever 
considered such patological case ;)).

What do you say?

Thanks,
Francesco Montorsi




More information about the Libc-alpha mailing list