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:
44 comments Page 2 of 5.

Lovely said:   8 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?

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();
}

Susee said:   8 years ago
May I know explanation for "return 0"? What it will do?

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).

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

Jyoti mishra said:   9 years ago
What is the program for this output?

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

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

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

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.

Anjali agarwal said:   10 years ago
What is extern? please explain me?


Post your comments here:

Your comments will be displayed after verification.