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

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.

Ajit Darnal said:   1 decade ago
Definition int a=20 ; is outside the main, how it is possible ?

Anjali agarwal said:   1 decade ago
What is extern? please explain me?

Gaurav Bachhav said:   1 decade ago
In C there are two important concept declaration and definition.

Declaration:-It only defines the type of variable but does not
allocate any memory space.

Ex:- extern int i;//this is the only way of only declaring variable.

Definition:-It also defines the type of variable but in this case the memory is allocated to variable and also it is initialized(initialization is not mandatory].

Ex:- int i=10;

int i; //this is also definition as memory is allocated but for simplicity we call it as declaration.

Sunil Kumar said:   1 decade ago
I think option D is correct because we never put semicolon after function definition.

Shwetha said:   1 decade ago
since int a=20; is in outside of the main() it is definition.

Jyoti mishra said:   1 decade ago
What is the program for this output?

          *
* *
* * *
* * * *
* * * * *

Ayush said:   1 decade ago
In this question the extern keyword is used inside the function which is prohibited.


Post your comments here:

Your comments will be displayed after verification.