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?
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. With WIDTH
, it will be just replaced with a constant 10
everywhere, so the expression ++WIDTH
doesn't make any sense. Ono the other side, you can declare an array with WIDTH
items, whereas you cannot declare an array with width
items.
Summing it up: the value of WIDTH
is known at compile time and cannot be changed. The compiler doesn't allocate memory for WIDTH
. On the contrary, width
is a variable with initial value 10, its further values are not known at compile time; the variable gets its memory from the compiler.
What is the difference between two?
The first is an Macro while second is an Variable declaration.
#define WIDTH 10
is a preprocessor directive that allows you to specify a name(WIDTH
) and its replacement text(10
). The preprocessor parses the source file and each occurrence of the name is replaced by its associated text. The compiler never actually sees a macro name at all, what it sees is the replaced text.
The variable declaration is evaluated by the compiler itself. It tells the compiler to declare a variable named width
and of the type int
and also initializes it with a value 10
.
The compiler knows this variable by its own name width
.
Which one should you prefer? And Why?
Usually, it is recommended to use compile time constant variables over #define
.
So Your variable declaration should be:
const int width = 10;
There are a number of reasons for selecting compile time constants over #define
, namely:
Scope Based Mechanism:
The scope of #define
is limited to the file in which it is defined. So, #defines
which are created in one source file are NOT available in a different source file. In short, #define
s don't respect scopes.Note that const
variables can be scoped.They obey all scoping rules.
Avoiding Weird magical numbers during compilation errors:
If you are using #define
those are replaced by the pre-processor at time of precompilation So if you receive an error during compilation, it will be confusing because the error message wont refer the macro name but the value and it will appear a sudden value, and one would waste lot of time tracking it down in code.
Ease of Debugging:
Also for same reasons mentioned in #2, while debugging #define
would provide no help really.
WIDTH
is a macro which will be replaced with the value (10) by the preprocessor whereas width
is a variable.
When you #define a macro (like WIDTH here), the preprocessor will simply do a text-replacement before the program is passed to the compiler. i.e. wherever you used WIDTH
in your code, it'll simply be replaced with 10
.
But when you do int width=10
, the variable is alive
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With