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

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.

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

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;

printf(" I love you\n");
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..

Post your comments here:

Your comments will be displayed after verification.