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.

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!

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?

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

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.

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!

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.

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

Shiv said:   1 decade 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.

Juel Khan said:   5 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.
(1)

Kranthi kumar G said:   1 decade 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.
(1)


Post your comments here:

Your comments will be displayed after verification.