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 1 of 2.

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.

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

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

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.

ROHITH said:   1 decade ago
Thanks @ROHIT.

R@hit said:   1 decade ago
I am satisfied with Kickem's explanation.

In this program the line
(int)(float)(char) i;........... is useless

If you compile the program then find that compiler say's that "Code has no effect" and highlight this line.

In this program the compiler print the size of double which is declared global in the program.

If you want to check that how it is true then you will replace double to the int or float you find that it print's the size of int or float.

Write the program like

#include<stdio.h>
int i;

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

Output is : 2

Thank you friends if you have more questions the contact me in gmail.com my email-id is : rk52567(at)gmail.com

Theja said:   1 decade ago
@Wikiok...Thanks for explanation

Kannan said:   1 decade ago
#include<stdio.h>
double i;

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

Prathyusha said:   1 decade ago
GUD EXPLANATION!.....@Kickem

Wikiok said:   1 decade ago
(float) i : Is not a declaration, but a cast. You need an lvalue too. It doesn't change the sizeof(i).
float i : It is a declaration. sizeof(i) = 4 (32bit platform)


Post your comments here:

Your comments will be displayed after verification.