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

Mohamed Abusuhail said:   6 years ago
@Dumanth.

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".
(2)

Aman said:   7 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.

Laksh said:   7 years ago
Here int is first promoted to float 3.0 and then the two values are compare which prints x and y are equal.
(1)

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

Initially,

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.

Souvik said:   9 years ago
#include<stdio.h>
int main()
{
int x=4,y,z;
y=--x;
z=x--;
printf("x=%d y=%d z=%d\n",x,y,z);

return 0;
}

What will be the output? Please explain.

Mahesh said:   9 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?
(1)

Kartik said:   9 years ago
int x = 3;
float y = 3.000000111111;
if(x == y)
printf("x and y are equal");
else
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");
else
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.

Shadab khan said:   9 years ago
Since float is bigger datatype than int so it implicitly typecast int into float. That's why both the x and y of float type then the comparison is true.

Deepan Majumdar said:   9 years ago
Implicit type conversion would occur in this case.

Saikrishna.y said:   10 years ago
How an int value can be converted into float and compare automatically without using any comparative operator?
(1)


Post your comments here:

Your comments will be displayed after verification.