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)

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 .

Siva Krishna said:   1 decade ago
@Amit and Poornima.

Whenever a program encounters the instruction
exit(0);
It means it sent a 0 to operating system which means normal termination of the program and same is entered in the system log
And

Whenever a program encounters the instruction
exit(1);
It means it sent a 1 to operating system which means abnormal termination and it enters an error termination in the system log.

Note:
0 means successful termination.
1 or non-zero abnormal termination.

Conclusion:
Both are used to termination only. but the difference to indicate to the OS and a professional programming.

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)

Arun said:   9 years ago
On C compilers targeting the 8086 processor family, far pointers were declared using a non-standard far qualifier.

Far is not a standard keyword. Most of the compilers will raise error with far and huge qualifiers but compilers for 8086 processor family might not. It is used with pointers for memory segmentation. Its has two parts segment value and offset value (memory location within that segment).

Sundar said:   1 decade ago
@All

sizeof(int) = 2 bytes in 16 bit platform. (Turboc Under DOS)
sizeof(int) = 4 bytes in 32 bit platform. (GCC under Linux, C++ under Windows)

Like the same lot of things to be considered depending upon the platform.

All the answers given on this website are based on 16-bit platform (Turbo C under DOS).

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.

Kim Joe said:   1 decade ago
These concepts come into picture when working with DOS where memory is limited to 1 MB and CPU registers are 16bit wide,so to access more than 16bit data the memory was accessed with offset:segment combination using far and huge,but in 32 bit compilers these keywords are obsolete .

Deepak said:   1 decade ago
@Saraswathi

Huge pointers are normalized to have the highest possible segment for a given address, so size would be same as far pointer i.e. 4 byte (16 bit segment value +16 bit offset value)

Difference between far and huge pointer is of the highest possible segment.

Kavipriya said:   8 years ago
What is the output in turboc compiler and Linux compiler?

#include<stdio.h>
main(){
int x=65,y=97;
if('x'>'y')
Print("A");
Print("B");
}

I don't know what concept is there if anyone know that concept can you post the answer friends.


Post your comments here:

Your comments will be displayed after verification.