bug in snprintf?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

bug in snprintf?

Michael Hentschel
I am not sure, but to me this looks strange:

When using snprintf (as well as printf) with hex numbers and the 0x
prefix, the prefix is not printed for zero values. It is for all other
values.

eg. snprintf(s, sizeof(s), "%#x\n", 0);
the output is just "0", not "0x0" as I would expect.

Is that a bug?
I used the mingw-runtime-3.15 package.

(I attached a test program)

regards,
Mike

#include <stdio.h>
#include <windows.h>

int main(void)
{
    char s[100];
    printf("printf:\n");
    printf("plain:                      |%x\n", 0);
    printf("padded w/ blanks:           |%6x\n", 0);
    printf("padded w/ zero:             |%06x\n", 0);
    printf("plain w/ prefix:            |%#x\n", 0);
    printf("padded w/ blanks w/ prefix: |%#6x\n", 0);
    printf("padded w/ zero w/ prefix:   |%#06x\n\n", 0);

    printf("plain:                      |%x\n", 10);
    printf("padded w/ blanks:           |%6x\n", 10);
    printf("padded w/ zero:             |%06x\n", 10);
    printf("plain w/ prefix:            |%#x\n", 10);
    printf("padded w/ blanks w/ prefix: |%#6x\n", 10);
    printf("padded w/ zero w/ prefix:   |%#06x\n\n", 10);

    printf("wsprintf:\n");
    wsprintf(s, "plain:                      |%x\n", 0);
    printf("%s", s);
    wsprintf(s, "padded w/ blanks:           |%6x\n", 0);
    printf("%s", s);
    wsprintf(s, "padded w/ zero:             |%06x\n", 0);
    printf("%s", s);
    wsprintf(s, "plain w/ prefix:            |%#x\n", 0);
    printf("%s", s);
    wsprintf(s, "padded w/ blanks w/ prefix: |%#6x\n", 0);
    printf("%s", s);
    wsprintf(s, "padded w/ zero w/ prefix:   |%#06x\n\n", 0);
    printf("%s", s);

    printf("snprintf:\n");
    snprintf(s, sizeof(s), "plain:                      |%x\n", 0);
    printf("%s", s);
    snprintf(s, sizeof(s), "padded w/ blanks:           |%6x\n", 0);
    printf("%s", s);
    snprintf(s, sizeof(s), "padded w/ zero:             |%06x\n", 0);
    printf("%s", s);
    snprintf(s, sizeof(s), "plain w/ prefix:            |%#x\n", 0);
    printf("%s", s);
    snprintf(s, sizeof(s), "padded w/ blanks w/ prefix: |%#6x\n", 0);
    printf("%s", s);
    _snprintf(s, sizeof(s), "padded w/ zero w/ prefix:   |%#06x\n\n", 0);
    printf("%s", s);

    getchar();

    return 0;
}

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: bug in snprintf?

Keith Marshall
On Tuesday 16 September 2008 16:55:57 Michael Hentschel wrote:
> I am not sure, but to me this looks strange:
>
> When using snprintf (as well as printf) with hex numbers and the 0x
> prefix, the prefix is not printed for zero values. It is for all
> other values.

I thought it a bit odd too, when I implemented it, but...

> eg. snprintf(s, sizeof(s), "%#x\n", 0);
> the output is just "0", not "0x0" as I would expect.

...this is correct behaviour.

> Is that a bug?

No.  It is as POSIX, (and C99), require it:
http://www.opengroup.org/onlinepubs/009695399/functions/snprintf.html
| #
|     Specifies that the value is to be converted to an alternative
|     form. ... For x or X conversion specifiers, a non-zero result
                                                  ^^^^^^^^^^^^^^^^^
|     shall have 0x (or 0X) prefixed to it

Regards,
Keith.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: bug in snprintf?

Michael Hentschel
>> eg. snprintf(s, sizeof(s), "%#x\n", 0);
>> the output is just "0", not "0x0" as I would expect.
>
> ...this is correct behaviour.
>
>> Is that a bug?
>
> No.  It is as POSIX, (and C99), require it:
> http://www.opengroup.org/onlinepubs/009695399/functions/snprintf.html
> | #
> |     Specifies that the value is to be converted to an alternative
> |     form. ... For x or X conversion specifiers, a non-zero result
>                                                   ^^^^^^^^^^^^^^^^^
> |     shall have 0x (or 0X) prefixed to it
>

OK, if that is the specs....
But I wonder, why they specified it like that. I guess almost every
application has to work around that now and print the hex zero manually.

regards,
Mike

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: bug in snprintf?

Robert Riebisch
Michael Hentschel wrote:

> But I wonder, why they specified it like that. I guess almost every
> application has to work around that now and print the hex zero manually.

Maybe because a "0" is a "0" in every common numerical system.

Robert Riebisch
--
BTTR Software
http://www.bttr-software.de/

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: bug in snprintf?

Michael Hentschel
>
>> But I wonder, why they specified it like that. I guess almost every
>> application has to work around that now and print the hex zero manually.
>
> Maybe because a "0" is a "0" in every common numerical system.
>

Well, I think the printf functions are mostly used for puting a
numerical value (a number) into some sort of string, which in turn is
printed to a screen, file, etc. In many cases this output is supposed to
be displayed to a human. And IMHO I find it somewhat inconsistent if I
change a hex number on my screen being displayed nicely with the 0x
prefix, only for zero the prefix disappeares.
But I don't want to question the C99 specs, so exuse me nagging ;-)

regards,
Mike

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: bug in snprintf?

Keith Marshall
On Wednesday 17 September 2008 09:52:47 Michael Hentschel wrote:
> >> But I wonder, why they specified it like that. I guess almost
> >> every application has to work around that now and print the hex
> >> zero manually.

Rhetorical, I guess, but if you want to follow it up, you would need
to consult the proceedings of the ISO Technical Committee, by which
the standard was ratified; my unauthoritative guess would be the same
as Robert's...

> > Maybe because a "0" is a "0" in every common numerical system.
>
> Well, I think the printf functions are mostly used for puting a
> numerical value (a number) into some sort of string, which in turn
> is printed to a screen, file, etc. In many cases this output is
> supposed to be displayed to a human. And IMHO I find it somewhat
> inconsistent if I change a hex number on my screen being displayed
> nicely with the 0x prefix, only for zero the prefix disappeares.

FWIW, I agree that this is inconvenient, but I did religiously follow
the spec, when I was writing the implementation for 3.15.  You will
also find that GNU's glibc implementation, and even Microsoft's
MSVCRT implementation exhibit identical behaviour; for me to make it
otherwise would create an unnecessary obstacle for those who wish to
write portable code.

> But I don't want to question the C99 specs, so exuse me nagging ;-)

IAC, this would be entirely the wrong forum in which to pursue any
such question.

Regards,
Keith.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users