Interview Question
Actualli i have tried the following codes in the C compiler.
#include<stdio.h>
int i;
static int i;
extern int i;
int main()
{
return 0;
}
It is not giving any compilation errors.
#include<stdio.h>
int i=10;
static int i=10;
extern int i;
int main()
{
return 0;
}
This code is throwing the error.(Redeclaration of i)
If the following three statements in three statements:
int i=100;
extern int i=100;
static int i=100;
are placed into three separate files that are fed to a compiler to be compiled into a single executable, the result will be an error concerning multiple definitions of the symbol i.
The extern statement is used to give a "heads-up' to the compiler that the symbol in question is defined "externally" to the current compilation unit, in this case the file. If the variable is set to 100 in the extern statement, then there will be two definitions of the variable. One may choose to the first statement to be:
int i;
in which case the three files can compile into a single executable. If this is done, however, the compiler will give a warning that a declaration occurs in an 'extern" statement. This is appropriate since the extern statement should not reserve storate.
in trurbo c
#include<stdio.h>
int i;
extern int i;
static int i;
void main() {
printf("%d",i);
}
no compilattion error..output 0
#include<stdio.h>
int i=10;
extern int i;
static int i;
void main() {
printf("%d",i);
}
no compilattion error..output 10
#include<stdio.h>
int i=10;
extern int i=20;
static int i;
void main() {
printf("%d",i);
}
compilattion error..multiple declarations of i
static int i and int i are visible in the same Translation unit . Hence conflicting declarations .
- Anonymous February 22, 2010