Mixing doubles and integers?

Discussion in 'C Programming' started by Johs, Jan 3, 2007.

  1. Johs

    Johs Guest

    In the below example a double is initialized with an integer. Afterwards
    another double is added:

    int mini = 0;
    double dd = 7;
    double a = mini;
    double res = a + dd;

    printf("res = %d\n", res);

    res equals 0, but why does it not equal 7?
    Johs, Jan 3, 2007
    #1
    1. Advertising

  2. In article <engfjt$gmt$-c.dk> Johs <> writes:
    > In the below example a double is initialized with an integer. Afterwards
    > another double is added:
    >
    > int mini = 0;
    > double dd = 7;
    > double a = mini;
    > double res = a + dd;
    >
    > printf("res = %d\n", res);
    >
    > res equals 0, but why does it not equal 7?


    Are you sure the format specifier matches the argument to printf?
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Jan 3, 2007
    #2
    1. Advertising

  3. Johs

    Chris Dollin Guest

    Johs wrote:

    > In the below example a double is initialized with an integer. Afterwards
    > another double is added:
    >
    > int mini = 0;
    > double dd = 7;
    > double a = mini;
    > double res = a + dd;
    >
    > printf("res = %d\n", res);
    >
    > res equals 0, but why does it not equal 7?


    `res` is a double. `%d` format expects an int. Your
    code is broken: it will exhibit Undefined Behaviour,
    which allows any of: behaving the way you want,
    behaving the way /I/ want, behaving the way the
    compiler-writer wanted, behaving the way the printf-
    writer wanted, behaving the way that "came naturally"
    for the code of the compiler and/or printf; such
    behaviour including, but not limited to, printing
    "rubbish", printing rubbish, printing a helpful
    run-time diagnostic, printing a helpful compile-time
    diagnostic, printing an /un/helpful run-time diagnostic,
    printing nothing but corrupting memory so that a completely
    obscure and apparently unrelated misbehaviour happens
    later on, ditto but not until an important client
    demo, ditto but not until used in a life-critical
    application [1], posting your credit card details to
    a selection of blogs, wiping your hard drive, and
    teleporting Early Spike into your bedroom late at
    night. Yes, the Standard doesn't even require Undefined
    Behaviour to be physically possible: it doesn't require
    anything at all. Hedgehogs may sing.

    [1] Of course the code reviews and unit tests will
    catch this before production [2].

    [2] Nervous giggle.

    --
    Chris "hopefully not Pyecroft" Dollin
    Nit-picking is best done among friends.
    Chris Dollin, Jan 3, 2007
    #3
  4. Johs

    Guest

    Johs wrote:
    > In the below example a double is initialized with an integer. Afterwards
    > another double is added:
    >
    > int mini = 0;
    > double dd = 7;
    > double a = mini;
    > double res = a + dd;
    >
    > printf("res = %d\n", res);
    >
    > res equals 0, but why does it not equal 7?


    You don't know what it equals - you lied to printf() and told it you
    were passing an int, then gave it a double...

    Note - it's usually better to cut and paste a complete testcase program
    (small enough to be postable) rather than just some code snippets like
    these.
    , Jan 3, 2007
    #4
    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. Paul Morrison

    doubles and integers

    Paul Morrison, Jan 4, 2005, in forum: Java
    Replies:
    2
    Views:
    348
    Ryan Stewart
    Jan 4, 2005
  2. dan
    Replies:
    1
    Views:
    2,301
    Jack Klein
    Nov 26, 2003
  3. J.K. Becker
    Replies:
    43
    Views:
    983
  4. Chris N. Hinds

    unions with long long ints and doubles?

    Chris N. Hinds, Sep 30, 2003, in forum: C Programming
    Replies:
    3
    Views:
    411
    Barry Schwarz
    Oct 2, 2003
  5. Martin Joergensen
    Replies:
    68
    Views:
    1,682
    Dave Thompson
    Apr 3, 2006
Loading...

Share This Page