what is the disadvantage of using macros?



what is the disadvantage of using macros?..

Answer / vadivel t

Though, macro has advantages... it hav few disadvantages
too...
------------------------------------------------------------
1.1st disadvantage is, In debugging time u can't see the
value of the macro assigned to it.

So, u have to have ur source file, to fine out the value of
the macro.

But nowadays there are some debuggers which are capable of
showing the value of macro in debugging time.
------------------------------------------------------------
2.Dont use macro for typedef or be cautious before use.

Ex:
lets say, u wanted to have a macro, which can be used to
represent a declaration to an int pointer

#define INTPTR int*

in main..
main()
{
INTPTR a, p;
/*here, our understanding will be 'a' and 'p' both are int
pointers*/
}

but in preprocessor time macro shall be replaced like this -
> int* a, p;
so only 'a' will be treated as int pointer and 'p' shall a
normal int variable.

So tyr to avoid using MACRO for typedef.
use -> typedef int* INTPTR, So that u can achieve desired
result.
------------------------------------------------------------
3.Be carefull while using macro in arithmatic operation.

Ex:

#define MUL(a,b) a*b

In main...
main()
{
int a = 3, b = 4;
....
....
.....
printf("%d", MUL(a+1, b+1));
/*Here u may expect the result 4 * 5 = 20 but the result
would be 8*/
}

lets analise,
in preprocessing time macro shall be replaced as below;

MUL(a+1, b+1) - > 3+1*4+1, so result would be 8.

To avoid the unexpected result.
Define macro lik ...

#define MUL(a,b) (a)*(b)
------------------------------------------------------------

Is This Answer Correct ?    5 Yes 3 No

Post New Answer

More C Interview Questions

main() {int i=5; // line 1 i=(++i)/(i++); // line 2 printf("%d",i); // line 3 } output is 2 but if we replace line 2 and line 3 by printf("%d",i=(++i)/(i++)); then output is 1. Why?

1 Answers   GATE,


what is difference between null and nul in c language

2 Answers  


Which control loop is recommended if you have to execute set of statements for fixed number of times?

0 Answers  


a simple c program using 'for' loop to display the output 5 4 3 2 1

2 Answers   Google,


Write a program to reverse a given number in c language?

0 Answers  






Write a program, where i have a grid with many cells, how many paths are possible from one point to other desired points.

0 Answers   Expedia,


Explain how do you generate random numbers in c?

0 Answers  


what is compiler

6 Answers  


Why we use void main in c?

0 Answers  


What are valid operations on pointers?

0 Answers  


What is the advantage of using #define to declare a constant?

0 Answers   Agilent, ZS Associates,


i have a written test for microland please give me test pattern

0 Answers   Microland,


Categories