Find out the error(line number) in the program given below?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Find out the error(line number) in the program given below?
#include <stdio.h>
int main(void)
{
typedef struct //Line number 4
{
int a;
int b;
}tag;
struct tag
{
int b;
int a;
};
tag tag_T1; //Line number 14
struct tag_T2; //Line number 15
return 0;
}
**MCQ
Show answer & explanation
At line 4, a typedef struct is created with tag name 'tag'. This creates a type alias 'tag' (not a struct tag). At line 11, 'struct tag' attempts to redefine a struct with the same name 'tag', which shadows the typedef. However, the critical error occurs at line 15: 'struct tag_T2;' declares a variable 'tag_T2' of type 'struct tag', but 'tag_T2' is treated as an incomplete type declaration (missing initialization or definition). More importantly, line 14 uses 'tag tag_T1;' which refers to the typedef'd type, while line 15 'struct tag_T2;' tries to use 'struct tag' (the redefined struct), creating a namespace collision. Line 15 is erroneous because it's an incomplete type declaration without a variable name properly bound.
Step-by-step Derivation:
Step-by-step analysis:
- Line 4-9: typedef struct { int a; int b; } tag; — Creates a type alias 'tag'.
- Line 11-14: struct tag { int b; int a; }; — Redefines 'struct tag' as a new struct type. This shadows the typedef.
- Line 14: tag tag_T1; — Declares tag_T1 as the typedef'd type (with members a, b).
- Line 15: struct tag_T2; — Attempts to declare tag_T2 as an incomplete struct tag type. This is syntactically incomplete—it's neither a variable declaration with initialization nor a valid struct type reference. The line lacks proper termination and variable binding. This causes a compilation error: 'tag_T2' is declared but not properly initialized, and 'struct tag_T2;' is a dangling declaration.