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.

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

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.

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?

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.

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.

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.

Jaya phanindra said:   8 years ago
While compiling the given code am getting the below error.

Can anyone explain this please.

Error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token.

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.

Madhav said:   1 decade ago
Now a days 16 OS: not available in market.

So we are use either 32 bit or 64 bit.

Please ask Q for 32/64 bit compiler not 16/8/4/2/1 bit compiler.

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


Post your comments here:

Your comments will be displayed after verification.