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<b);
will it return 1 absolutely in any case? 6 4204
On 27 Dec 2005 20:21:06 -0800, "moosdau" <mo*****@gmail.com> 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<b);
will it return 1 absolutely in any case?
Yes. To quote from K&R: "By definition, the numeric value of a
relation or logical expression is 1 if the relation is true and 0 if
the relation is false." I really doubt the new standard would change
this since a lot of code depends on it.
<<Remove the del for email>>
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.:)
Barry Schwarz wrote: On 27 Dec 2005 20:21:06 -0800, "moosdau" <mo*****@gmail.com> 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<b);
will it return 1 absolutely in any case?
Yes. To quote from K&R: "By definition, the numeric value of a relation or logical expression is 1 if the relation is true and 0 if the relation is false." I really doubt the new standard would change this since a lot of code depends on it.
You are right, there's no change in this aspect, and C99 also defines it
as 1 and 0.
To quote from std:
+----C99:6.5.8 Relational operators-----
|6. Each of the operators < (less than), > (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
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<b);
will it return 1 absolutely in any case?
Yes. The result of a relational (<, <=, >, >=) 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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Roy Yao |
last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ?
According to my analysis, operator sizeof, (type) and * have the same
precedence, and they combine from right to left. Then this...
|
by: Christopher Benson-Manica |
last post by:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one...
|
by: orekinbck |
last post by:
Hi There
Say I want to check if object1.Property1 is equal to a value, but
object1 could be null.
At the moment I have code like this:
if (object1 != null)
{
if (object1.Property ==...
|
by: August Karlstrom |
last post by:
Hi,
Can someone explain the reason for the warning from GCC below:
<shell-session>
$ cat test.c
#include <stdbool.h>
bool a, b, c, d;
|
by: Brian Kitt |
last post by:
I have a process where I do some minimal reformating on a TAB delimited
document to prepare for DTS load. This process has been running fine, but I
recently made a change. I have a Full Text...
|
by: Protoman |
last post by:
I'm writing a program to calc truth tables of arguments to prove that
they are logically valid. Currently, I have to tell the program to
print a truth table, and check it by hand to prove it's...
|
by: Ludwig |
last post by:
Hi,
i'm using the regular expression \b\w to find the beginning of a word,
in my C# application. If the word is 'public', for example, it works.
However, if the word is '<public', it does not...
|
by: Joe HM |
last post by:
Hello -
I have the following very simple code ...
Dim lStringA As String = ""
Dim lStringB As String = ""
....
lStringB = Replace(lStringA, "DUMMY", "", Compare:=CompareMethod.Text)
|
by: Paddy |
last post by:
Hi,
# If I have a function definition
def f1(arg):
global capturecall
if capturecall:
...
do_normal_stuff(arg)
# and its later use:
def f2():
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |