%f undefined behavior?

Discussion in 'C Programming' started by Mantorok Redgormor, Sep 16, 2003.

  1. Why does the following print -1.997505? Is this because of undefined behavior?

    #include <stdio.h>
    int main(void)
    {
    printf("%f\n", 0x1f);
    return 0;
    }
     
    Mantorok Redgormor, Sep 16, 2003
    #1
    1. Advertising

  2. Mantorok Redgormor <> spoke thus:
    > Why does the following print -1.997505? Is this because of undefined behavior?


    > #include <stdio.h>
    > int main(void)
    > {
    > printf("%f\n", 0x1f);
    > return 0;
    > }


    Compiling with all warnings on...
    test.c:6: warning: double format, different type arg (arg 2)

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
     
    Christopher Benson-Manica, Sep 16, 2003
    #2
    1. Advertising

  3. In article <>,
    Mantorok Redgormor wrote:
    > Why does the following print -1.997505? Is this because of undefined behavior?
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > printf("%f\n", 0x1f);
    > return 0;
    > }


    Here, it prints
    -14167097396369144661962308996344933237638
    562294362303773503482977858904653824.000000
    (except for the line break in the middle)

    But then again, you're printing an integer with a floating point
    format string, which makes it interpret the bit pattern of the
    integer as if it was the bit pattern of a floating point value
    (possibly going outside the data that was the integer).

    --
    Andreas Kähäri
     
    Andreas Kahari, Sep 16, 2003
    #3
  4. Andreas Kahari <> wrote:

    >In article <>,
    >Mantorok Redgormor wrote:
    >> Why does the following print -1.997505? Is this because of undefined behavior?
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> printf("%f\n", 0x1f);
    >> return 0;
    >> }

    >
    >Here, it prints
    >-14167097396369144661962308996344933237638
    >562294362303773503482977858904653824.000000
    >(except for the line break in the middle)
    >
    >But then again, you're printing an integer with a floating point
    >format string, which makes it interpret the bit pattern of the
    >integer as if it was the bit pattern of a floating point value
    >(possibly going outside the data that was the integer).


    Even worse: it invokes Nasal Demons, aka undefined behaviour, because
    %f forces printf to retrieve an argument of sizeof(double), but there
    is only sth. of size of an int available.

    [OT]
    IOW: this will most certainly mess up your stack, whatever that is...
    [/OT]

    Regards

    Irrwahn
    --
    What does this red button do?
     
    Irrwahn Grausewitz, Sep 16, 2003
    #4
  5. Irrwahn Grausewitz <> wrote in message news:<>...
    > Andreas Kahari <> wrote:
    >
    > >In article <>,
    > >Mantorok Redgormor wrote:
    > >> Why does the following print -1.997505? Is this because of undefined
    > >> behavior?
    > >>
    > >> #include <stdio.h>
    > >> int main(void)
    > >> {
    > >> printf("%f\n", 0x1f);
    > >> return 0;
    > >> }

    > >
    > >Here, it prints
    > >-14167097396369144661962308996344933237638
    > >562294362303773503482977858904653824.000000
    > >(except for the line break in the middle)
    > >
    > >But then again, you're printing an integer with a floating point
    > >format string, which makes it interpret the bit pattern of the
    > >integer as if it was the bit pattern of a floating point value
    > >(possibly going outside the data that was the integer).


    It needn't ever even attempt to interpret the bit pattern of the
    integer.

    >
    > Even worse: it invokes Nasal Demons, aka undefined behaviour, because
    > %f forces printf to retrieve an argument of sizeof(double), but there
    > is only sth. of size of an int available.


    Huh? %f expects a double, not merely an object the same size of a
    double.

    > [OT]
    > IOW: this will most certainly mess up your stack, whatever that is...
    > [/OT]


    Not all the world is intel.

    There are implementations (modern, in use) which pass parameters via
    registers, not a hardware stack.

    --
    Peter
     
    Peter Nilsson, Sep 16, 2003
    #5
  6. (Peter Nilsson) wrote:

    >Irrwahn Grausewitz <> wrote in message news:<>...
    >> Andreas Kahari <> wrote:

    <SNIP>
    >> >
    >> >But then again, you're printing an integer with a floating point
    >> >format string, which makes it interpret the bit pattern of the
    >> >integer as if it was the bit pattern of a floating point value
    >> >(possibly going outside the data that was the integer).

    >
    >It needn't ever even attempt to interpret the bit pattern of the
    >integer.
    >
    >>
    >> Even worse: it invokes Nasal Demons, aka undefined behaviour, because
    >> %f forces printf to retrieve an argument of sizeof(double), but there
    >> is only sth. of size of an int available.

    >
    >Huh? %f expects a double, not merely an object the same size of a
    >double.


    Hm, I used strange wording here, indeed.

    >
    >> [OT]
    >> IOW: this will most certainly mess up your stack, whatever that is...
    >> [/OT]

    >
    >Not all the world is intel.


    Oh, really? :)

    >
    >There are implementations (modern, in use) which pass parameters via
    >registers, not a hardware stack.


    Right, and that's just the reason for [OT][/OT] tagging...
    --
    What does this red button do?
     
    Irrwahn Grausewitz, Sep 17, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aka
    Replies:
    10
    Views:
    746
  2. REH
    Replies:
    25
    Views:
    857
    Victor Bazarov
    Mar 29, 2005
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,847
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    668
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    393
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page