Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use #define instead of a variable

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.

like image 235
Greener Avatar asked May 14 '11 21:05

Greener


People also ask

Why are sentences used?

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?

Why is and/or used?

The conjunctions and and or connote very different meanings. In particular, and suggests the joint consideration of two concepts, whereas or suggests exclusivity.

Why do we use for in English?

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.

Why does or why do?

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.


2 Answers

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:

  1. Wikipedia article
  2. C++ specific site
  3. Documentation on GCC's preprocessor
  4. Microsoft reference
  5. C specific site (I don't think it's different from the C++ version though)
like image 59
supercheetah Avatar answered Oct 08 '22 00:10

supercheetah


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.

like image 44
Paul Tomblin Avatar answered Oct 08 '22 00:10

Paul Tomblin