# 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;
}
```

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.
Kartik said:
8 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.

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.

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.

@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

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

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)

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.

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.

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.

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

Hence it prints "x and y are equal".

Dumanth said:
1 decade ago

void main()

{

float a=1.1;

double b=1.1;

if(a==b)

{

printf(" I love you\n");

}

else

{

printf(" I hate you\n");

}

}

If you execute this code you will get output as I hate you..

then How this is happening ?

{

float a=1.1;

double b=1.1;

if(a==b)

{

printf(" I love you\n");

}

else

{

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?

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)

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

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.

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:

Quick links

Quantitative Aptitude

Verbal (English)

Reasoning

Programming

Interview

Placement Papers