# 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;
}
``````
x and y are equal
x and y are not equal
Unpredictable
No output
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.

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?

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".

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.

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

Solution: Automatical conversion x-> (float)x, so ((float)x == y) is TRUE.

int is promoted 2 float then it will compare ....ans is correct.

Why int will be promoted to float. It could float be converted to int ?

Why int will be promoted to float. It could float be converted to int ?

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 ?

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..