The program given below gives a compiler error in two lines.
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
The program given below gives a compiler error in two lines.
#include <stdio.h>
#define pull 45 //line2
#define pulll 46 //line 3
int main()
{
int zx;
zx = pulll; //line 7
pulll = pull; //line 8
pull = zx; //line 9
printf("%d\n%d", pull, pulll); //line 10
return 0;
}
Find the erroneous line numbers.
Show answer & explanation
Answer: A. Line 8 and Line 9
#define creates compile-time substitutions, not variables. Lines 8 and 9 attempt to assign values to pull and pulll as if they were variables, but they are macros and cannot be reassigned. The compiler will reject these lines with "lvalue required" or similar errors because macros are not lvalues.
Step-by-step Derivation:
Step-by-step analysis:
- Line 2:
#define pull 45– creates a macro; valid. - Line 3:
#define pulll 46– creates a macro; valid. - Line 7:
zx = pulll;– reads the macro value (becomeszx = 46;); valid. - Line 8:
pulll = pull;– attempts to assign to macropulll(becomes46 = 45;); COMPILER ERROR (lvalue required). - Line 9:
pull = zx;– attempts to assign to macropull(becomes45 = zx;); COMPILER ERROR (lvalue required). - Line 10:
printf(...)– reads macro values; valid.
Macros are textual substitutions and cannot be used on the left-hand side (lvalue) of an assignment. The two erroneous lines are Line 8 and Line 9.