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

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

Kas said:   1 decade 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();
}

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

Ramya said:   10 years ago
Will you explain please?

Ajit Darnal said:   1 decade ago
Definition int a=20 ; is outside the main, how it is possible ?

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

Is it a declaration or definition?

Please can someone explain this?

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.

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

Vishu said:   9 years ago
@Prathyusha.

You are right, answer A is corret.

Vishu said:   9 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;
}


Post your comments here:

Your comments will be displayed after verification.