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:
46 comments Page 1 of 5.

Aniket said:   3 years ago
It shows an error msg because extern are declared inside the function.
(1)

Shital said:   8 years ago
If int a=20; is definition, then what about if we say int a=20; is initialisation?
(1)

Vishu said:   10 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;
}
(1)

Mounika said:   1 decade ago
Absolutely option A is correct answer.

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.

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.


Post your comments here:

Your comments will be displayed after verification.