C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 2)
2.
What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
Answer: Option
Explanation:
Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.
Discussion:
57 comments Page 3 of 6.
Raka said:
1 decade ago
@Rathika
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}int a=20;
it print a=20;
Copy this program run it.
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}int a=20;
it print a=20;
Copy this program run it.
Pallavi said:
1 decade ago
We have already declared the int i=20 then why it gives linker error?
Anurag said:
1 decade ago
extern int i;
int main()
{
int i = 20;
printf("%d\n", sizeof(i));
getch();
return 0;
}
This gives size 4 as extern declared outside.
int main()
{
int i = 20;
printf("%d\n", sizeof(i));
getch();
return 0;
}
This gives size 4 as extern declared outside.
Rupinderjit said:
1 decade ago
@Anurag.
It's value only depend upon the environment you're working on. And FYI global declaration of variable is extern, by default.
It's value only depend upon the environment you're working on. And FYI global declaration of variable is extern, by default.
Vamshi krishna said:
1 decade ago
@Anurag.
Size of i depends on compilers you are working with if you execute with 16 bit compiler its size is 2 bytes in case of 32 bit compiler i size is 4 bytes.
Size of i depends on compilers you are working with if you execute with 16 bit compiler its size is 2 bytes in case of 32 bit compiler i size is 4 bytes.
Vidya said:
1 decade ago
Rehana output is 4 neither 10 nor 20, anybody explain me briefly.
Harshad said:
1 decade ago
@Nani ..
#include<stdio.h>
int main()
{
extern int i;
printf("%d\n", sizeof(i));
return 0;
}
int i=20;
run the program in 16 bit platform then got the ans: 2
if u run program on 32 bit platform then got the ans: 4
#include<stdio.h>
int main()
{
extern int i;
printf("%d\n", sizeof(i));
return 0;
}
int i=20;
run the program in 16 bit platform then got the ans: 2
if u run program on 32 bit platform then got the ans: 4
Noor said:
1 decade ago
Global declaration is meant for accessing variable even outside the block where it's declared, but that doesn't mean that it cannot be accessed or defined in the block where it is declared, so we should not get any error.
Ranjani said:
1 decade ago
Even though variable i in external file it is int data type. In sizeof() operator it going to print size of the datatype only. the data type mentioned in (extern int i;) statement. Then why we didn't get the output?
Vikas said:
1 decade ago
please explain me, what is the mean of s*=3;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers