C Programming - Complicated Declarations - Discussion

Discussion Forum : Complicated Declarations - Find Output of Program (Q.No. 8)
8.
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(i));
    return 0;
}
4
8
16
22
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
14 comments Page 2 of 2.

Nikhil sahu said:   1 decade ago
Explanation:

#include<stdio.h>
double i;

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

If you compile above commented code it will not show any error because it took double size as 8 and display it.
Now,

#include<stdio.h>
//double i;

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

Now when we compile this code we will get an error.
Error: \'i\' undeclared (first use in this function),
Because the compiler ignoring this line.

I hope everyone can understand.

Shivaraj said:   1 decade ago
What if after typecast the value of i is stored back is
i = (int)(char)(float)i;

Taranjeet said:   1 decade ago
@Shivaraj.

Then also the output would have been 8.

The output will be different when you write the statement within sizeof as
printf("%d",sizeof((int)(float)(char)i));

Anamika said:   1 decade ago
i = (int)(char)(float) i;

Can any one explain how this typecasting works? Why there won't be any changes in its size? Its little confusing.


Post your comments here:

Your comments will be displayed after verification.