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.

Kartik said:   8 years ago
int x = 3;
float y = 3.000000111111;
if(x == y)
printf("x and y are equal");
printf("x and y are not equal");
return 0;


int x = 3;
float y = 2.9999999;
if(x == y)
printf("x and y are equal");
printf("x and y are not equal");
return 0;

Now check this out. In both case you will find the same result. From my point of view an implicit conversion of float type is done by compiler after a certain limit.

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.

Mohamed Abusuhail said:   5 years ago

The correct solutions is Compiler assuming default all the declared values are in double value so, the first step initialize values to different data type then it will compile, the compiler takes it as double value even if it is any data type, and then compare a and b value both are equal. So the answer is "I LOVE YOU".

Meghana said:   8 years ago
The output will be: x = 2, y = 3, z = 3.


x = 4.

y = not assigned.

z = not assigned.

y = --x;

Performs a prefix decrements, so x will be 3 and y will be 3.

z = x--;

Performs a post-fix decrements operation so,

z = 3.

And x will decrements to 2 after being assigned to z.

Thus x = 2, y = 3, z = 3.

Aman said:   6 years ago
I have checked all the values.

When there is no fractional part like int 3.0 and float/double=3.0 then both are same (type conversion occur).

When there is values like 3.1 or 3.2 like, there if no type conversion.

Also if we put float y=3.0f, then also both int and float are equal.

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

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 ?

Mahesh said:   8 years ago
Hmm. I too tried it in compiler. It give "X and Y are equal".

How it possible?

Both are different data types. There should be apply type casting. But in this program it is not applicable. Can you clarified my doubt?

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

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.

Post your comments here:

Your comments will be displayed after verification.