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;
Answer: Option
Explanation:
- During declaration we tell the datatype of the Variable.
- During definition the value is initialized.
Discussion:
46 comments Page 1 of 5.
Aniket said:
2 years ago
It shows an error msg because extern are declared inside the function.
(1)
Shital said:
7 years ago
If int a=20; is definition, then what about if we say int a=20; is initialisation?
(1)
Mounika said:
10 years ago
Absolutely option A is correct answer.
But, declaration is outside of the main function.
So, could you please explain more about that?
But, declaration is outside of the main function.
So, could you please explain more about that?
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.
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.
Somjit Nag said:
1 decade ago
a is declared and defined as a global variable. There is no need for an extern here.
Extern is used when the same variable needs to be used across multiple files (compilation units to be more precise).
Extern is used when the same variable needs to be used across multiple files (compilation units to be more precise).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers