sprintf changed from version 3.13 to 3.15

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
6 posts • Page 1 of 1
funkey
Posts:85
Joined: Mon Dec 27, 2010 1:11 pm

sprintf changed from version 3.13 to 3.15

Post by funkey »

Hello,

I just wanted to mention, that the inbuild sprintf function changed behavior between V3.13 and V3.15.

Code: Select all

string s;
sprintf(s, "Test_%03s", "5");
In V3.13 result is 'Test_005'
In V3.15 result is 'Test_ 5'


This is just for your info.

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: sprintf changed from version 3.13 to 3.15

Post by mkoller »

First, this is not what I can see.
Your code produces "Test_ 5" in the version 3.13 P11 ans also in 3.15 latest version
Second, this is probably not at all what you want to do. You probably want to use the format ending in %03d (int!) and also pass 5 as int, not as string.

funkey
Posts:85
Joined: Mon Dec 27, 2010 1:11 pm

Re: sprintf changed from version 3.13 to 3.15

Post by funkey »

So maybe the issue comes from the operating system!?
I use V3.13 Patch21 on openSuse 13.1 and V3.15 Patch7 on CentOS 7.3.

And I know I can use %03d with an int, but I could use a, b, c,.. with %03s.

funkey
Posts:85
Joined: Mon Dec 27, 2010 1:11 pm

Re: sprintf changed from version 3.13 to 3.15

Post by funkey »

Martin Koller wrote:
Your code produces "Test_ 5"
Anyway, this is wrong for both of your versions.

I tested in a C application

Code: Select all

#include 
#include 

int main()
{
    char buffer[50];
    char* x = "a";
    sprintf(buffer, "Test_%03s", x);
    printf("%s", buffer);
    return 0;
}
and the result is of course 'Test_00a'

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: sprintf changed from version 3.13 to 3.15

Post by mkoller »

I ran my tests on openSuse 42.3 and here your C program also does not show the '0' prefix.
The man page for sprintf states for the 0-flag:
"The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with zeros rather than blanks. If the 0 and - flags both appear, the 0 flag is ignored. If a precision is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag is ignored. For other conversions, the behavior is undefined."
So for a string it is undefined.
Probably the difference is the libc version. Mine is 2.22

funkey
Posts:85
Joined: Mon Dec 27, 2010 1:11 pm

Re: sprintf changed from version 3.13 to 3.15

Post by funkey »

OK, thanks! So this is great! It depends on system and systems are different and behaviour is undefined. Good to know now. :blink: :angry:

Best regards
Christoph

6 posts • Page 1 of 1