454,385 Members | 1,737 Online
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

# does the logical expression return 1 ( if it is true ) absolutely?

 P: n/a it's likely that the compiler could only promise returning a non-zero value in my impression, but it did return 1 every time, like the code below: int a=3,b=8; printf("%d\n",a
6 Replies

 P: n/a On 27 Dec 2005 20:21:06 -0800, "moosdau" wrote: it's likely that the compiler could only promise returning a non-zerovalue in my impression,but it did return 1 every time,like the code below:int a=3,b=8;printf("%d\n",a> Dec 28 '05 #2

 P: n/a thanks very much! I didn't read K&R 's book when I learn c. the book I read only refer that the value is non zero,didn't mention it must be 1. now I know.:) Dec 28 '05 #3

 P: n/a Barry Schwarz wrote: On 27 Dec 2005 20:21:06 -0800, "moosdau" wrote:it's likely that the compiler could only promise returning a non-zerovalue in my impression,but it did return 1 every time,like the code below:int a=3,b=8;printf("%d\n",a (greater than), <= | (less than or equal to), and >= (greater than or equal to) | shall yield 1 if the specified relation is true and 0 if | it is false. | The result has type int. +--------------------------------------- and 6.5.9 (Equality Operators) goes on to define the == and != as above. -- (Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt (clc FAQ) http://c-faq.com Dec 28 '05 #4

 P: n/a moosdau wrote: it's likely that the compiler could only promise returning a non-zero value in my impression, but it did return 1 every time, like the code below: int a=3,b=8; printf("%d\n",a, >=) or equality (==, !=) expression is always either 1 or 0, and is always of type int. It is also true that any non-zero integral expression in a Boolean context will evaluate to true, so given an int variable i=123, if (i != 0) {/* do stuff */} and if (i) {/* do stuff */} are equivalent. Dec 28 '05 #5

 P: n/a It is a usual confusion: one thing is that the "if" statement goes to the "then" part for any value different of 0 and another one is what is returned by C operators. Dec 28 '05 #6

 P: n/a moosdau wrote: thanks very much! I didn't read K&R 's book when I learn c. the book I read only refer that the value is non zero,didn't mention it must be 1. now I know.:) What confuses some people is that while equality and relational operators evaluate to 0 and 1, the operands of if(), while(), ?:, ||, &&, !, and middle operand of for(;;) are compared with 0, so any non-zero value will cause the same result of the test. -- Thad Dec 29 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.