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 5 of 5.

Santhosh said:   1 decade ago
Here declaration part is ok but where in case of definition that defined at out side of the main function so it will not be considered if we execute that code we will get garbage value.

Teja said:   1 decade ago
What is this extern function? How and where it can be used?

Pawan Asati said:   1 decade ago
option A is correct because extern is a storage class so a is treated as global variable so it definition can be out of main we write.

Pooja said:   1 decade ago
External variable is declared as:
extern <data_type> <var>;
It is present in another program and we are refering it.
So we have to define it outside the main().....

Md Alam said:   1 decade ago
Hi friends,

I am agreed with that Prathyusha say.

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;.

If it is suppose to be a program that I write and declare a extern () funtion within the main () and define the variable outside the main (). Then it will work or not?

Anjan said:   1 decade ago
If int a=20; then is it int a is declaration and a=20 defination?

Please clear me.


Post your comments here:

Your comments will be displayed after verification.