What's wrong with this code?

This is a discussion on What's wrong with this code? within the C Programming forums, part of the General Programming Boards category; I'm a beginner. This is an example code from my book and it keeps producing a warning (warning C4244: '=' ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    49

    What's wrong with this code?

    I'm a beginner. This is an example code from my book and it keeps producing a warning (warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data):

    Code:
    #include <stdio.h>
    #include <string.h>
    #define DENSITY 62.4
    int main()
    {
    	float weight, volume;
    	int size, letters;
    	char name[40];
    
    	printf("Hi! What's your first name?\n");
    	scanf("%s", name);
    	printf("%s, what's your weight in pounds?\n");
    	scanf("%f", &weight);
    	size = sizeof name;
    	letters = strlen(name);
    	volume = weight / DENSITY;
    	printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume);
    	printf("Also, your first name has %d letters,\n", letters);
    	printf("and we have %d bytes to store it in.\n", size);
    	return 0;
    }
    Can someone please tell me what the problem with this code is? I've looked it over carefully and I can't find out what's wrong with it.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The problematic line is here:
    Code:
    #define DENSITY 62.4
    The preprocessor replaces text matching DENSITY with the text 62.4. The compiler then analyzes this. The line read by the compiler is thus:
    Code:
    volume = weight / 62.4;
    Now, in an expression, the type of the result is the type of the longest, floatiest operand. The type of weight is float, but the type of a floating-point literal is double. double is floatier(*) than float, so the result of the expression weight / 62.4 is double. You then attempt to assign a temporary value with the type double to a variable of type float. This is an assignment of a wider type to a narrower type and requires a diagnostic.

    The solution is to either cast the result of the expression to (float), or cause the floating-point literal to be float by appending F or f:
    Code:
    #define DENSITY 62.4f
    (*)
    My best code is written with the delete key.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    floatiest
    floatier
    You could also say it has higher/highest precision...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You could also say it has higher/highest precision...
    My way is more fun though.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    49
    Oh, ok. Thanks! I wonder why my book didn't cover this

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I wonder why my book didn't cover this
    It your book is worth anything it will cover this when describing floating-point constants and type conversion rules.

    K&R2 covers it in section 2.3, page 37 and section 2.7, page 42.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21