πŸ“Š C Programming
Q. What will be the output of this code?
Code:
void main()
{
      extern int i;
      i=20;
      printf("%d", sizeof(i));
}
  • (A) 20
  • (B) 2
  • (C) Compiler Error
  • (D) Linker Error
πŸ’¬ Discuss
βœ… Correct Answer: (D) Linker Error

Explanation: Linker error: undefined symbol '_i'. Explanation:
extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn't find any error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error.

Explanation by: Rajeev Malhotra
Linker error: undefined symbol '_i'. Explanation:
extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn't find any error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
189
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Rajeev Malhotra
Publisher
πŸ“ˆ
84%
Success Rate