Menu

float var gives ?wrong? values in printf

John
2005-10-11
2012-09-26
  • John

    John - 2005-10-11

    I am going through the tutorial that comes with Dev-C++ 5 (beta). Everything was great right up to float variables.

    int,and char work fine. But float and double work well untill I give them a value other than zero. In the code below, if I give a and b a value greater than zero, a printf's as 0 and b as 1072693248. Double does the same.

    Am I trying to printf them incorecctly?

    I tried uninstalling dev-C++ 5 and installing ver. 4. I'm back to five with a clean install and an install on another pc. All do the same trick.

    Here is the code:


    include <cstdlib>

    include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    int one, two;
    one=1;
    two=2;
    float a,b;
    a=1;
    b=2;
    printf("var 'one' equals: -%d-, and var 'two' equals: -%d-\n",one, two);
    printf("But Float var 'a' is %d (should be 1), and b is %d (should be 2)\n\n Why Why Why???\n\n",a,b);
    system("PAUSE");
    return EXIT_SUCCESS;
    }


    The output of this is:

    var 'one' equals: -1-, and var 'two' equals: -2-
    But Float var 'a' is 0 (should be 1), and b is 1072693248 (should be 2)

    Why Why Why???

    Press any key to continue . . .

    end of output

    Thanks,
    Snapd8

     
    • John

      John - 2005-10-11

      I finally found the right question and asked the oracle, and google said "%f my son, %f"

      Thanks and sorry, posted to soon.

      Snapd8

       

Log in to post a comment.