Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define MY_INT VS const int MY_INT [duplicate]

Possible Duplicate:
“static const” vs “#define” in c

When I do this :

#define WEEKDAYS 7

and that :

const int WEEKDAYS = 7;

Any difference between them ? seems that both do the same thing - sets a constant value for future usage within the code .

like image 364
JAN Avatar asked Aug 25 '12 16:08

JAN


2 Answers

#define WEEKDAYS 7

void f() {
    int WEEKDAYS = 3; // error
}

const int WEEKDAYS_CONST = 7;

void g() {
    int WEEKDAYS_CONST = 3; // okay: local scope for WEEKDAYS_CONST
}
like image 152
Pete Becker Avatar answered Sep 28 '22 10:09

Pete Becker


#define WEEKDAYS 7

Replaces all occurrence of the word WEEKDAYS in your source file with the digit 7.

const int WEEKDAYS = 7;

Defines an actual constant represented by 7 that you can access in your code.

like image 40
Qiau Avatar answered Sep 28 '22 10:09

Qiau