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

number of times a digit is present in a number

0 Answers  


How many levels deep can include files be nested?

0 Answers  


Is c procedural or functional?

0 Answers  


What does the message "warning: macro replacement within a string literal" mean?

1 Answers  


Explain what are preprocessor directives?

0 Answers  






What is the basic structure of c?

0 Answers  


Given a number N, product(N) is the product of the digits of N. We can then form a sequence N, product(N), product(product(N))… For example, using 99, we get the sequence 99, 99 = 81, 81 = 8. Input Format: A single integer N Output Format: A single integer which is the number of steps after which a single digit number occurs in the sequence. Sample Test Cases: Input #00: 99 Output #00: 2 Explanation: Step - 1 : 9 * 9 = 81 Step - 2 : 8 * 1 = 8 There are 2 steps to get to this single digit number. Input #01: 1137638147

2 Answers  


Why doesnt this code work?

0 Answers  


What are the string functions? List some string functions available in c.

0 Answers  


Write a program to check whether a number is prime or not using c?

0 Answers  


What are the two types of functions in c?

0 Answers  


If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5).

5 Answers   Microsoft, Motorola,


Categories