C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - General Questions (Q.No. 10)
In the following program where is the variable a getting defined and where it is getting declared?
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

- During declaration we tell the datatype of the Variable.

- During definition the value is initialized.

45 comments Page 1 of 5.

Aniket said:   12 months ago
It shows an error msg because extern are declared inside the function.

Shubham said:   3 years ago
What is the output of this program? Please anyone explain.

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

Aman said:   7 years ago
a is defined outside the scope of the program. It will not even get executed.

Vishu said:   7 years ago

You are right, answer A is corret.

Vishu said:   7 years ago

Here is the code.

#include <stdio.h>
int main()
int i,j,n;
printf("enter the num:");
for(i = 1; i <= n; i++)
for(j = 1; j <=n-i ; j++ )
printf(" ");
for(j = 1; j <= i; j++)
printf("* ");
return 0;

Deepak said:   8 years ago
Here, we should declare a=20 above the main function.

Laxmi said:   8 years ago
extern int a=2; it is a definition. Because variable a is initialized to 2 then memory is created for a. That's why we call it as a definition.

Anusha said:   8 years ago
If we give like this extern int a = 2;

Is it a declaration or definition?

Please can someone explain this?

Mounika said:   8 years ago
Absolutely option A is correct answer.

But, declaration is outside of the main function.

So, could you please explain more about that?

Post your comments here:

Your comments will be displayed after verification.