system
March 4, 2013, 10:13am
1
Hy
I got a strange behaviour when I use this code:
#define alt 10000
void setup(){
Serial.begin(115200);
Serial.println(alt);
if (55000 >= (alt * 100)){
Serial.println("Yes");
}
else{
Serial.println("no");
}
}
void loop(){ }
Why Is the Output yes? Since 10000 * 100 is greater then 5500??
Whe I use following code it works:
Instead of *100 I use *100.
#define alt 10000
void setup(){
Serial.begin(115200);
Serial.println(alt);
if (55000 >= (alt * 100.0)){
Serial.println("Yes");
}
else{
Serial.println("no");
}
}
void loop(){ }
I don't get it!!
Andy
system
March 4, 2013, 10:23am
2
Since 10000 * 100 is greater then 5500?
No, it isn't - it is less than zero. (16 bit arithmetic, remember)
Edit: Sorry, not less that zero, but still less than 55000 (16960, I think)
system
March 4, 2013, 10:26am
3
Try forcing an integer size:
#define alt 10000UL
That will define "alt" as being an unsigned long value of 10,000.
This is why it is generally better to use "const" for numeric values instead of #define as you specify a data type for the compiler to work with:
const unsigned long alt = 10000;
system
March 4, 2013, 10:28am
4
Personal Message (Online)
Re: How does define work?
« Reply #2 on: Today at 11:26:34 »
Bigger Bigger Smaller Smaller Reset Reset Reply with quoteQuote Modify messageModify Remove messageRemove Split TopicSplit Topic
Try forcing an integer size:
Code:
#define alt 10000UL
That will define "alt" as being an unsigned long value of 10,000.
This is why it is generally better to use "const" for numeric values instead of #define as you specify a data type for the compiler to work with:
Code:
const unsigned long alt 10000;
But slip in an assignment, to keep the compiler happy.
system
March 4, 2013, 10:33am
5
AWOL:
Majenko:
const unsigned long alt 10000;
But slip in an assignment, to keep the compiler happy.
I haven't finished my first coffee of the day yet.
system
March 4, 2013, 10:44am
6
wow thx a lot
so const is similair to define you still can't change the value after it is "defined" in the code?
Andy
system
March 4, 2013, 10:46am
7
GekoCH:
wow thx a lot
so const is similair to define you still can't change the value after it is "defined" in the code?
Andy
That is correct. Once compiled the end result is the same - the number is just a number stored in progmem in the same way as #define - the resultant code is (usually) exactly the same.
system
March 4, 2013, 11:08am
8
so const is similair to define you still can't change the value after it is "defined" in the code?
Vaguely similar. The advantage, as you've seen is that the value part has a type associated with it, unlike the value part of a #define statement. That type lets the compiler point out more mistakes you might make, and lets the compiler generate correct code when the code to be generated depends on the type (as in math operations).
system
March 4, 2013, 11:41am
9
thx a lot to all of you explaining me the problem and the difference among the const and define statement!!!
Andy