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.

Ranjith said:   2 decades ago
I don't know what is far and huge. Can you explain?

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 ?.

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 .

Gangadhararao said:   1 decade ago
#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;
}

How is the answer is 2,4,4 explain in depth?

Bharadwaj said:   1 decade ago
#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;
}

How the answer is 2,4,4?
Actually I compiled and I got the output as 4,4,4
So clarify this once

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).

Kavita.C.Karjagar said:   1 decade ago
1.Explain what is far and huge?
2.were it is used?

Rovin varshney said:   1 decade ago
char *s1;
char far *s2;
char huge *s3;

Can anyone explain these three line, whether it is pointer or simple variable declaration. Its so much confusing.

Vinu said:   1 decade ago
What is far and huge?

Reddy said:   1 decade ago
How they are using "*" symbol for variable declaration? i.e. *s1, *s2 n *s3

Any one answer me?


Post your comments here:

Your comments will be displayed after verification.