C and C++ Interview Questions |
10. Differentiate between an internal static and external static variable?
Ans:
An internal static variable is declared inside a block with static storage class whereas an external static variable is declared outside all the blocks in a file. An internal static variable has persistent storage, block scope and no linkage. An external static variable has permanent storage, file scope and internal linkage. |
Ex - 1: What is the output?
#include<stdio.h>
main()
{
extern int i;
printf(“%d”,i);
}
int i=200;
Ans:
200.
Explanation:
In this example the variable i is declared as extern. Extern storage class allows the values from outside program .It are also global scope. |
Ex - 2: What is the output?
#include<stdio.h>
main()
{
extern int i=20;
printf(“%d”,i);
}
int i=200;
Ans:
Compiler error.
Explanation:
Extern variables cannot be initialized. Extern allows only variable declarations. |
Ex - 3: What is the output?
#include<stdio.h>
main()
{
extern int i;
i=20;
printf(“%d”,i);
}
Ans::
Runtime error.
Explanation:
Because extern have the global scope. In the above code the variable i is undefined symbol. Here i do not declare outside the main. |