What are the advantages of Macros over External(Global) variables?

Recommended Answers

All 4 Replies

That's like asking what are the advantages of Apples over Video Games. They are two different things.

commented: why bother -1

An external variable has a specific type. Therefore the compiler will do type-checking when compiling. You can also assign a value to a global variable at run-time.

As WaltP said, Macros are very different. When a Macro is referenced, the compiler simply copies the value and pastes it in the code where you've used it (before compiling). It is equivalent to using a Find & Replace tool in a text editor. It does allow for some additional functionality however, such as #ifdef/#ifndef, #undef and functions. Although those are somewhat dependent on the compiler.

Macros and External Variables are really two different things:-
Macros are preprocessor directive means they are executed before the actual compilation of code begins.They are started with #define.

External Variables are used for declaration of variables(local), they can be defined any where in the programs.By default value stored in global is 0 and declared as int type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.