Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by iddw » Wed, 29 Oct 2003 06:04:17


On Mon, 27 Oct 2003 08:58:49 -0600, Morris Dovey < XXXX@XXXXX.COM >




You left out the operator in question (logical negation, "!"). In
n869, thats 6.5.3.3p5:

The result of the logical negation operator ! is 0 if the value of
its operand compares unequal to 0, 1 if the value of its operand
compares equal to 0. The result has type int. The expression !E is
equivalent to (0==E).

Regards,

-=Dave
--
Change is inevitable, progress is not.
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by iddw » Wed, 29 Oct 2003 08:27:55

On Mon, 27 Oct 2003 22:46:20 +0100, Guillaume



Can you name one? I can't. And I use some weird ones...


I'm not sure what your point is here.

If you want all 1's, use the bitwise negation operator (~) rather than
the logical negation operator. But there are other dangers. Note
that !2 == !1, but ~2 != ~1. That's one reason why we have both
logical and bitwise flavors of & and |.

If you're talking about assembly language efficiency, it depends on
the underlying processor architecture. It's nearly or exactly as
efficient to use XOR with 1.


it find to where know you Forth, want you If.


The committee had two choices: Have a single "true" value and
everything else is "false", or have a single "false" value and
everything else is "true." I think they made the right choice,
especially in the face of existing C implementations. You may
disagree.


More often, this feature is used in an expression like
"if (mask & 0x18)" which will be "true" if either (or both) bits 3 and
4 are set in "mask."


In C, the result of a relational or logical operator has type int.
There's no mixing. Unless you count the promotion of a _Bool to an
int in the usual integer promotions...


Not in C.


Cute. I wouldn't recommend it, but it's perfectly clear. I haven't
seen anybody advocate that sort of code, however...

Regards,

-=Dave
--
Change is inevitable, progress is not.

 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by Spehro Pef » Wed, 29 Oct 2003 22:21:51

On Mon, 27 Oct 2003 23:27:55 GMT, the renowned XXXX@XXXXX.COM (Dave


It's clear that one of the values should be 0 because testing for zero
is virtually always a fast operation in machine code. Whether you
choose 0 for TRUE or FALSE is a matter of taste.

A language that emphasized other things than speed and simplicity
might choose to interpret anything other than 0 or (say) 1 as an
invalid state and deal with it as such, but that is not in the C
philosophy.



With C you *can* write legitimate warning-free stuff like:

c += a = ++a == b++;

It might even make sense (at the time).

Best regards,
Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
XXXX@XXXXX.COM Info for manufacturers: http://www.yqcomputer.com/
Embedded software/hardware/analog Info for designers: http://www.yqcomputer.com/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by hoh » Wed, 29 Oct 2003 23:09:36

In article < XXXX@XXXXX.COM >,



You can write lots of legitimate code that looks strange, but this
one is not legitimate, it invokes undefined behaviour. The example
statement updates the variable ``a'' twice without an intervening
sequence point.

From ANSI/ISO/IEC 9899-1999:

6.5 Expressions ?2

2 Between the previous and next sequence point an object
shall have its stored value modified at most once by
the evaluation of an expression. Furthermore, the prior
value shall be read only to determine the value to be
stored.70)

70) This paragraph renders undefined statement
expressions such as
i = ++i + 1;
a[i++] = i;
while allowing
i = i + 1;
a[i] = i;


No. Even the C compiler isn't required to make sense of it.

--
Gan Larsson http://www.yqcomputer.com/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by buddy.spam » Wed, 29 Oct 2003 23:17:04


:
: With C you *can* write legitimate warning-free stuff like:
:
: c += a = ++a == b++;
:

You can, but the results of that statement may not be the same on all
compilers...too many side-effects.

Okay, it _might_ be the same on all compilers, but it might not :)

Nice code though!

