answersLogoWhite

0


Best Answer

Macros are preprocessor statements which will have specific set of instructions which are repeated in source code for several times and which wil be replaced at every call made.

1. Reduce source code length

2. Prog more readable.

3. Any modification to instructions in macro reflects in every call

4. No performance drawback by macros

1. Every call will be replaced and hence internally code length will be large.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

advantage is that it saves memory and time.because with the use of macros the compilation will become faster and it is very easy to change any value in the big program The main advantage of using the macro is the speed of the execution of the program (if macro is not used in the program many times or if the program is small).

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Hi All, I don't know exactly about it, but I want to share what I know. When we compile our file, macros and inline function calls get expanded, that is, calls get replaced with definition. It makes file bulky and consume more processor time. In case of macros, some problem other problems also occur. For example: #define cube(x) (x*x*x); Now answer for cube(4+4); should be 512 but answer will be 40 because it will be expanded like this: 4+4*4+4*4+4 Due to higher precedence of * operator, answer will be 40. If anybody would like to improve or correct (if needed) my answer, it will be my pleasure. Thanks, Sonia Answer: Use #define cube(x) ((x)*(x)*(x));

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Macros are used to provide conditional compilation and inline expansion through text substitution. Macros are processed by the preprocessor not by the compiler. The role of the preprocessor is to modify source code, stripping out all user-defined comments and acting upon preprocessor directives (all lines beginning with a # symbol). The compiler's input is the output of the preprocessor, an intermediate source file that contains nothing but language source (no macros or comments).

The simplest directive is the #include directive where the preprocessor simply inserts the named file in place of the directive (if the file contains preprocessor directives, these are also processed prior to insertion).

The #define directive is used to define a symbol. We call these symbols macros. A macro need not have a value associated with it in which case it is used purely for conditional compilation. If a value is defined, the value can either be an object or a function, and the object or function is used purely for text substitutions.

The most common macro we encounter in C is the NDEBUG macro. It has no value associated with it and is therefore used purely for conditional compilation:

#define NDEBUG

When we define a macro, the preprocessor places the symbol in the macro table and removes the directive. Although there is no value associated with it, its presence within the table is enough to signify it has been defined. That is, it either exists or it does not. We can use this fact to conditionally compile code. We can also use it to conditionally define another macro:

#ifdef NDEBUG

#define assert(CONDITION) ((void)0)

#else

#define assert(CONDITION) ((CONDITION) ? (void)0 : _assert(#CONDITION, __FILE__, __LINE__))

#endif

void f(int* ptr) { assert (ptr!=0);

// use ptr...

}

Here were defining a macro function named assert(). Normally we only use assertions in debug code in order to test our assumptions. If the NDEBUG macro is defined then this means we're not debugging, so the preprocessor should output the following:

void f(int* ptr) {

(void)0;

// use ptr...

}

But if NDEBUG is not defined, then we are debugging and so we want the preprocessor to output the following instead:

void f(int* ptr) {

(ptr!=0) ? (void)0 : _assert(ptr!=0, __FILE__, __LINE));

// use ptr...

}

Note that __FILE__ and __LINE__ are also macros.

The statement, (void)0;, is a noop (non-operation). It simply acts as a placeholder, it does not generate any machine code.

Macro objects are much less useful than macro functions.

#define VALUE 42

This is essentially defining a constant, but there's a perfectly good, type-safe mechanism for defining constants within the language itself:

const int value = 42;

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

All macros are defined with the #define directive:

#define NDEBUG

Note that macro names are always written in uppercase. This is by convention and makes them much easier to spot in code.

Here we've defined the macro NDEBUG. This is a standard macro used by many C libraries to provide conditional compilation of debug code:

void foo (int x) {

#ifndef NDEBUG

if (x==0) printf ("Error in foo(): argument must be non-zero!\n");

#endif

// ...

}

