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

Sanjana said:   2 years ago
1. Near Pointer:
The Near Pointer is used to store the 16-bit addresses. It means that they can only reach the memory addresses within the current segment on a 16-bit machine. That is why we can only access the first 64 kb of data using near-pointers.
The size of the near pointer is 2 bytes.

2. Far Pointer:
A far pointer stores the address in two 16-bit registers that allow it to access the memory outside of the current segment. The compiler allocates a segment register to store the segment address, and then another register to store offset within the current segment. The offset is then added to the shifted segment address to get the actual address
The size of the far pointer is 4 bytes.

3. Huge Pointer:
The huge pointer also stores the addresses in two separate registers similar to the far pointer. It has the following characteristics:
In the Huge pointer, both offset and segment address is changed.
The Huge Pointers always compare the absolute addresses, so the relational operation can be performed on it.
The size of the huge pointer is 4 bytes.
(1)

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.

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.

Gourvi said:   5 years ago
@For_all.

Here in this question, we have 3 type of pointers that is near, far and huge. And by default in c we have near pointer, for eg: int *p, here size of (p) would be 2 bytes in 16 bit complier. But here far and huge pointers are also being used and there sizeof is 4 bytes.

P.S: Forget about char size as here main focus is on near, far and huge pointers.

And for more clear understanding please refer google.
(2)

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

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

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.

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

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.

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


Post your comments here:

Your comments will be displayed after verification.