What is the point of #define
in C++? I've only seen examples where it's used in place of a "magic number" but I don't see the point in just giving that value to a variable instead.
Use “why” in a sentenceWhy don't we go swimming this afternoon? Why don't you try a taste of this? I can't figure out why he didn't tell the truth. Could you please tell me again why you are late?
The conjunctions and and or connote very different meanings. In particular, and suggests the joint consideration of two concepts, whereas or suggests exclusivity.
For: purpose We use for to talk about a purpose or a reason for something: I'm going for some breakfast. I'm really hungry. She leaves on Friday for a 15-day cruise around the Mediterranean.
The words “do” and “does” mean the same, that is, “to carry out or to perform an action.” “Do” is used in the first and second persons; “does” is used in the third person. 3. “Do” is used when referring to two or more persons or things while “does” is used when referring to a single person or thing.
The #define
is part of the preprocessor language for C and C++. When they're used in code, the compiler just replaces the #define
statement with what ever you want. For example, if you're sick of writing for (int i=0; i<=10; i++)
all the time, you can do the following:
#define fori10 for (int i=0; i<=10; i++) // some code... fori10 { // do stuff to i }
If you want something more generic, you can create preprocessor macros:
#define fori(x) for (int i=0; i<=x; i++) // the x will be replaced by what ever is put into the parenthesis, such as // 20 here fori(20) { // do more stuff to i }
It's also very useful for conditional compilation (the other major use for #define
) if you only want certain code used in some particular build:
// compile the following if debugging is turned on and defined #ifdef DEBUG // some code #endif
Most compilers will allow you to define a macro from the command line (e.g. g++ -DDEBUG something.cpp
), but you can also just put a define in your code like so:
#define DEBUG
Some resources:
Mostly stylistic these days. When C was young, there was no such thing as a const variable. So if you used a variable instead of a #define
, you had no guarantee that somebody somewhere wouldn't change the value of it, causing havoc throughout your program.
In the old days, FORTRAN passed even constants to subroutines by reference, and it was possible (and headache inducing) to change the value of a constant like '2' to be something different. One time, this happened in a program I was working on, and the only hint we had that something was wrong was we'd get an ABEND (abnormal end) when the program hit the STOP 999
that was supposed to end it normally.
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