C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 5)
What will be the output of the program?
int main()
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
        printf("x and y are not equal");
    return 0;
x and y are equal
x and y are not equal
No output
Answer: Option

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

27 comments Page 3 of 3.

Vishwas said:   1 decade ago
Why int will be promoted to float. It could float be converted to int ?

Sharanu said:   1 decade ago
int is promoted 2 float then it will compare ....ans is correct.

Wikiok said:   1 decade ago
Solution: Automatical conversion x-> (float)x, so ((float)x == y) is TRUE.

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

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.

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

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?

Post your comments here:

Your comments will be displayed after verification.