Thread: specifier for double type

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    15

    specifier for double type

    Hi,

    I am new to C programming and also to these boards. I started learning C on my own and I have a small question about the specifier to use for double data types. In the following small code I expected that the last printf statement should display 2.5 but instead i am getting 0.000000. Can someone tell me what I am doing wrong?

    Also when using the float data type is it true that when assigning a value it should include a suffix f the value as otherwise it will be automatically converted to double?

    Thank You

    Code:
    #include <stdio.h>
    int main()
    {
          int number = 0;
          double number2 = 2.5;
          printf("The int variable \"number\" has %d bytes\n", sizeof(number));
          printf("The double variable \"number2\" has %d bytes\n", sizeof(number2));
          (double)number = number2;
          printf("%lf", number);
          system("PAUSE");
          return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Emerald View Post
    I am new to C programming and also to these boards. I started learning C on my own and I have a small question about the specifier to use for double data types. In the following small code I expected that the last printf statement should display 2.5 but instead i am getting 0.000000. Can someone tell me what I am doing wrong?
    For printf, use %f to printf doubles (and floats). There is no %lf for printf (only for scanf).

    Also when using the float data type is it true that when assigning a value it should include a suffix f the value as otherwise it will be automatically converted to double?
    If you don't include the "f" suffix, the number is of type double; it's not converted (how can it be, if it was never any other type to begin with?).
    Last edited by Elysia; 08-13-2009 at 07:55 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can't use casting to change the type of a variable you already declared as something else. Once "number" is an int, it will always be an int. Casting would, however, allow you to cast "number2" to an int and assign that, or you could cast "number" to a double and assign that to "number2".

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Emerald View Post
    Hi,

    I am new to C programming and also to these boards. I started learning C on my own and I have a small question about the specifier to use for double data types. In the following small code I expected that the last printf statement should display 2.5 but instead i am getting 0.000000. Can someone tell me what I am doing wrong?
    For scanf, the specifier is "lf". For printf, the specifier is "f".
    Quote Originally Posted by Emerald View Post
    Also when using the float data type is it true that when assigning a value it should include a suffix f the value as otherwise it will be automatically converted to double?
    Literal values that appear in your code (such as 7.5 or 3.4) are doubles unless you tack an f on them (or unless they don't have a decimal point, like 6, in which case they are int). The conversion from double to float usually doesn't cause any problem.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    Thank you all for the replies, it makes much more sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM