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:
44 comments Page 1 of 5.
Gaurav Bachhav said:
10 years 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.
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?
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?
Kas said:
8 years ago
@Jyoti mishra.
#include<stdio.h>
main(){
int n;
int row,col;
scanf("%d",&n);
int m=n;
for(row=1;row<=n;row++){
for(col=1;col<=m-row;col++){
printf(" ");
}
for(col=1;col<=row;col++){
printf("* ");
}printf("\n");
}
getch();
}
#include<stdio.h>
main(){
int n;
int row,col;
scanf("%d",&n);
int m=n;
for(row=1;row<=n;row++){
for(col=1;col<=m-row;col++){
printf(" ");
}
for(col=1;col<=row;col++){
printf("* ");
}printf("\n");
}
getch();
}
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!.....
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!.....
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?
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.
Vishu said:
6 years ago
@Jyoti.
Here is the code.
#include <stdio.h>
int main()
{
int i,j,n;
printf("enter the num:");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <=n-i ; j++ )
printf(" ");
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
Here is the code.
#include <stdio.h>
int main()
{
int i,j,n;
printf("enter the num:");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <=n-i ; j++ )
printf(" ");
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
Somjit Nag said:
8 years 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).
Lovely said:
7 years ago
extern int a; a is declared (forward referencing) (compiler only gets hint).
int a=20; is the definition (value is initialized).
Then when is the memory allocated to 20 and given a name a?
int a=20; is the definition (value is initialized).
Then when is the memory allocated to 20 and given a name a?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers