Possible Duplicate:
Why does this integer division yield 0?
I have a C header file,which contains various constant values used in my project.The values are defined using #define (e.g. #define MAX_DATA_VAL 5).
When I use this value in another file for division,I get the wrong answer.
//- In file MyParameters.h
#define MAX_DATA_VAL 5
#define MIN_DATA_VAL -5
// - In file MyFunctions.c
#include "MyParameters.h"
void Function()
{
float temp = 0;
temp = 2/MAX_DATA_VAL; // Wrong Output
}
The value of temp remains 0 (checked while debugging).However if I did:
temp = 0;
float a = MAX_DATA_VAL;
temp = 2/MAX_DATA_VAL; // Correct Output
I get the right answer!!…Could anyone please explain what’s happening…
Thanks in advance 🙂
MAX_DATA_VAL is being treated as an integer and thus your division was doing integer division (rounds down to nearest whole number). To make sure it knows it’s a float, do: