Question about mixed scalar type operation

Discussion in 'C Programming' started by linq936@hotmail.com, May 14, 2007.

  1. Guest

    Hi,
    I have this little test program:

    #include <stdio.h>

    int main(){
    float f1 = 2.2;
    double d1 = 3.3;
    int j = f1 * d1;
    int i = (int) f1 * d1;
    int i2 = (int) (f1 * d1);
    printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);
    }

    I use gcc on Linux to compile it, there is no warning and the result
    is like this:

    2066953011, i=1075644989, j=6, i2=7

    I have several questions:

    1. Why there is no warning in compiler when I assign the
    multiplication of float and double to integer?

    2. As for the result, I can understand how i2 comes from, but others
    do not make sense to me.

    Could you give me some pointer?
    , May 14, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:

    > I have this little test program:


    >#include <stdio.h>


    >int main(){
    > float f1 = 2.2;
    > double d1 = 3.3;
    > int j = f1 * d1;
    > int i = (int) f1 * d1;
    > int i2 = (int) (f1 * d1);
    > printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);
    >}


    > I use gcc on Linux to compile it, there is no warning and the result
    >is like this:


    >2066953011, i=1075644989, j=6, i2=7


    > I have several questions:


    >1. Why there is no warning in compiler when I assign the
    >multiplication of float and double to integer?


    It is not considered a syntactic or semantic error to do such
    a multiplication and assignment -- the results are well defined.
    The compiler is not required to return a warning for well defined
    behaviour that -might- have been the result of the programmer having
    overlooked something (or might have been perfectly intended.)


    >2. As for the result, I can understand how i2 comes from, but others
    >do not make sense to me.


    In your printf() statement, f1*d1 is going to be a double, and you
    attempt to print that double out using an integer format. If it
    happens that on your machine that a double is "wider" than an
    integer, the %d format will only use up some of the bytes of the double,
    possibly leaving the other bytes from the double in a location to
    be printed by the next format element along, i=%d . Once that has
    happened, everything else gets shifted along from what you expect.
    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
    Walter Roberson, May 14, 2007
    #2
    1. Advertising

  3. wrote:
    > Hi,
    > I have this little test program:


    > #include <stdio.h>


    > int main(){
    > float f1 = 2.2;
    > double d1 = 3.3;
    > int j = f1 * d1;

    This is exactly the same as the i2 case below

    > int i = (int) f1 * d1;

    This is (int) 2.2 * 3.3 ->
    2 * 3.3 ->
    6.6
    assigned to an int as 6

    > int i2 = (int) (f1 * d1);

    This is (int) (2.2 * 3.3) =>
    (int) (7.26) ->
    7

    > printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);

    ^^
    This is an error. f1*d1 is a floating point value.
    %d is a specifier for signed ints.
    Unless a double and an int have exactly the same size,
    you have hopelessly munged the arguments to printf.

    Unless you are using a C99 compiler, you need to return a
    value here, e.g.
    return 0;

    > }


    > I use gcc on Linux to compile it, there is no warning and the result
    > is like this:


    > 2066953011, i=1075644989, j=6, i2=7


    After you change the erroneous "%d" to "%g", the output should look like:
    7.26, i=6, j=7, i2=7



    > I have several questions:


    > 1. Why there is no warning in compiler when I assign the
    > multiplication of float and double to integer?


    Because there is absolutely nothing wrong with doing so, and there is
    nothing to warn about.


    > 2. As for the result, I can understand how i2 comes from, but others
    > do not make sense to me.


    No, you can't understand "how i2 comes from", because the erroneous "%d"
    makes a complete hash of your output line.

    > Could you give me some pointer?


    Don't feed us straight lines.
    Martin Ambuhl, May 14, 2007
    #3
    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. Chishun Kwong
    Replies:
    0
    Views:
    411
    Chishun Kwong
    Mar 3, 2005
  2. david ullua
    Replies:
    13
    Views:
    653
  3. Replies:
    2
    Views:
    252
    David Thompson
    Jul 1, 2007
  4. Clint Olsen
    Replies:
    6
    Views:
    342
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    155
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page