C Programming - Control Instructions - Discussion


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;

[A]. x and y are equal
[B]. x and y are not equal
[C]. Unpredictable
[D]. No output

Answer: Option A


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

Acute said: (Sep 1, 2010)  
As it is float it must be stored as 2. 999999 something like that. Then the comparison must take place.

Can anybody please explain me this?

Rajendra Pandey said: (Dec 13, 2010)  
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".

Maulik Patel said: (Feb 23, 2011)  
Here, int & float are different datatype,

So Comparative operator can't work with above condition.

So given ans is wrong.

Sundar said: (Feb 23, 2011)  
The given answer is correct only. Don't get confused.

I have tested the above program in Turbo C, GCC.

I got the same output as given in option A.

Output: x and y are equal

Wikiok said: (Mar 17, 2011)  
Solution: Automatical conversion x-> (float)x, so ((float)x == y) is TRUE.

Sharanu said: (May 1, 2011)  
int is promoted 2 float then it will compare ....ans is correct.

Vishwas said: (Jul 30, 2011)  
Why int will be promoted to float. It could float be converted to int ?

Vishwas said: (Jul 30, 2011)  
Why int will be promoted to float. It could float be converted to int ?

Dumanth said: (Aug 1, 2011)  
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 ?

Zulfiquar said: (Oct 3, 2011)  
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..

Raju Naidu said: (Oct 31, 2011)  
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.

Irfan said: (Sep 7, 2012)  
Zulfiquar how can you say that float will be taken as int?Will you please elaborate.

Harshal said: (Sep 21, 2012)  
What is the diffrence b/w main, void main and int main?

Prasad said: (Sep 26, 2012)  
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.

Sanghati Biswas said: (Oct 7, 2012)  
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.

Sandeep said: (Jul 17, 2014)  
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.

Tasaduq Hussain said: (Aug 30, 2014)  
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?

Saikrishna.Y said: (Nov 6, 2014)  
How an int value can be converted into float and compare automatically without using any comparative operator?

Deepan Majumdar said: (Jun 4, 2015)  
Implicit type conversion would occur in this case.

Shadab Khan said: (Jun 30, 2015)  
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.

Kartik said: (Jul 5, 2015)  
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.

Mahesh said: (Nov 13, 2015)  
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?

Souvik said: (Jan 4, 2016)  
int main()
int x=4,y,z;
printf("x=%d y=%d z=%d\n",x,y,z);

return 0;

What will be the output? Please explain.

Meghana said: (Jan 16, 2016)  
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.

Laksh said: (Jun 29, 2017)  
Here int is first promoted to float 3.0 and then the two values are compare which prints x and y are equal.

Aman said: (Nov 4, 2017)  
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.

Mohamed Abusuhail said: (Jun 23, 2018)  

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

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.