473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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?

Dec 28 '05 #1
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>>
Dec 28 '05 #2
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
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
Dec 28 '05 #4

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.

Dec 28 '05 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
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...
80
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...
17
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 ==...
25
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;
2
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...
6
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...
6
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...
11
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)
6
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():
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.