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 2 of 5.
Satyadev said:
1 decade ago
The definition of 'a' outside the function main is not valid, so it is not considered.
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?
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?
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?
Aman said:
1 decade ago
When we have need to declare the extern variables? and what are its Benefits?
Bhavana said:
1 decade ago
I agree with prathyusha.
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.
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.
Rathika.b said:
1 decade ago
Please give an real example for "extern" keyword; Then only we know that how it works.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers