1

Now, bear with me, I'm new to . While following an online tutorial for , it mentioned that all #define is used for is to define a constant, like this.

#define RANDOM_CONSTANT 288 

What I'm confused about is, why can't it just be done by creating a variable like this?

int RANDOM_CONSTANT = 288; 

Does #define have any other applicable uses other than defining constants?

6
  • 4
    It has many useful purposes, of which constants is not one of them. What tutorial are you using? Commented Aug 11, 2014 at 21:55
  • @MooingDuck Tutorialspoint Commented Aug 11, 2014 at 21:57
  • Oh, the tutorial says that you can use define for constants to improve readability. Later on down the page, it goes into other uses as well. Ok, that's fine. Commented Aug 11, 2014 at 21:59
  • 2
    Today it is better to use int const RANDOM_CONSTANT = 288;. In early versions of C that wasn't possible and #define was the best option. C++ chose (probably for practical reasons) to use the same preprocessor as C. Commented Aug 11, 2014 at 22:02
  • 2
    You'd want const int RANDOM_CONSTANT = 288; to make the name a constant expression. But as inetknght's answer points out, that doesn't let you use it in a #if expression. Commented Aug 11, 2014 at 22:23

4 Answers 4

2

#define is used to define some of the text substitutions performed by the preprocessor. If you write

#define foo 417 

and then refer to foo in your program, all instances of the identifier foo will be turned into the number 417. (But foo4 will remain as foo4, for instance.)

#define twice(x) x,x 

then an occurrence of twice(417) in your program will turn into 417,417.

If I were you, I wouldn't worry about it too much at this stage.

Sign up to request clarification or add additional context in comments.

Comments

2

#define allows you to change the environment before compile time. So for example, you can use #if RANDOM_CONSTANT will allow you to modify what code gets compiled.

This is highly useful so that you can conditionally compile, for example, debugging features based on whether or not certain things were #defined or not.

Comments

2

#define defines MACROs, not constants. #define, for example, may have certain parameters:

#define A(i) printf("%d", i) 

2 Comments

Consider #define LOG(message) WriteToLog(__FILE__, __LINE__, message) That saves a LOT of typing.
@MooingDuck Yes, as C++ improves #define becomes less important, but it's still not dead. There are things that can be done using #define that are "impossible" otherwise. (and I think your response applies to user3879237's answer, not to my comment)
1

There seem to be two issues to address here:

  1. What is #define useful for?
  2. Why use constants instead of variables?

The answers to which are:

  1. #define can do more complicated things then just replace a number with a name. It can actually take parameters. For example: #define getmax(a,b) a>b?a:b does not merely replace a number with a name.

  2. Two parts: First, constants cannot be edited by the program (hence the name ;) ) so this in a way is a sort of safe guard. Second, it is precompiled not a variable so it takes no storage space and doesn't need to be looked up making it more efficient.

4 Comments

I don't think he's actually asking the second one, only the first question
@MooingDuck He asked "What I'm confused about is, why can't it just be done by creating a variable like this?" So he wants to see why constants have an advantage other variables in this usage.
Hmm, I interpreted it as him saying "when should I use a define as opposed to a constant like this", but rereading, your interpretation is also possible.
You should use parenthesis in your macro definitions because just a>b?a:b won't work well in many situations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.