Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the #define directive in C++?

What is the role of the #define directive?

like image 872
user336671 Avatar asked May 10 '10 20:05

user336671


People also ask

What is the purpose of the word the?

The word the is considered a definite article because it defines the meaning of a noun as one particular thing. It's an article that gives a noun a definite meaning: a definite article. Generally, definite articles are used to identify nouns that the audience already knows about.

Is it the purpose for or the purpose of?

So "purpose of" describes a property or capacity of a shoe, where "purpose for" describes what might be done with a shoe. Show activity on this post. You'd say a/the cause of something to refer to its cause.

What is purpose and example?

Purpose is defined as to plan or intend to do something. An example of purpose is someone deciding they will start saving 10% of their income. verb. 5. An object to be reached; a target; an aim; a goal.

What is the purpose of the sentence?

Declarative sentences, or declarations, convey information or make statements. Interrogative sentences, or questions, request information or ask questions. Imperative sentences, or imperatives, make commands or requests. Exclamatory sentences, or exclamations, show emphasis.


1 Answers

#define is used to create macros in C and in C++. You can read more about it in the C preprocessor documentation. The quick answer is that it does a few things:

  1. Simple Macros - basically just text replacement. Compile time constants are a good example:

    #define SOME_CONSTANT 12
    

    simply replaces the text SOME_CONSTANT with 12 wherever it appears in your code. This sort of macro is often used to provide conditional compilation of code blocks. For example, there might be a header included by each source file in a project with a list of options for the project:

    #define OPTION_1
    #define OPTION_2
    #undef  OPTION_3
    

    And then code blocks in the project would be wrapped with matching #ifdef/#endif# blocks to enable and disable those options in the finished project. Using the -D gcc flag would provide similar behaviour. There are strong opinions as to whether or not this method is really a good way to provide configuration for an application, however.

  2. Macros with arguments - allows you to make 'function-like' macros that can take arguments and manipulate them. For example:

    #define SQUARE(x)  ((x) * (x))
    

    would return the square of the argument as its result; be careful about potential order-of-operations or side-effect problems! The following example:

    int x = SQUARE(3);     // becomes int x = ((3) * (3));
    

    will works fine, but something like:

    int y = SQUARE(f());   // becomes int y = ((f()) * (f()));
    

    will call f() twice, or even worse:

    int z = SQUARE(x++);   // becomes int z = ((x++) * (x++));
    

    results in undefined behaviour!

    With some tools, macros with arguments can also be variadic, which can come in handy.

As mentioned below in the comments, overuse of macros, or the development of overly complicated or confusing macros is considered bad style by many - as always, put the readability, maintainability, and debuggability of your code above 'clever' technical tricks.

like image 84
Carl Norum Avatar answered Nov 15 '22 06:11

Carl Norum