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

Irfan said:   1 decade ago
Zulfiquar how can you say that float will be taken as int?Will you please elaborate.

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

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 ?

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

Harshal said:   1 decade ago
What is the diffrence b/w main, void main and int main?

Deepan Majumdar said:   1 decade ago
Implicit type conversion would occur in this case.


Post your comments here:

Your comments will be displayed after verification.