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.

Juel Khan said:   3 years ago
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.

Jangum said:   7 years ago
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.

Pavan Kumar said:   7 years ago
What is typecasting?

Naresh said:   7 years ago
Is it possible to convert float (4 bytes) to int (2 bytes)?

Shiv said:   9 years ago
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.

Archana said:   10 years ago
But look at the declaration of I globally at the top. What does it signify in the pgm?

Kranthi kumar G said:   10 years ago
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.

Tinku said:   1 decade ago
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!

Aakash Gupta said:   1 decade ago
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?

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


Post your comments here:

Your comments will be displayed after verification.