A C language preprocessor macro definition has the form: #define name replacemen
ID: 3827492 • Letter: A
Question
A C language preprocessor macro definition has the form: #define name replacement text Such a definition calls for a macro substitution of the simplest kind - subsequent occurrences of the token name will be replaced by the replacement text, where name has the same form as a variable name and replacement text is arbitrary. It is also possible to define macros with arguments, so that the replacement text can be different for different calls of the macro. This form of "parameter passing" is essentially pass-by-name. Besides inheriting all the potential problems of pass-by-name, macro substitutions has problems of its own. Consider the following piece of code fragment. #define testNeg(x) if (x > 0) printf("Oouch ") ... if (a > b) testNeg(b); else printf("Aaargh/n"); printf("Thank you "); What is the output (if any) of the fragment if (i) a is 4 and b is 3, (ii) a is -4 and b is 3, and (iii) a is -3 and b is -4? What is the potential problem of the code fragment?Explanation / Answer
please find the comments below
if(a > b) condition will be true and macro testNeg(b) is replaced with b=3 value
if(3 > 0) printf(“Oouch ”)
so the output will be Oouch.
So the output will be Aaargh.
The problem with code fragment is
To avoid this macro we can write inline function in place of macro. This function works same as that of macro but will check types, i.e. will check types as normal function.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.