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.
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".
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)
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)
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?
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)
Saikrishna.y said:
10 years ago
How an int value can be converted into float and compare automatically without using any comparative operator?
(1)
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.
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.
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.
Meghana said:
9 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.
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.
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers