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 1 of 5.

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

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

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

Vishu said:   6 years ago
@Prathyusha.

You are right, answer A is corret.

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

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

Laxmi said:   7 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:   7 years ago
If we give like this extern int a = 2;

Is it a declaration or definition?

Please can someone explain this?

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

But, declaration is outside of the main function.

So, could you please explain more about that?

Ramya said:   7 years ago
Will you explain please?


Post your comments here:

Your comments will be displayed after verification.