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

  • Thread starter Thread starter pairofstrings
  • Start date Start date
AI Thread Summary
The expression "int i = x < y < z;" evaluates to "1" because it is processed as "i = ((x < y) < z);". Since x (10) is less than y (20), this results in true, which is interpreted as "1", making the next comparison "1 < z" (where z is 5), which is also true. If the initial comparison (x < y) were false, it would evaluate as "i = (0 < z)", resulting in "1" again since 0 is less than 5. Declaring an integer without initializing it will typically result in a garbage value when printed. The clrscr() function is non-standard and not necessary for the program's functionality.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top