C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 4)
4.
What is the output of the program in Turbo C (in DOS 16-bit OS)?
#include<stdio.h>
int main()
{
    char *s1;
    char far *s2;
    char huge *s3;
    printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
    return 0;
}
2, 4, 6
4, 4, 2
2, 4, 4
2, 2, 2
Answer: Option
Explanation:

Any pointer size is 2 bytes. (only 16-bit offset)
So, char *s1 = 2 bytes.
So, char far *s2; = 4 bytes.
So, char huge *s3; = 4 bytes.
A far, huge pointer has two parts: a 16-bit segment value and a 16-bit offset value.

Since C is a compiler dependent language, it may give different output in other platforms. The above program works fine in Windows (TurboC), but error in Linux (GCC Compiler).

Discussion:
55 comments Page 3 of 6.

Ashwini Shirke said:   8 years ago
Why intially 2 byte output is come?

Manish Motwani said:   8 years ago
Need some assistance, please any kind and knowledgeable person who can help me tell how the output of the c program is affected by 16 bit, 32 bit and 64 bit operating system.

Ani said:   7 years ago
I don't know what is far and huge here, Can you explain?

Kiran BS said:   6 years ago
far and huge is character variables. Here the variables aren't separated by comma.

And as I know the size of pointers is always same so when I compiled and checked it is showed 8, 8, 8 as output, which is the correct answer or even 4, 4, 4 is possible since the format specifier is %d.

Laxman said:   6 years ago
Sir every pointer has 2 bytes so how *s2, *s3 4 bytes?

Amol said:   6 years ago
@Sirji if you used * as like above example then you got an error.

Dipak said:   5 years ago
No, it's wrong because every pointer has same size irrespective to its base type so its either 4 bytes or 8 bytes depend upon the compiler 32 bit or 64 bit irrespectively.

Shri Hari Rajeswar said:   5 years ago
Can you please explain how you said like this? because pointer holds same bytes irrespective of the base you use.

Shefali said:   1 decade ago
In a generic OS , memory is organised in a segment:offset fashion. Now say,it is of "X" MB and this "X" MB is made up of say "S" number of segments of each segment having "B" Bytes where S*B Bytes=X MB.

(char *s)-> Pointer: A near pointer is that which will only point within the current segment say segment 3 (there are S number of segments numbered 0 to S-1) by containing only offset .

(char far *s1) :=> Far Pointer: A far pointer is that which will point anywhere in the X MB across segments by containing segment+offset .
The numbers X,S and B vary across diff operating system memory models under which you are programming .

Reema said:   1 decade ago
I dont know what is far and huge. Can you explain ?

And what do you mean by 16-bit offset and 16-bit segment value ?.


Post your comments here:

Your comments will be displayed after verification.