Pages: [1] 2   Go Down
Author Topic: The difference between #define and const  (Read 4238 times)
0 Members and 1 Guest are viewing this topic.
Japan
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone.
I'm wondering what is the differences between
Code:
#define RedLed 4
and
Code:
const int RedLed = 4;

thank you in advance.
Logged

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

One defines an integer whose value can not change.

The other defines a symbol substitution operation that the pre-compiler will perform, prior to the compiler being invoked.

In general, they can be used interchangeably. There are subtle differences, but in 99.9% of sketches, the differences do not matter.
Logged

Italy
Offline Offline
Full Member
***
Karma: 0
Posts: 127
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone.
I'm wondering what is the differences between
Code:
#define RedLed 4
and
Code:
const int RedLed = 4;

thank you in advance.

With the define, before your program is compiled into machine runnable form, all instances of "RedLed" in your text are replaced with the text "4".

With the const int you are saying "I have an object, which is an integer, and which cannot be changed, and its value is 4"

In many cases the result is the same, but define can be used to create "macros" and more complicated doodahs. Usually when you use a define the name you give it is in upper case, which makes it easier to recognise as a define inside your sources:


#define REDLED 4
...
if (led == REDLED) {...


In your case the more modern way would be to use the const int. I think define was used a lot in the past because ints could not be forced to be constant (in older compilers).

So in your case I would use const int. In fact, though many would disagree, I'd write:

const int ikRedLed = 4 ;

which tells me that I have an integer konstant (ik).





Logged

Owen F. Ransen

Japan
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul, thanks ofransen.
So, if we could just automatically replace a word like ikRedLed with a value, then why would we use const int ikRedLed = 4?
#define is shorter to write.
And when you say const int is the modern way, then i suppose there must be something more beneficial using const int?
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I understand it correctly...

Code:
constant int TheValueOne=1;

will take up memory

Code:
#define TheValueOne 1

doesn't take up any space

so I guess it saves a byte or two

can't work out why "constant  int" is "more modern" though?
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

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

Quote
can't work out why "constant  int" is "more modern" though?
Because it supports type-checking, which a macro doesn't.
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.

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can't work out why "constant  int" is "more modern" though?
Because it supports type-checking, which a macro doesn't.

aha (light comes on)
cheers!
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Japan
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 aha! so that's the difference.
Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because it supports type-checking, which a macro doesn't.
A macro in its simplest form is just a mechanism for text substitution, but this should not prevent you from generating type qualified code.
Code:
#define RedLed (int)4
Logged

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

Code:
#define RedLed ((int)4)
;-)
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.

Japan
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define RedLed ((int)4)
ouch!!! i thought i can sum the difference already. i was wrong, they can be the same.  smiley-lol
I'll just use #define when i don't need to check the type. at least its shorter to write, and seems to save memory.

Thanks guys, these enlightens.
Logged

0
Offline Offline
Faraday Member
**
Karma: 24
Posts: 3496
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@MartinGS: that's not exactly the same as a constant:

Code:
int const *  test = &RedLed;

If RedLed is a "const int" thist compiles. If it is ((int) 4) it won't. That is with the macro approach you get type checking and do not allocate memory. With a const you can reference but you pay allocating memory.
« Last Edit: July 02, 2011, 09:05:19 am by Udo Klein » Logged

Check out my experiments http://blog.blinkenlight.net

Italy
Offline Offline
Full Member
***
Karma: 0
Posts: 127
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I understand it correctly...

Code:
constant int TheValueOne=1;

will take up memory

Code:
#define TheValueOne 1

doesn't take up any space

so I guess it saves a byte or two

can't work out why "constant  int" is "more modern" though?

That is not true, and depending on the compiler a define may take up more memory than a const. The thing is that the define sort of creates a copy (maybe several copies if you use it a lot) of the value, whereas the const int is a single object. Think about it, where is the 4 stored? It must be in the memory somewhere.

I'm no Arduino compiler expert, but it could be that #define is faster when it comes to execution.
Logged

Owen F. Ransen

Italy
Offline Offline
Full Member
***
Karma: 0
Posts: 127
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul, thanks ofransen.
So, if we could just automatically replace a word like ikRedLed with a value, then why would we use const int ikRedLed = 4?
#define is shorter to write.
And when you say const int is the modern way, then i suppose there must be something more beneficial using const int?


int is a single object, hence in some circs better to use than storing the same value allover the place in your program.

So given that int is sometimes better than #define, why use const? Because we are fallible programmers.

// at the start of the program you may have..
int RedLed =4 ; // This should never change
int RedLedLevel = 22 ; // Brightess of the led which can change

...while programming late Friday night after too much cider I do this:

// Increase brightness...
RedLed = RedLed + 10 ;


Spot the drunken error? Now if you do this:

// at the start of the program you may have..
const int RedLed =4 ; // This should never change
int RedLedLevel = 22 ; // Brightess of the led which can change

...while programming late Friday night after too much cider I do this:

// Increase brightness...
RedLed = RedLed + 10 ;


(note the const) the compiler will complain and fail when you try to change the value of RedLed. If you know something should remain constant, and you know that you make mistakes sometimes, then make it a const object. It is a bit more typing, but it has saved my bacon in the past!

The error message you'll get will be something like:

Serial_Test:18: error: assignment of read-only variable 'RedLed'










 
Logged

Owen F. Ransen

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

Quote
Think about it, where is the 4 stored? 
Usually as an immediate constant as part of the instruction that references it.
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.

Pages: [1] 2   Go Up
Jump to: