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

Neethu said:   1 decade ago
Explain, in which cases we were used these far and huge ?

Dhiraj said:   1 decade ago
@Reddy

Here *s1 means its a pointer variable which is of character type, means the pointer s1 points to some other variable which is of character type and can access that character variable.

Jeyanthi said:   1 decade ago
I don't understand this.

Kamesh said:   1 decade ago
Can any body tell me what does mean by

char far *s2;
char huge *s3;

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 .

Lathaa said:   1 decade ago
A Null pointer is one which does not refer to any thing.

Far pointer refers to an address which not in the same segment where pointer is defined.

Near pointer refers to an address in the same segment where the pointer is defined.

Poornima said:   1 decade ago
Could you please explain the differences between the exit(0); and exit(1);and also the difference between the return(0); and return(1);.

Prashant said:   1 decade ago
@Sundar

We are talking about sizeof(char) not a sizeof(int)....!!!!

How come it can be 4 rather than becoming 1 or 2...?

Saraswathi said:   1 decade ago
Then what is huge pointer? is it same as far pointer.

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.


Post your comments here:

Your comments will be displayed after verification.