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 2 of 3.
Raju Naidu said:
1 decade ago
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.
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:
1 decade ago
Zulfiquar how can you say that float will be taken as int?Will you please elaborate.
Harshal said:
1 decade ago
What is the diffrence b/w main, void main and int main?
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.
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.
Sandeep said:
1 decade ago
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:
1 decade ago
If y = 3.0000001. Then output iz x and why are equal.
But,
When y = 3.000001. The output iz x and why are not equal.
What is reason behind it?
But,
When y = 3.000001. The output iz x and why are not equal.
What is reason behind it?
Saikrishna.y said:
1 decade ago
How an int value can be converted into float and compare automatically without using any comparative operator?
(1)
Deepan Majumdar said:
1 decade ago
Implicit type conversion would occur in this case.
Shadab khan said:
1 decade 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