Thread: trouble with double

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    trouble with double

    Im nw to programing and was having trouble to make the following code work:
    Code:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
            double final_Z,re,im;
            re=0.0;
            im=0.0;
           
            re=re+1.0;
            im=im+5.0;
     
            printf("%d \n %d \n",re,im);    
     }
    It gives the following output:

    0
    1072693248

    I expect the output:
    1
    5

    whats wrong help !!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    Use &#37;f for decimal numbers.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Trafalgar Law View Post
    Use %f for floating point numbers.
    Correction in red above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Trafalgar Law View Post
    Use &#37;fl for double floating point numbers.
    Correction #2
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Trafalgar Law View Post
    Use %lf for double floating point numbers.
    Correction #3

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Correction #3
    Actually, %lf is only correct for "double" when it comes to scanf. On printf (or any other variable argument function), all floating point values are passed as double, aside from long double (assuming the compiler supports long double at all). %f is fine for double. If you use %lf, it may work for double, but it is quite likely to NOT work - it may be undefined (older compilers) or it may interpret the value as a long double (modern compilers) - both of which is likely to render your output incorrect. Better to stick with %f (or %g or %e if you want different formatting preferences).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with double pointers
    By cat1 in forum C Programming
    Replies: 4
    Last Post: 05-06-2009, 01:28 AM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM