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;
}
Discussion:
14 comments Page 1 of 2.
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
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
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.
#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.
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));
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));
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)
float i : It is a declaration. sizeof(i) = 4 (32bit platform)
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.
Can any one explain how this typecasting works? Why there won't be any changes in its size? Its little confusing.
Kickem said:
1 decade ago
In the line <(int)(float)(char) i;> the casts do not modify the type of the variable i, so i is still a Double stored 8 bytes.
Kannan said:
1 decade ago
#include<stdio.h>
double i;
int main()
{
(int)(float)(char) i;
printf("%d",sizeof(i));
return 0;
}
double i;
int main()
{
(int)(float)(char) i;
printf("%d",sizeof(i));
return 0;
}
Ramanathan said:
1 decade ago
What is the difference between these two
(float) i; and
float i;
It produce the size as 8 and 4 respectively.?
(float) i; and
float i;
It produce the size as 8 and 4 respectively.?
Shivaraj said:
1 decade ago
What if after typecast the value of i is stored back is
i = (int)(char)(float)i;
i = (int)(char)(float)i;
Raj said:
1 decade ago
Initially i is declared as double. So i=8 .
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers