1

Hi,

Simple question but why would someone use #define.
Whats the difference between;

#define sum 1 and const int sum = 1;

both can't be changed and anywhere you sum its going to be 1!.

Many thanks

Alex

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by Alexkid
1

The first is a preprocessor directive, before the compiler compiles your code, it will go through and replace sum with 1. The second declares a variable in memory to hold that quantity. I'm sure it can be argued as to which is best, but the "const int" is probably more common in C++ (when it comes to numeric constants).

Edited by jonsca: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.