C Programming - Complicated Declarations - Discussion

Discussion Forum : Complicated Declarations - Find Output of Program (Q.No. 2)
2.
What will be the output of the program in DOS (Compiler - Turbo C)?
#include<stdio.h>
double i;

int main()
{
    (int)(float)(char) i;
    printf("%d", sizeof((int)(float)(char)i));
    return 0;
}
1
2
4
8
Answer: Option
Explanation:
Due to the C language is being platform dependent:

In Turbo C (DOS - 16 bit platform), the output will be 2.

But in GCC (Unix/Linux - 32 bit platform), the output will be 4.
Discussion:
19 comments Page 1 of 2.

Raja said:   2 decades ago
The option does not matches with the answer of the online compiler which is giving the answer as 4.

Why?

Sundar said:   2 decades ago
Hi Raja,

C is a machine dependent language. Typically we assume that the C program is compiled on DOS or Windows platform with Turbo C. The above program will give '2' as output in Turbo C.

But, this online compiler runs on a 64-bit platform (Linux server - GCC compiler). So it produces 4 as output.

Just try the below code in in Turbo C and Online Compiler, you will understand it better:

int a;
printf("%x", &a);

Have a nice day!

Ankit Anand said:   2 decades ago
Actually its not about C language, its about compiler we are using,

I am using gcc 4:4.4.31-ubuntu1.

It gives sizeof(int) = 4, but turbo compiler on windows gives sizeof(int) = 2.

and Yes questions based on size of leads to confusion...due to their compiler dependency.

Sasikala [Chennai] said:   2 decades ago
Hi Ankit Anand and Sundar,

Thanks for your informations.

While answering the questions in written test, which one should I follow? TurboC or GCC? Windows or Linux?

Thanks in advance.

Himanshu said:   1 decade ago
What the meaning of these three declaration... which is consider by compiler... int, char, float?

Suresh said:   1 decade ago
I gets cast to a char, then to a float and finally to an int whose size is then used in sizeof. In Turbo C int is 2 bytes.

Sasi said:   1 decade ago
Can you please explain (int) (float) (char) i this statement?

Kapilsuman said:   1 decade ago
(int)(float)(char) i;
it runs like that
typecast i into char first which is of 1 byte.
: 1 byte<-(char) i
than in float
: 4 byte <- char variable i(size is 1 byte)
than in int
: 2 byte<- float variable i(size is 4 byte)
so finally i is of 2 byte integer variable

Shri said:   1 decade ago
Very good explanation kapilsuman. Kudos to you.

Kaleem said:   1 decade ago
That right way to explain.... weldn kapilsuman


Post your comments here:

Your comments will be displayed after verification.