I cannot understand what is the difference between:
#define WIDTH 10
and
int width = 10;
What are the benefits of using the first or the second?
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.
Well, there is a great difference. You can change the value of
width, you can take its address, you can ask for its size and so on. WithWIDTH, it will be just replaced with a constant10everywhere, so the expression++WIDTHdoesn’t make any sense. Ono the other side, you can declare an array withWIDTHitems, whereas you cannot declare an array withwidthitems.Summing it up: the value of
WIDTHis known at compile time and cannot be changed. The compiler doesn’t allocate memory forWIDTH. On the contrary,widthis a variable with initial value 10, its further values are not known at compile time; the variable gets its memory from the compiler.