#define is a preprocessor directive. Things defined by #define are replaced by the preprocessor before compilation begins.
const variables are actual variables like other normal variable.
The big advantage of const over #define is type checking. We can also have poitners to const varaibles, we can pass them around, typecast them and any other thing that can be done with a normal variable. One disadvantage that one could think of is extra space for variable which is immaterial due to optimizations done by compilers.
In general const is a better option if we have a choice. There are situations when #define cannot be replaced by const. For example, #define can take parameters (See this for example). #define can also be used to replace some text in a program with another text.
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between const char *p, char * const p and const char * const p
- Difference between const int*, const int * const, and int const *
- “static const” vs “#define” vs “enum”
- Function overloading and const keyword
- Const Qualifier in C
- Why copy constructor argument should be const in C++?
- C++ | const keyword | Question 1
- C++ | const keyword | Question 2
- C++ | const keyword | Question 3
- C++ | const keyword | Question 5
- C++ | const keyword | Question 5
- How to modify a const variable in C?
- typedef versus #define in C
- Similarities and Differences between Ruby and C language
- Difference and Similarities between PHP and C
- What are the differences between bitwise and logical AND operators in C/C++?
- Difference Between malloc() and calloc() with Examples
- What is the difference between single quoted and double quoted declaration of char array?
- Difference between "int main()" and "int main(void)" in C/C++?
- Difference between ++*p, *p++ and *++p

