answersLogoWhite

0

The simple answer is that directives are symbols that instruct or direct the preprocessor, while declarations are C++ symbols that are processed by the C++ compiler.

The preprocessor and the compiler are essentially two completely separate operations. The preprocessor is tasked with modifying your source files in order to produce temporary intermediate source files which the compiler can process more efficiently. These intermediate files contain nothing but C++ code, thus the preprocessor's primary job is to strip out all the comments from your source files.

Preprocessor directives are used to instruct the preprocessor in all other modifications, such as where and when to include one file within another and which sections of code are valid for a particular build. This is achieved by #define directives, in conjunction with #ifdef, #else and #endif directives.

The #define directive defines a symbol. As the preprocessor encounters these directives, the symbols are placed in a definition table, which includes all the symbols that were initially passed via the command line. Some symbols have no value assigned to them; their presence in the table is sufficient to know they are defined, and the preprocessor can simply act according to whether a symbol is currently defined or not. The #undef directive is the antidote to #define, allowing symbols to be undefined (removed from the table) by the preprocessor.

However, #define can also be used to assign a value to a symbol. This value acts a text replacement for the symbol, thus wherever the symbol is encountered within your code (outwith any other directives), the symbol is replaced with the text value associated with that symbol. These are known as macros.

#define can also be used to declare macro functions, where the text replacement includes one or more arguments within parenthesis, followed by C++ code that processes those arguments. Thus all occurrences of the symbol in your code must include an argument list with the same number of arguments as the macro directive. The preprocessor then inline expands the macro function, replacing its arguments with the formal arguments provided by your code.

The problem with #define in general is that it is not a C++ statement, it is a macro, and macros are not subject to the strong-typing that is fundamental to C++. So while you can use #define to declare literal constants, those constants are untyped. Moreover, since the compiler never sees the macro definition (it is stripped from the intermediate file), any errors that occur as a result of a macro expansion cannot be sourced back to the macro, which makes debugging difficult at best. In the case of macro functions, the debugger cannot help you at all, you're completely on your own.

Thus when you have the option to use directives or declarations, always go with a declaration. C++ code is type safe, can be easily debugged, is easier to maintain, and usually results in much smaller compiled code. Only use the preprocessor to generate code that would be difficult or impossible to generate with the compiler alone. In other words, use directives specifically to direct the preprocessor. Never rely on them to generate C++ code unless you have no other option, or it would greatly simplify a far more complex C++ implementation.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Difference between Percentage of Completion method and Completed Contract method?

Difference between Percentage of Completion method and Completed Contract method?


What is the difference between simplex and dual simplex method?

what is difference between regular simplex method and dual simplex method


What are the difference between roster method and rule method?

what is the difference between roster method and rule method


What is directive method?

Its a method to determine high resistance. Hope that helps.


What is the difference between Modified accrual and Full accrual method?

What is the difference between Modified accrual and Full accrual method?"


What is the method to find the difference between two sets in Python?

To find the difference between two sets in Python, you can use the "-" operator or the difference() method.


What is the difference between scientific method and socratic method?

ice baby


What is the difference between event and method?

this is not the way to question me when i question you ..... and the major difference between event and method is that event starts with alphabet E and method starts with alphabet M ok.....


Difference between method study and work measurement?

Differentiate between work study and method study.


What are the difference between heat method and chemical method in sanitation?

im lovin it


What is a qualitative method?

the difference between goals and objectives


What is difference between skills and method in science?

well the method is the steps on how you do it and the skills are sort of how you do it i think