C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - True / False Questions (Q.No. 3)
3.
If the definition of the external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function.
True
False
Answer: Option
Explanation:

True, When a function is declared inside the source file, that function(local function) get a priority than the extern function. So there is no need to declare a function as extern inside the same source file.

Discussion:
6 comments Page 1 of 1.

Knight said:   1 decade ago
This situation is just similar to example, suppose xyz is a extern variable,and no external declaration is occur

int main(){
int xyz; //local declaration
xyz=100;
printf("%d",xyz);
}

Is totally valid, because int xyz got priority because it is local.

Manjiri said:   1 decade ago
What extern keyword does?

Aditya Chauhan said:   1 decade ago
extern is keyword which is used to define any variable or function in externally.

#include<stdio.h>
int main()
{
extern int a;
printf("%d\n",a);
return 0;
} int a = 20;

Output is : 20

Where extern tells the compiler that variable type int and named 'a' will be linked to the program at runtime.

JON said:   1 decade ago
Why Below Code is giving Error! Kindly Explain :)

#include<stdio.h>
void fun()
{
printf("Extern Fun");
}
int main()
{
void fun(){ printf("Internal Fun");}
extern fun();
return 0;
}

JON said:   1 decade ago
Found solution also.

#include<stdio.h>
int main()
{
extern void fun();
fun();
return 0;
}
void fun()
{
printf("Extern Fun");
}

To use Extern Function Explicitly you can use above code.

Dewesh shringi said:   9 years ago
Your program gives an error because you use the extern keyword before a function which is defined in the same program.

That is function's definition.

Once you invoke the function control automatically goes to their corresponding definition.

So there is no need of extern keyword.

Post your comments here:

Your comments will be displayed after verification.