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

Tasaduq Hussain said:   9 years ago
If y = 3.0000001. Then output iz x and why are equal.

When y = 3.000001. The output iz x and why are not equal.

What is reason behind it?

Sandeep said:   9 years ago
int and float are 4 byte and compiler converts int to float while comparing both, while double is of 8 byte so we can't compare float and double, as I think, please let me know if I am wrong.

Sanghati biswas said:   1 decade ago
Listen up everyone. This is happening only when we are giving the values like 3. 1, 1.2 i.e. when there is any fractional part.

@Dumanth in case of float 1.1 is not stored in a. What is actually stored in a is slightly less than 1. 1 (1.099999988 appx) and in case of double (1.099999999) and in case of long double exactly 1.1 gets stored. (because float, double, long double take 4, 8, 10 respectively) so the else part is get executed. But when the we are giving the values like 3.0. No fractional part. So if part gets executed.

Prasad said:   1 decade ago
Decimal range between .0 to .5 is treated as float.

Example 3.5 is a float no need to write as 3.5f.

3.8 is double because its range greater than (see after decimal point) than .5 range so it is double.

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

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

Raju Naidu said:   1 decade ago
float a=3.02f
then it'l take a float value

If you are mentioned any decimal number without mentioning the 'f' symbol at the end it treated as double.

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

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 ?

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

Post your comments here:

Your comments will be displayed after verification.