Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By AndiPersti

snprintf what does the standard say about this code should output.

This is a discussion on snprintf what does the standard say about this code should output. within the C Programming forums, part of the General Programming Boards category; FYI: I do some embedded programming with a non-standard C-like compiler. They have changed how the function snprintf works from ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2,702

    snprintf what does the standard say about this code should output.

    FYI:

    I do some embedded programming with a non-standard C-like compiler. They have changed how the function snprintf works from one major version to another. I am thinking they changed to more closely match the C Standard like the 89 or 99 standard. After, I figure out what the standard says; I plan to test Rabbit's Dynamic C under version 9.62 and 10.72. Right now, I am not sure what to test.

    Here's code I am running under MinGW GCC just to figure out what snprintf does under MinGW GCC (4.7.1) TDM build shipped with Code::Blocks 12.11. I am guessing MinGW GCC is close to one of the C standards.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char testArray[8] = {0};
        int result;
    
        result = snprintf(testArray, 6, "%s", "abcdef");
        printf("snprintf := %d; testArray:=\"%s\"\n", result, testArray);
    
        result = snprintf(testArray, 5, "%4s", "abcdef");
        printf("snprintf := %d; testArray:=\"%s\"\n", result, testArray);
    
        result = snprintf(testArray, 6, "%4s", "abcdef");
        printf("snprintf := %d; testArray:=\"%s\"\n", result, testArray);
    
        result = snprintf(testArray, 5, "%4s", "abcde");
        printf("snprintf := %d; testArray:=\"%s\"\n", result, testArray);
    
        result = snprintf(testArray, 5, "%4s", "abcd");
        printf("snprintf := %d; testArray:=\"%s\"\n", result, testArray);
    
        return 0;
    }
    The output

    Code:
    snprintf := 6; testArray:="abcde"
    snprintf := 6; testArray:="abcd"
    snprintf := 6; testArray:="abcde"
    snprintf := 5; testArray:="abcd"
    snprintf := 4; testArray:="abcd"

    I expected the number of chars output in the second line to be 5 instead of 6.

    Is this a lack of mine understanding of the standard or a poor implementation of the standard in MinGW GCC (4.7.1) TDM build?

    Please state which C standard you are talking about the correct answer should be.

    Edit: I just tested Rabbit Dynamic C 10.72 and it matched MinGW GCC output; so, I am going with it is my lack of understanding of the C standard; unless someone posts otherwise. It is too hard to test Rabbit Dynamic C 9.62 (at home) so, I will likely not test it for a long time, if ever.

    Tim S.
    Last edited by stahta01; 03-03-2013 at 01:21 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Code:
    $ gcc -W -Wall -Wextra foo.c
    $ ./a.out 
    snprintf := 6; testArray:="abcde"
    snprintf := 6; testArray:="abcd"
    snprintf := 6; testArray:="abcde"
    snprintf := 5; testArray:="abcd"
    snprintf := 4; testArray:="abcd"
    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
    Copyright (C) 2011 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    The second one returns 6 because output was truncated.
    The functions snprintf() and vsnprintf() do not write more than size bytes (including the trailing '\0'). If the output was truncated due to this limit then the return
    value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available. Thus, a
    return value of size or more means that the output was truncated. (See also below under NOTES.)
    stahta01 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    The C99 standard (draft version) says (7.19.6.5 3)
    Returns
    The snprintf function returns the number of characters that would have been written had n been sufficiently large, not counting the terminating null character, or a negative value if an encoding error occurred. Thus, the null-terminated output has been completely written if and only if the returned value is nonnegative and less than n.
    Same for C11 draft version.

    So the output is correct (in regard to the standard).

    Bye, Andreas
    stahta01 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. standard input / output check
    By benjammin5 in forum C Programming
    Replies: 5
    Last Post: 10-26-2011, 01:18 AM
  2. Replies: 3
    Last Post: 07-11-2011, 10:40 AM
  3. Replies: 7
    Last Post: 09-16-2009, 10:01 AM
  4. Where does the standard output go the following program
    By Overworked_PhD in forum Linux Programming
    Replies: 1
    Last Post: 02-23-2009, 07:48 PM
  5. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21