Thread: double and float array type

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    double and float array type

    Hi Guys,
    So i am using a loop to fill an array from user input.
    Whats the difference between a float and a double because I am not worried:
    Code:
    double init_T[4]={0};
    	for (i = 0; i <= 4; i++)
    	{
    	    //init_T[i] = i+2;
    	    scanf("%f",&init_T[i]);
    	}
    	for (i = 0; i <= 4; i++)
    	{
    	    printf("%f\n",init_T[i]);
    	}
    Input 1 2 3 4 5
    Output
    1
    2
    3
    4
    5
    then change the array to a type double and Output becomes:
    0.00
    0.00
    0.00
    0.00
    0.56556
    Why does the double change everything?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A double is larger than a float, so needs a different format for scanf() - it is rather critical that scanf() know the actual size of the variable being read. %f is for float. This needs to be modified with a l (letter L) prefix for double (i.e. %lf). The prefix is also need for other floating point formats (%e versus %le, %g versus %lg, etc).

    The l modifier is not required for printf() when printing floating point types (as a float value is implicitly promoted to double when passed by value).

    If you want to work with long double types, then the L format specifier is needed for both scanf() and printf() - %Le, %Lf, %Lg.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2009, 10:26 PM
  2. Replies: 17
    Last Post: 11-22-2008, 03:40 AM
  3. C - float and double data-type question..
    By ShadeS_07 in forum C Programming
    Replies: 10
    Last Post: 07-14-2008, 06:06 AM
  4. use of float type array in structures
    By vpsingh25 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-02-2003, 01:47 PM
  5. Help! Double-type array makes errors
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2001, 12:41 AM

Tags for this Thread