(On a side note, i really want to kick my self when i use macros with
++...ie FOO(c++) or FOO(++c), especially if FOO uses it's argument more
than once...

even worse is FOO(a+b,c) which could do anything, if FOO() isn't written
properly (ala #define FOO(ab,c) ((ab) * (c) + (ab))

Without the extra ( ), that macro won't work at all with an arg of a+b.
The preprocessor can really bite you when it wants to :)

Regards,

--buddy

--
Remove '.spaminator' and '.invalid' from email address
when replying.
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by iddw » Wed, 29 Oct 2003 23:43:17

On Tue, 28 Oct 2003 13:21:51 GMT, Spehro Pefhany


[...]

Well, you can write it, and it need not generate a diagnostic, but
it's not legitimate. It invokes undefined behavior. The variable a
is modified twice between sequence points.

However,

c += d = ++a == b++;

would work to illustrate your point. And as you say, might even make
sense at the time, though it wouldn't get through my code review
without at least one, and most likely two sets of parentheses...

Regards,

-=Dave
--
Change is inevitable, progress is not.
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by Spehro Pef » Thu, 30 Oct 2003 00:03:32

On Tue, 28 Oct 2003 14:09:36 GMT, the renowned XXXX@XXXXX.COM



<snip>

Good point. One would need to add a "," in there to make it valid C.
I was under the (mistaken) impression that "==" was guaranteed to
provid a sequence point (as || and && do).

(It does happen to compile without warnings and do what you'd expect
with a few compilers I tried it on, FWTF*T*W- NM).

Best regards,
Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
XXXX@XXXXX.COM Info for manufacturers: http://www.yqcomputer.com/
Embedded software/hardware/analog Info for designers: http://www.yqcomputer.com/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by hoh » Thu, 30 Oct 2003 00:56:13

In article < XXXX@XXXXX.COM >,




That would not help. Placing a sequence point at ``=='' does
not insert a sequence point between the ``a ='' and ``++a''
in ``a = ++a''.

--
Gan Larsson http://www.yqcomputer.com/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by Spehro Pef » Thu, 30 Oct 2003 02:50:31

On Tue, 28 Oct 2003 15:56:13 GMT, the renowned XXXX@XXXXX.COM




If we replace "==" with "||", it would put a sequence point at the end
of the first operand to "||".

Best regards,
Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
XXXX@XXXXX.COM Info for manufacturers: http://www.yqcomputer.com/
Embedded software/hardware/analog Info for designers: http://www.yqcomputer.com/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by iddw » Thu, 30 Oct 2003 04:33:48

On Tue, 28 Oct 2003 17:50:31 GMT, Spehro Pefhany


[...]
[...]

That only helps if a == -1 before the statement is executed (which
would result in ++a == 0 and force the execution of b++).

It also changes the meaning of the expression...

You could fix it with a comma operator and a temporary:

c += t = ++a == b++, a=t;

Or, since we're apparently intending to throw away the result of ++a
anyway,

c += a = a+1 == b++;

Regards,

-=Dave
--
Change is inevitable, progress is not.
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by Dennis Cla » Thu, 30 Oct 2003 07:34:22


Or we could just write readable code and leave the games at the door with
the "most obtuse C code" competition every year. Really guys, writing
obscure code does not make it run faster, in fact, often just the reverse.

I've seen for{} loops written where all the processing occurs in the ()
section and nothing in the {} brackets - Who the heck is that code for?
It sure isn't for the poor sot that has to support it!

Look at your architecture, write your code the way your processor works
and stop trying to play "stump the chump" with the compiler. With embedded
work, you'll often get better performance and smaller code size.

IMO,
DLC

: On Tue, 28 Oct 2003 15:56:13 GMT, the renowned XXXX@XXXXX.COM


:>In article < XXXX@XXXXX.COM >,

:>
:>> >> c += a = ++a == b++;
:>
:>> Good point. One would need to add a "," in there to make it valid C.
:>> I was under the (mistaken) impression that "==" was guaranteed to
:>> provid a sequence point (as || and && do).
:>
:>That would not help. Placing a sequence point at ``=='' does
:>not insert a sequence point between the ``a ='' and ``++a''
:>in ``a = ++a''.

: If we replace "==" with "||", it would put a sequence point at the end
: of the first operand to "||".

: Best regards,
: Spehro Pefhany
: --
: "it's the network..." "The Journey is the reward"
: XXXX@XXXXX.COM Info for manufacturers: http://www.yqcomputer.com/
: Embedded software/hardware/analog Info for designers: http://www.yqcomputer.com/

--
============================================================================
* Dennis Clark XXXX@XXXXX.COM www.techtoystoday.com *
* "Programming and Customizing the OOPic Microcontroller" Mcgraw-Hill 2003 *
============================================================================
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by Chris Hill » Fri, 31 Oct 2003 00:30:19

In article <D8jnb.23$ XXXX@XXXXX.COM >, Morris Dovey
< XXXX@XXXXX.COM > writes


I though that was the document that everyone was quoting? What other
document is the C standard (except perhaps the 9899:1990 which many
compilers still use)

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/\
/\/\/ XXXX@XXXXX.COM www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by hoh » Fri, 31 Oct 2003 01:58:35

In article <NYFxlLAL09n$ XXXX@XXXXX.COM >,




The document known as n869 is a committee draft of the
ISO/IEC 9899:1999 standard document. The n869 draft was
created early in 1999 and was available for a while from
the C standards working group as a free download. Several
changes were made between the n869 draft and the final
standard.

Some people still quote from the obsolete n869, perhaps
because it can be downloaded from the net (not officially),
they like to create confusion, or are too poor to spend, IIRC,
US$14 to buy the real document.


The 9899:1990 is no longer current and has been retracted.
It consisted of:

ISO/IEC 9899:1990 (standard)
ISO/IEC 9899 AM1 (amendment, 1995)
ISO/IEC 9899 TCOR1 (technical corrigendum 1, 1995)
ISO/IEC 9899 TCOR2 (technical corrigendum 2, 1996)

The only documents describing the current C standard are:

ISO/IEC 9899:1999 (standard)
ISO/IEC 9899 Cor1 (corrigendum 1, 2001)

--
Gan Larsson http://www.yqcomputer.com/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by Chris Hill » Sat, 01 Nov 2003 03:56:45

In article < XXXX@XXXXX.COM >, Goran Larsson < XXXX@XXXXX.COM >
writes


This is correct except that BSI has made
ISO/IEC 9899 TCOR2 (technical corrigendum 2, 1996)
available again simply because a lot of compilers still work to is and
are not moving to C99. Also there are quite a few standards that still
reference C90 specifically. As many of these are in the embedded, safety
critical and high integrity areas companies are required to have copies
of the base standards.

AFAIK there are currently only 2 compilers claiming C99 compliance
anyway.

However any one who starts quoting a committee draft is asking for
trouble!!

Regards
Chris
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/\
/\/\/ XXXX@XXXXX.COM www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
 
 
 

Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Post by hoh » Sat, 01 Nov 2003 04:44:02

In article <CQzyHKAt7Vo$ XXXX@XXXXX.COM >,



The reason for making the ISO/IEC 9899:1990 series available again
was that the current C++ standard refers to 9899:1990. That does not
make it a valid C standard, only an apendix to the C++ standard. :-)


That is why it was made available again. It has still been superseeded
by the 9899:1999 standard.


True. There is a reason why it was removed from the website run by the
working group.

--
Gan Larsson http://www.yqcomputer.com/