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.