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.

Ajmer thakur said:   2 months ago
The datatype of int is extern so is cancelled to be defined anywhere in the code.

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

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

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

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

Vishu said:   8 years ago
@Prathyusha.

You are right, answer A is corret.

Vishu said:   8 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:   9 years ago
Here, we should declare a=20 above the main function.

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

Is it a declaration or definition?

Please can someone explain this?


Post your comments here:

Your comments will be displayed after verification.