473,387 Members | 3,684 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.

Boolean OR (double pipe) and ISO C99


Hello, I hate to make a fool of myself, but:
in C99, is

var = ((exp1) || (exp2));

necessarily equivalent to

int temp = (exp1); var = (temp != 0 ? temp : (exp2));

as I believe it was in pre-99 C? gcc seems to compile it rather as

var = ((exp1) != 0 || (exp2) != 0)

ie. the final value of var is always 0 or 1.

If this latter translation is indeed correct, can anyone think of
a way to achieve the old effect without a temporary variable?
This code needs to go into a macro, and exp1 and exp2 are potentially
complex expressions involving function calls and side effects.

--
"It's not true or not." A reality show producer (real quote)
Nov 14 '05 #1
3 2408
Ian Zimmerman wrote:
Hello, I hate to make a fool of myself, but:
in C99, is

var = ((exp1) || (exp2));

necessarily equivalent to

int temp = (exp1); var = (temp != 0 ? temp : (exp2));

as I believe it was in pre-99 C?
It was not. || gives you 1 if one of its operands is
not equal to 0.
gcc seems to compile it rather as

var = ((exp1) != 0 || (exp2) != 0)

ie. the final value of var is always 0 or 1.
This is correct behaviour.
The standard did not change in that respect.

If this latter translation is indeed correct, can anyone think of
a way to achieve the old effect without a temporary variable?
This code needs to go into a macro, and exp1 and exp2 are potentially
complex expressions involving function calls and side effects.


Umh, I fear you will need some sort of "var" as above if you
do not want the whole thing too complicated.
I would either go for
(var=(exp1)) || (var=(exp2))
or
(var=(exp1)) ? (var) : (var=(exp2))
and pass var, calling the macro SET_VAR_....(var, ....)
or "return"
(var=(exp1)) ? (var) : (exp2)

<OT>
BTW: Within the last couple of months, there was a discussion
of a GNU extension which would have permitted you solving
your problem by (exp1)?:(exp2). Maybe you can find other
interesting arguments/alternatives there.

http://groups.google.de/groups?selm=....cs.purdue.edu
</OT>
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2
On 11 Dec 2004 23:11:06 -0800, Ian Zimmerman <it*@buug.org> wrote in
comp.lang.c:

Hello, I hate to make a fool of myself, but:
in C99, is

var = ((exp1) || (exp2));

necessarily equivalent to

int temp = (exp1); var = (temp != 0 ? temp : (exp2));
No, the result of both the || and && operators has always been an int
with the value of 0 or 1.
as I believe it was in pre-99 C? gcc seems to compile it rather as
Where did you come up with this belief? It has never been correct.
var = ((exp1) != 0 || (exp2) != 0)

ie. the final value of var is always 0 or 1.
Yes, the final value must always be 0 or 1. From the day that these
operators were added to the language, before the publication of the
first edition of the C Programming Language in 1978.
If this latter translation is indeed correct, can anyone think of
a way to achieve the old effect without a temporary variable?
This code needs to go into a macro, and exp1 and exp2 are potentially
complex expressions involving function calls and side effects.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
int temp = (exp1); var = (temp != 0 ? temp : (exp2));


Jack> No, the result of both the || and && operators has always been an
Jack> int with the value of 0 or 1.

Ian> as I believe it was in pre-99 C? gcc seems to compile it rather as

Jack> Where did you come up with this belief? It has never been
Jack> correct.

From reading and writing too much Lisp/Scheme, no doubt.
;-)

Anyway, I found a workaround with a bit of refactoring; the function call
in exp2 now takes the value of exp1 as an argument, and does nothing if
the latter is nonzero.

--
"It's not true or not." A reality show producer (real quote)
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
1
by: jenny | last post by:
Hi, I have a java socket program running on AIX 4.3.3.0 platform. It opens a socket and sends data to our customer over a leased fractional T1 line. The line is always connected. However,...
5
by: richard | last post by:
I have a simple test to pass information from a client to a server using named pipe. what I really want is: when I type a line on the client, the server will output the line immediately. but to my...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
2
by: FB's .NET Dev PC | last post by:
I am writing two services in VB.NET, one of which needs to send text strings to the other. After reading, I decided (perhaps incorrectly) that named pipes would be the best interprocess...
0
by: phillip.s.powell | last post by:
SELECT id, student_first_name, student_last_name, major, minor , MATCH(major, minor) AGAINST ('"mechanical engineering"') AS score FROM students WHERE MATCH(major, minor) AGAINST ('"mechanical...
2
by: Steve R. Hastings | last post by:
While studying iterators and generator expressions, I started wishing I had some tools for processing the values. I wanted to be able to chain together a set of functions, sort of like the...
0
by: Malte | last post by:
Hello there, We got a problem atm with our 20k database. Everytime we search for sth. with double quotes it takes much more longer than without. Here is what we did: mysql> SELECT COUNT(*)...
2
by: otorojava | last post by:
The script works but I'm having a problem reading double and boolean import java.io.*; import java.util.StringTokenizer; import javax.swing.JOptionPane; public class FileAddress {
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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.