It’s a simple question, but why would someone use #define to define constants?
What’s the difference between
#define sum 1 and const int sum = 1;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
#definehas many different applications, but your question seems to be about one specific application: defining named constants.In C++ there’s rarely a reason to use
#defineto define named constants.#defineis normally widely used in C code, since C language is significantly different from C++ when it comes to defining constants. In short,const intobjects are not constants in C, which means that in C the primary way to define a true constant is to use#define. (Also, forintconstants one can use enums).