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

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).

thats great, thanks

Alex

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.