#define MY_INT VS const int MY_INT

Possible duplicate:
"static const" vs "#define" in c

When I do this:

#define WEEKDAYS 7

So what:

const int WEEKDAYS = 7;

Any difference between the two? it seems that both do the same thing - sets a constant value for future use in code.

+5
source share
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
}
+6
source
#define WEEKDAYS 7

Replaces all occurrences of a word WEEKDAYSin the source file with the number 7.

const int WEEKDAYS = 7;

Defines the actual constant represented by 7, which you can get in your code.

+2
source

All Articles