The compiler never sees the macro or the directive, it only sees the code generated by the preprocessor. The #ifndef directive tells the preprocessor to check if NDEBUG is not defined. If it is not defined, the code up to the #endif directive is submitted to the compiler, otherwise it is omitted.

If NDEBUG is not defined, the compiler sees the following:

void foo (int x) {

if (x==0) printf ("Error in foo(): argument must be non-zero!\n");

// ...

}

But if NDEBUG is defined, the compiler sees the following instead:

void foo (int x) {

// ...

}

Typically, the NDEBUG macro is defined by the IDE when compiling production/retail code but is not defined when compiling debug code. Macros can also be defined via the command line.

Macros are often used to define constants:

#define MEANING_OF_LIFE 42

Macros such as this are not recommended as they are not type-safe. If you need a type-safe constant, then use a constant type:

const int meaning_of_life = 42;

Macros can also be used to define functions:

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

void foo (int w, int x, double y, double z) {

int m = MAX (w, x);

double n = MAX (y, z);

}

Again, this is not type-safe, however when used appropriately it can make code easier to read. After preprocessing, the compiler will see the following:

void foo (int w, int x, double y, double z) {

int m = w > x ? w : x;

double n = y > z ? y : z;

}

Note that the compiler never sees the macro so it cannot help you debug any problems within the macro. The onus is entirely upon the programmer to ensure macros are used appropriately and in a type-safe manner.

Note the curious syntax in macro function definitions:

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

All the parenthesis are required. If we omit them we get some curious side-effects:

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

int x = 42, y = 0;

// ...

int z = MAX (++x, y);

The compiler sees the following:

int x = 42, y = 0;

// ...

int z = ++x > y ? ++x : y;

But what we actually wanted was:

int x = 42, y = 0;

// ...

int z = ++x > y ? x : y;

Macros can be a huge time-saver and can help make code more readable, but use them with caution and only when appropriate. Macro functions are always inline expanded and that may or may not be desirable. If a C language facility already exists, then make use of it rather than resorting to macros. Code that makes extensive use of macros or that uses macros to implement a "private" language is usually indicative of a design flaw, either within the program itself or in the C language. If the former, consider redesigning your program to minimise the use of macros. If the latter, consider using C++ where there is much less need for macros.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

# What are macros? what are its advantages and disadvantages? # What are macros? what are its advantages and disadvantages?

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Macros are complicated to write. Someone needs a good knowledge to create them correctly. There is a risk of doing some damage to your data if you make a mistake.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

In computing, there are various reasons that macros are considered useful. For example, you can use them to automate repetitive tasks.

This answer is:
User Avatar

User Avatar

Anonymous

Lvl 1
3y ago

geierburbvcwi

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a macro and its advantages?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the threats and advantages for fair and lovely cream in macro environment?

It is suitable for my skin But it increases the pores


What is the difference between executing a macro and calling a macro?

Calling a macro loads the macro into memory, while executing the macro runs the macro.


What is macro call?

Nested macro calls refer to the macro calls within the macros. A macros is available within other macro definitions also. In the scenario when a macro call occurs, which contains another macro call, the macro processor generates the nested macro definition as text and places it on the input stack. The definition of the macro is then scanned and the macro processor complies it.


What is nested macro calls?

Nested macro calls refer to the macro calls within the macros. A macros is available within other macro definitions also. In the scenario when a macro call occurs, which contains another macro call, the macro processor generates the nested macro definition as text and places it on the input stack. The definition of the macro is then scanned and the macro processor complies it.


What is enclosed between a macro header statement and a macro end statement?

The macro content.


Are oats micro are macro nutrients?

Macro


Is inflation Macro or Micro economics?

MACRO


What are macro buttons?

They are buttons (icons) you can put in a spreadsheet and attach to a macro to make it easier to execute a macro.


Toyota macro environment factors?

macro factors of toyta prius macro factors of toyta prius


What is another word for macro culture?

religion is a macro culture, a macro culture is a sub division of a culture


What is the macro-molecule that makes up an enzyme?

Macro?


Macro in system software?

Macro in system software?