Pages: [1]   Go Down
Author Topic: How does define work?  (Read 407 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 268
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy

I got a strange behaviour when I use this code:

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.


Code:
#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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24361
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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)
« Last Edit: March 04, 2013, 05:26:55 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
« Last Edit: March 04, 2013, 05:32:57 am by majenko » Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24361
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote from: 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.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 268
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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).
Logged

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 268
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thx a lot to all of you explaining me the problem and the difference among the const and define statement!!!

Andy
Logged

Pages: [1]   Go Up
Jump to: