Thread: Beginner's C, problems with double, float

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Question Beginner's C, problems with double, float

    This is probably as simple as it gets, but here goes:

    This is my code:

    Code:
    main(){
    float alpha=0.9;
    printf("alpha=%d\n",alpha);
    }
    And this is my output:

    Code:
    alpha=1072483532
    What gives? The same thing happens when I change from "float" to "double". I'm compiling using gcc on Mac OS X, if that helps at all...

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Firstly, don't use %d, the 'd' modifier is for integers. Use %f for floats & doubles.

    http://www.cplusplus.com/reference/c...io/printf.html

    Also, be explicit - eg int main(), main also has to return a value see the FAQ.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float alpha = 0.9f;
        printf("alpha = %f", alpha);
        
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Smile Thank you!

    Thank you for your help--I made the changes that you suggested, and all's right with the world now. Except, possibly, that I've realized I'm a total n00b (I think that's how you folk spell it, yeah?).

    Thank you again!

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Never mind, practice makes perfect - we all had to start somewhere. If you're serious about learning C, get a book as it really helps.

    Some suggested ones: http://cboard.cprogramming.com/showthread.php?t=74079

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm compiling using gcc on Mac OS X
    Use the following compiler options, they'll tell you a lot about your code without having to run it.
    Code:
    $ cat foo.c
    #include <stdio.h>
    main(){
    float alpha=0.9;
    printf("alpha=%d\n",alpha);
    }
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c:2: warning: return type defaults to `int'
    foo.c: In function `main':
    foo.c:4: warning: int format, double arg (arg 2)
    foo.c:5: warning: control reaches end of non-void function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  5. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM