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.
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?
Ramya said:
10 years ago
Will you explain please?
Lovely said:
10 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?
Kas said:
1 decade 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();
}
Susee said:
1 decade ago
May I know explanation for "return 0"? What it will do?
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).
Ayush said:
1 decade ago
In this question the extern keyword is used inside the function which is prohibited.
Jyoti mishra said:
1 decade ago
What is the program for this output?
*
* *
* * *
* * * *
* * * * *
Shwetha said:
1 decade ago
since int a=20; is in outside of the main() it is definition.
Sunil Kumar said:
1 decade ago
I think option D is correct because we never put semicolon after function definition.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers