Why Does the Answer to "int i=x<y<z;" Equal "1"?

  • Thread starter pairofstrings
  • Start date
In summary: The value of i is not initialized, so it can contain any value. However, in some cases it may happen to contain 0 or 1 (like in this example), but this cannot be relied upon. It is best practice to always initialize variables before using them to avoid unexpected results.
  • #1
pairofstrings
411
7
Why is the answer "1"?

Here is a program :

#include<stdio.h>
#include<conio.h>
void main()
{
int i,x=10,y=20,z=5;
clrscr();
i=x<y<z;
printf("%d\n",i);
getch();
}


I am getting the answer as "1". Why? Isn't this suppose to give garbage value if not "0" (zero). Also, please tell me the cases when the answer could be a garbage value or a zero. Please give simple examples. And can you tell me why clrscr(); syntax has to be typed after integer (int) declaration? - Or else it will give you an error.
Thank you very much.
 
Last edited:
Technology news on Phys.org
  • #2


Hi pairofstrings! :smile:

The expression evaluated: i = x < y < z
is not really a proper expression.

It is evaluated as: i = ((x < y) < z);
Since (x < y) is true, this evaluates as: i = (true < z).
Next true is interpreted as "1", so this evaluates as: i = (1 < z);
Again (1 < z) is true, so the value stored in the integer i is "1".

The clrscr() is a non-standard function call that is not necessary.
Its function is to clear the screen.

If you had just declared i without giving it a value, it would have been displayed as garbage.
 
  • #3


I like Serena said:
i = ((x < y) < z);
Since (x < y) is true, this evaluates as: i = (true < z).
Next true is interpreted as "1", so this evaluates as: i = (1 < z);
Again (1 < z) is true, so the value stored in the integer i is "1".

What happens when (x<y) is false. Is this interpreted as "0" and this is evaluated as i=(0< z);
then ( 0< z)which is true here since z=5. Now the value of "i" stored is "1" again. Am I right here?

And if I declared i= nothing and then give a print statement then the value is displayed as garbage?

#include<stdio.h>
#include<conio.h>
void main()
{
int i,x=10,y=20,z=5;
clrscr();
printf("%d\n",i);
getch();
}
Will this return a garbage value?
 
Last edited:
  • #4


pairofstrings said:
What happens when (x<y) is false. Is this interpreted as "0" and this is evaluated as i=(0< z);
then ( 0< z)which is true here since z=5. Now the value of "i" stored is "1" again. Am I right here?

Yes.

Will this return a garbage value?

In general - yes.
 
  • #5


The answer to "int i=x<y<z;" equals "1" because of the way that the C programming language evaluates expressions. In C, relational operators like "<" are evaluated from left to right. So in this case, the expression is first evaluated as "x<y", which evaluates to "true" since 10 is less than 20. This "true" value is then converted to an integer, which becomes "1". Then, the expression "1<z" is evaluated, which is also true since 1 is less than 5. This final "true" value is again converted to an integer, which remains "1". Therefore, the answer to "int i=x<y<z;" is "1".

In general, the answer to "int i=x<y<z;" could be any integer value, depending on the values of x, y, and z. For example, if x=10, y=5, and z=20, the answer would be "0" since the first expression "x<y" would evaluate to "false" and be converted to "0". The final expression would also evaluate to "false" and remain "0". On the other hand, if x=5, y=10, and z=20, the answer would be "1" since both expressions would evaluate to "true" and be converted to "1".

The answer could also be a garbage value or a zero in certain cases. For example, if x=10, y=0, and z=5, the expression "x<y" would evaluate to "false" and be converted to "0". However, the expression "0<z" would result in a division by zero error, which would produce a garbage value. Similarly, if x=0, y=0, and z=0, the expression "x<y" would evaluate to "false" and be converted to "0". The final expression "0<z" would also evaluate to "false" and remain "0".

As for the "clrscr();" syntax, this is a function that is used to clear the screen in C. It must be included after the "int" declaration because the "getch();" function will pause the program and wait for user input before the screen is cleared. If the screen is not cleared before the program pauses, the user will not be able to see the output of the program. Therefore, it is best
 

Related to Why Does the Answer to "int i=x<y<z;" Equal "1"?

What does the statement "int i=x

The statement "int i=x

Why does the result of "int i=x

The result of "int i=x

Can the statement "int i=x

Yes, the statement "int i=x

What happens if the values of x, y, or z are changed?

If the values of x, y, or z are changed, the result of the statement "int i=x

Can this statement be used with other data types besides integers?

Yes, the statement "int i=x

Similar threads

  • Programming and Computer Science
Replies
4
Views
923
  • Programming and Computer Science
Replies
4
Views
766
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
680
  • Programming and Computer Science
Replies
3
Views
1K
Replies
63
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K

Back
Top