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

What is sizeof int?

0 Answers  


how to find a 5th bit is set in c program

4 Answers   IBM,


hi folks i m approching for h1 b interview on monday 8th of august at montreal and i m having little problem in my approval notice abt my bithdate my employer has made a mistake while applying it is 12th january and istead of that he had done 18 the of january do any body have any solution for that if yes how can i prove my visa officer abt my real birthdate it urgent please let me know guys thaks dipesh patel

0 Answers  


Is c weakly typed?

0 Answers  


how the compiler treats any volatile variable?Explain with example.

1 Answers   Tata Elxsi,






how does the C compiler interpret the following two statements p=p+x; q=q+y; a. p=p+x; q=q+y b. p=p+xq=q+y c. p=p+xq; q=q+y d. p=p+x/q=q+y

2 Answers   TCS, Tech Synergy,


what is the difference between NULL('\0') and 0?

14 Answers   Microsoft,


Explain how can a program be made to print the line number where an error occurs?

0 Answers  


to get a line of text and count the number of vowels in it

3 Answers   Satyam,


what are the static variables

8 Answers   HCL, iFlex, TCS, Wipro,


Create a structure to specify data on students as given below: Roll number, Name, Department, Course, and Year of joining. Assume that there are not more than 450 students in the collage. (a) Write a function to print the names of all students who joined in the last 3 years. (b) Write a function to print the data of a student whose roll numbers are divisible by 4.

0 Answers  


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

2 Answers   Cap Gemini, HCL,


Categories