+ 5

What is the difference between #define and const in C++?

#define MAX 10 and const int MAX = 10 have the same value but are you supposed to use one over the other? Are there pros and cons to them? Or is it just user preference?

1st Jun 2018, 11:45 PM
Fabio Rocha
Fabio Rocha - avatar
3 Réponses
+ 7
The one place that makes a huge difference between them is: int array[MAX]; The define is legal to be used in this case. While some compiler's do allow it, the usage of the constant is not supported by the current standard.
2nd Jun 2018, 1:32 AM
John Wells
John Wells - avatar
+ 5
Three is no big difference between them, there are in compile time and how to check data types and handle there values! read this : https://stackoverflow.com/questions/6442328/what-is-the-difference-between-define-and-const
2nd Jun 2018, 12:37 AM
Mahdi Salek
Mahdi Salek - avatar