C programming Question

Discussion in 'Computer Science & Culture' started by Trilobyte, Oct 31, 2005.

Thread Status:
Not open for further replies.
  1. Trilobyte Registered Senior Member

    Messages:
    130
    I'm starting to use an integrated development environment for C/C++ ("Dev C++"), but am having trouble with simple variable types. (namely 'float' and 'double').

    The only output I can seem to get from the following program is the value zero displayed on the screen (I wrote this program to test what was wrong with a larger program after I found by elimination that the variable type was the cause of the "undesirable output"). Can anyone point out an obvious flaw or suggest why it gives this output?

    #include<stdio.h>
    int main()
    {
    float a=7.0;
    printf("%d",a);
    }

    (that is the program but to be able to have it stay on the screen I just added an infinite loop, like below)

    #include<stdio.h>
    int main()
    {
    float a=7.0;
    printf("%d",a);


    do
    { printf("\a");}
    while(1==1);
    }

    The main point is that if I change the variable type to 'int' then it outputs, a 7 in this case, as expected, but with 'float' and equally with 'double' it only outputs a zero. Why??
     
    Last edited: Nov 1, 2005
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Facial Valued Senior Member

    Messages:
    2,225
    What's %d used for? The double or float?
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Trilobyte Registered Senior Member

    Messages:
    130
    Good question. I was actually just about to delete my post. I just found what I did. It should be %f and not %d for type float, when it is stated in printf.
    Thanks anyway.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Blindman Valued Senior Member

    Messages:
    1,425
    It should be printf("%f",a) to display a float. %d displays int's and decimal numbers.
     
  8. Facial Valued Senior Member

    Messages:
    2,225
    There we go. thanks blindman
     
  9. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    Here's a decent link to a site which has all the formatting options for printf. Could help in the future.

    -AntonK
     
Thread Status:
Not open for further replies.

Share This Page