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.
Deepan Majumdar said:
1 decade ago
Implicit type conversion would occur in this case.
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?
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.
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.
Acute said:
1 decade ago
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?
Can anybody please explain me this?
Harshal said:
1 decade ago
What is the diffrence b/w main, void main and int main?
Irfan said:
1 decade ago
Zulfiquar how can you say that float will be taken as int?Will you please elaborate.
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.
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..
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 ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers