+ 1
What is the difference between #define and const in c++
#define vs const in c++
6 Respostas
+ 2
#define: the compiler replaces the part of the program it uses it, therefore it is more faster:
#define x 10
printf("%d", x);
gets replaced to:
printf("%d", 10);
constant values are normal values that can't be changed.
+ 1
I did.
+ 1
Then I don't think I can help you anymore
+ 1
#define is a preprocessor directive, whatever expression you set in #define will be used to replace every occurrence of the identifier.
Syntax:
#define <identifier> <expression>
Before compilation begins, every occurrence of <identifier> in your code shall be replaced by the <expression> it represents. Note that preprocessor directive doesn't need semicolon at end.
const is a qualifier, it adds an attribute to an identifier that marks the identifier as read-only, meaning an attempt to modify its value (data) will result in failure and warning or possibly error.
Syntax:
const <type> <identifier> <value>
P.S. Please lookup the net for more descriptive and clear definition of preprocessor directive and const qualifier, my attempt to answer may be mistaken or inappropriate from lack of in-depth knowledge.
0
Airree sorry I don't think you answered my question the question is the difference between #define and const
0
Then I don't understand