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

  • Thread starter Thread starter pairofstrings
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the evaluation of the expression "int i = x < y < z;" in C programming, specifically why it results in the value "1". Participants explore the mechanics of the expression, the implications of variable initialization, and the behavior of the clrscr() function.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions why the expression evaluates to "1" and whether it could yield a garbage value or zero in other cases.
  • Another participant explains that the expression is evaluated as "i = ((x < y) < z);" and clarifies that since (x < y) is true, it leads to "i = (1 < z);" which is also true, resulting in "i" being set to "1".
  • A participant raises a scenario where (x < y) is false, suggesting that it would be interpreted as "0", leading to "i = (0 < z);" and questions if this would also result in "1".
  • There is a discussion about the potential for "i" to display a garbage value if it is declared without initialization.
  • Participants note that clrscr() is a non-standard function and is not necessary for the program to run.

Areas of Agreement / Disagreement

Participants generally agree on the evaluation process of the expression but there is uncertainty regarding the behavior of uninitialized variables and the implications of the clrscr() function. The discussion remains unresolved regarding the exact conditions under which a garbage value might appear.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about variable initialization and the non-standard nature of certain functions like clrscr().

pairofstrings
Messages
411
Reaction score
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


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.
 


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:


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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
2K
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
63
Views
6K
  • · Replies 6 ·
Replies
6
Views
6K