C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - General Questions (Q.No. 10)
10.
In the following program where is the variable a getting defined and where it is getting declared?
#include<stdio.h>
int main()
{
    extern int a;
    printf("%d\n", a);
    return 0;
}
int a=20;
extern int a is declaration, int a = 20 is the definition
int a = 20 is declaration, extern int a is the definition
int a = 20 is definition, a is not defined
a is declared, a is not defined
Answer: Option
Explanation:

- During declaration we tell the datatype of the Variable.

- During definition the value is initialized.

Discussion:
46 comments Page 4 of 5.

Mayank said:   1 decade ago
extern int a; indicates that the variable a is defined elsewhere, usually in a separate source code module.

printf("%d\n", a); it prints the value of local variable int a = 20. Because, whenever there is a conflict between local variable and global variable, local variable gets the highest priority. So it prints 20.

Bhavana said:   1 decade ago
I agree with prathyusha.

Aman said:   1 decade ago
When we have need to declare the extern variables? and what are its Benefits?

Purnima said:   1 decade ago
Hi friends in my way if we declare extern int a;then we can use the variable a in any line or in may any function through out the programm, .

And if we declare int a;then it is local declaration here we can use a inside of the function only, if that function is closed then we can not use a in in another function.

Is it correct?

M.DHILIP BABU said:   1 decade ago
Why was declared int a=20 outside? suppose int a=20 declared inside of the function what is the o/p of this program?

Satyadev said:   1 decade ago
The definition of 'a' outside the function main is not valid, so it is not considered.

Kavita.C.Karjagar said:   1 decade ago
1.What is the difference between int a and extern int a?
2.when we use extern int a and int a?

Prathyusha said:   1 decade ago
Hi friends!

here in the program please go through the question again.he was just asking where "a" is declared and where it is defined.
whether "a" is normal integer or external integer not matters.
so,declaration is : extern int a;
and definition is :int a=20;
that's it!....

correct me if i am wrong plz!.....

Sindhu said:   1 decade ago
int a=20 is outside the main() so it is not considered as a declaration for a in given program

Ravi Karthick said:   1 decade ago
Extern means external declaration that is declared for outside the function,so that a that is declared as extern and defined in outside the main ar a=20


Post your comments here:

Your comments will be displayed after verification.