C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>
int main()
{
int x = 3;
float y = 3.0;
if(x == y)
printf("x and y are equal");
else
printf("x and y are not equal");
return 0;
}
Answer: Option
Explanation:
Step 1: int x = 3; here variable x is an integer type and initialized to '3'.
Step 2: float y = 3.0; here variable y is an float type and initialized to '3.0'
Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied.
Hence it prints "x and y are equal".
Discussion:
27 comments Page 2 of 3.
Sandeep said:
1 decade ago
int and float are 4 byte and compiler converts int to float while comparing both, while double is of 8 byte so we can't compare float and double, as I think, please let me know if I am wrong.
Souvik said:
10 years ago
#include<stdio.h>
int main()
{
int x=4,y,z;
y=--x;
z=x--;
printf("x=%d y=%d z=%d\n",x,y,z);
return 0;
}
What will be the output? Please explain.
int main()
{
int x=4,y,z;
y=--x;
z=x--;
printf("x=%d y=%d z=%d\n",x,y,z);
return 0;
}
What will be the output? Please explain.
Sundar said:
1 decade ago
The given answer is correct only. Don't get confused.
I have tested the above program in Turbo C, GCC.
I got the same output as given in option A.
Output: x and y are equal
I have tested the above program in Turbo C, GCC.
I got the same output as given in option A.
Output: x and y are equal
Shadab khan said:
1 decade ago
Since float is bigger datatype than int so it implicitly typecast int into float. That's why both the x and y of float type then the comparison is true.
Raju Naidu said:
1 decade ago
float a=3.02f
then it'l take a float value
If you are mentioned any decimal number without mentioning the 'f' symbol at the end it treated as double.
then it'l take a float value
If you are mentioned any decimal number without mentioning the 'f' symbol at the end it treated as double.
Tasaduq Hussain said:
1 decade ago
If y = 3.0000001. Then output iz x and why are equal.
But,
When y = 3.000001. The output iz x and why are not equal.
What is reason behind it?
But,
When y = 3.000001. The output iz x and why are not equal.
What is reason behind it?
Acute said:
1 decade ago
As it is float it must be stored as 2. 999999 something like that. Then the comparison must take place.
Can anybody please explain me this?
Can anybody please explain me this?
Maulik patel said:
1 decade ago
Here, int & float are different datatype,
So Comparative operator can't work with above condition.
So given ans is wrong.
So Comparative operator can't work with above condition.
So given ans is wrong.
Saikrishna.y said:
1 decade ago
How an int value can be converted into float and compare automatically without using any comparative operator?
(1)
Laksh said:
8 years ago
Here int is first promoted to float 3.0 and then the two values are compare which prints x and y are equal.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers