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 1 of 3.
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?
Rajendra Pandey said:
1 decade ago
the int x = 3 is first converted into float value 3.0 and then compared since int and float cannot be compared it must first convert int value in float and then the comparision takes place. so the condition is true.
Hence it prints "x and y are equal".
Hence it prints "x and y are equal".
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.
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
Wikiok said:
1 decade ago
Solution: Automatical conversion x-> (float)x, so ((float)x == y) is TRUE.
Sharanu said:
1 decade ago
int is promoted 2 float then it will compare ....ans is correct.
Vishwas said:
1 decade ago
Why int will be promoted to float. It could float be converted to int ?
Vishwas said:
1 decade ago
Why int will be promoted to float. It could float be converted to int ?
Dumanth said:
1 decade ago
void main()
{
float a=1.1;
double b=1.1;
if(a==b)
{
printf(" I love you\n");
}
else
{
printf(" I hate you\n");
}
}
If you execute this code you will get output as I hate you..
then How this is happening ?
{
float a=1.1;
double b=1.1;
if(a==b)
{
printf(" I love you\n");
}
else
{
printf(" I hate you\n");
}
}
If you execute this code you will get output as I hate you..
then How this is happening ?
Zulfiquar said:
1 decade ago
According to me if u mention float like...
float a=3.02f
then a will take float value.
If u write like...
float a=3.02
then a will take an int value.
So for float you must use the "f" with value..
float a=3.02f
then a will take float value.
If u write like...
float a=3.02
then a will take an int value.
So for float you must use the "f" with value..
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers