C Programming - Complicated Declarations - Discussion

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;
}

[A]. 1
[B]. 2
[C]. 4
[D]. 8

Answer: Option B

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.

Raja said: (Jul 8, 2010)  
The option does not matches with the answer of the online compiler which is giving the answer as 4.

Why?

Sundar said: (Jul 8, 2010)  
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: (Jul 9, 2010)  
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: (Jul 9, 2010)  
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: (Sep 12, 2011)  
What the meaning of these three declaration... which is consider by compiler... int, char, float?

Suresh said: (Nov 18, 2011)  
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: (Dec 1, 2011)  
Can you please explain (int) (float) (char) i this statement?

Kapilsuman said: (Jan 2, 2012)  
(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: (Feb 3, 2012)  
Very good explanation kapilsuman. Kudos to you.

Kaleem said: (Mar 18, 2012)  
That right way to explain.... weldn kapilsuman

Aakash Gupta said: (Apr 23, 2013)  
I'm getting following errors in Borland c++:

(int)(float)(char) i; //Error: undefined symbol 'i'
printf("%d", sizeof((int)(float)(char)i)); //ERROR: Not an allowed type

& I'm getting following errors in GCC compiler:
In function 'main':
Line 6: error: 'i' undeclared (first use in this function)
Line 6: error: (Each undeclared identifier is reported only once
Line 6: error: for each function it appears in.)

Please Help with this?

Tinku said: (May 10, 2013)  
Can you please explain that variable "i" will be declared as which datatype and why?
(int)(float)(char) i;


@Aakash: For me the code is running on Linux. I suggest you to try code on some other C version. Hope this helps!

Kranthi Kumar G said: (Jul 9, 2013)  
type casting will like this.

char size(1) will converted to float size(4).

float size(4) will then converted to size int(2).

So answer is 2.

Archana said: (Nov 7, 2013)  
But look at the declaration of I globally at the top. What does it signify in the pgm?

Shiv said: (Oct 2, 2014)  
double i;

int main()
{
(int)(float)(char) i;
printf("%d", sizeof(i));
return 0;
}

Output:8
Why is this?
Can anyone give me a valid solution.

Naresh said: (Feb 7, 2016)  
Is it possible to convert float (4 bytes) to int (2 bytes)?

Pavan Kumar said: (Jun 17, 2016)  
What is typecasting?

Jangum said: (Sep 7, 2016)  
Typecasting: Converting one datatype into another datatype.

int x;
char v;
v = (int) x; // x (int) is converted into char datatype and it's value stored into char variable v.

Juel Khan said: (May 12, 2020)  
Data type placed in first position in sizeof operator will works only. So, the output is 4 for 64-bit machine and 2 for 32-bit machine. Thanks to all.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.