answersLogoWhite

0


Best Answer

The #define preprocessor directive is severely overused in my opinion.

Often, you will see programmers write something like this:

# define MAX(a, b) (((a) > (b))? (a) : (b))

However doing so is rather dangerous, because the preprocessor replaces things textually.

This means that if you pass in a call to a function, it may happen twice, for example:

MAX(++i, j) will be expanded to

(((++i) > (j))? (++i) : (j))

which is bad.

A much safer (and more common) example is that people will use #define to create a constant variable:

#define MYCONST 10

This too can cause problems if another file depends on the constant and certain other conditions are met.

A safer alternative is to declare a const variable.

The one advantage of using #define for literal constants is that the number will not be stored in memory attached to an object like a const variable will.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why and when do you use the define directive?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a preprocessor directive to accomplish Define symbolic constant YES to have the value 1?

I'm not exactly sure that this is a question, but here you are:#define YES 1


How would we define new header file using condition inclusion?

Conditional inclusion is a trick that some languages must use to prevent multiple declarations of the same symbols. A prime example would be "C++", that typically breaks the interface and implementation definitions into .h and .cpp files, respectively. Since it is common that the .h file will be included in numerous .cpp files that define symbols needed by the implementation, a common method is to use the #define preprocessor directive. For your file "MyClass.h":#ifndef __MyClass_H__#define __MyClass_H__... class definition goes here ...#endif


What is the difference between define and undefine?

# define and # undef are compiler directives in C and C++. The # define directive creates a definition for something that will be replaced multiple times in the code. For example: # define HELLO 5 Creates an association between HELLO and replaces it with 5 in the code (for the compiler only). The # undef (undefine) counterpart removes the definition from what the compiler sees. It is usually specified when either the definition should no longer be used or when the definition needs to change.


What is the use of define key word in c?

Actually, the preprocessor is not part of the C compiler, but here you are: #define is meant to define symbols. Examples #define NULL ((void *)0) #define getchar() getc(stdin)


What type of language is directive?

Assembler.

Related questions

What is a pre-processor directive?

#include is a preprocessor directive. So is #define, #if, etc


How do you set global variables to on?

You can use the preprocessor directive #define, or you can describe a variable in the body of main(). With the preprocessor directive you can make the variable accessible even out of your current project.


Write a preprocessor directive to accomplish define macro square volume that computes the volume of a square the macro takes one argument?

Pick one: #define SQUARE_AREA(A) ((A) * (A)) #define CUBE_VOLUME(A) ((A) * (A) * (A))


Write a preprocessor directive to accomplish Define symbolic constant YES to have the value 1?

I'm not exactly sure that this is a question, but here you are:#define YES 1


How do you use the word directive in s sentence?

The Prime Directive is a basic rule in Star Trek.His directive is that we seek an amiable solution. What is your directive, boss?


What is the difference between a DoD manual and a DoD directive?

Directives establish or describe policy, programs, and organizations. They also define missions, provide authority, and assign responsibilities. Instructions include more detail on how to implement a directive.


Which DoD Directive establishes policy for the use of commercial wireless devices?

DOD 8100.2


How do you declare constant in Turbo C?

In C there is no constant with a name. It is done with the preprocessor directive of #define as in #define pi 3.1416 The preprocesor substitutes every occurance of word pi (with blanks on either side) with 3.1416


What is directive and non-directive approach to rural development?

comprehensively describe the directive and non-directive approach to rural development


Why are random numbers useful in PHP?

Random numbers can be used for many different things. Personally i use the rand() directive for security measures to define a session identifier. Also i use the rand feature (sometimes) for user authentication while using captcha images. basically, when you need a random number, use rand() ;)


How would we define new header file using condition inclusion?

Conditional inclusion is a trick that some languages must use to prevent multiple declarations of the same symbols. A prime example would be "C++", that typically breaks the interface and implementation definitions into .h and .cpp files, respectively. Since it is common that the .h file will be included in numerous .cpp files that define symbols needed by the implementation, a common method is to use the #define preprocessor directive. For your file "MyClass.h":#ifndef __MyClass_H__#define __MyClass_H__... class definition goes here ...#endif


How does the information operations directive afdd 2-5 2004 define information operations?

Maximizing and maintaining security of classified and unclassified information systems and vital information assets.