What is the difference between #define and const in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between #define and const in c++

#define vs const in c++

10th Jul 2019, 4:50 PM
DONALSOFT
DONALSOFT - avatar
6 Answers
+ 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.
10th Jul 2019, 4:54 PM
Airree
Airree - avatar
+ 1
I did.
10th Jul 2019, 5:08 PM
Airree
Airree - avatar
+ 1
Then I don't think I can help you anymore
10th Jul 2019, 5:11 PM
Airree
Airree - avatar
+ 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.
10th Jul 2019, 9:03 PM
Ipang
0
Airree sorry I don't think you answered my question the question is the difference between #define and const
10th Jul 2019, 5:08 PM
DONALSOFT
DONALSOFT - avatar
0
Then I don't understand
10th Jul 2019, 5:10 PM
DONALSOFT
DONALSOFT - avatar