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

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Quoted text here. Click to load it

What really happens though is that "a+b" is an integer, and the
expression wants a boolean.  The computer then applies builtin
integer-to-boolean conversion rules with no typecasting required.
Ie, "5" is not a boolean value, but it is trivally cast to a boolean.

This is analogous to adding a 'char' to an 'int'; the char is
trivially converted to an int before the addition is done.

--
Darin Johnson
    Laziness is the father of invention

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

Quoted text here. Click to load it

Um, no.  Fron n869 6.8.4.1 (The if statement) paragraph 2 (semantics)

   In both forms, the first substatement is executed if the expression
   compares unequal to 0.  In the else form, the second substatement
   is executed if the expression compares equal to 0.

There is no typecasting.  Until C99, there is no boolean type.  Even
in C99, values of type _Bool are promoted (to int in most cases)
before any operators can be applied.  E.g., given

   _Bool b;

   if (b != 0)

The value of b is first promoted to int (by the usual arithmetic
conversions) before being compared to (the int constant) 0.  Even with

   if (b != (_Bool)0)

The zero is cast to _Bool, which is then promoted to int.  In all
cases, the result of the != operator has type int.

Regards,

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

Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On Mon, 27 Oct 2003 22:46:20 +0100, Guillaume

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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."

Quoted text here. Click to load it

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...

Quoted text here. Click to load it

Not in C.

Quoted text here. Click to load it

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.

Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On Mon, 27 Oct 2003 23:27:55 GMT, the renowned snipped-for-privacy@hotmail.com (Dave
Quoted text here. Click to load it

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.


Quoted text here. Click to load it

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"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Quoted text here. Click to load it

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;

Quoted text here. Click to load it

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

--
Göran Larsson     http://www.mitt-eget.com /

Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On Tue, 28 Oct 2003 14:09:36 GMT, the renowned snipped-for-privacy@invalid.invalid

Quoted text here. Click to load it
<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"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Quoted text here. Click to load it


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

--
Göran Larsson     http://www.mitt-eget.com /

Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On Tue, 28 Oct 2003 15:56:13 GMT, the renowned snipped-for-privacy@invalid.invalid

Quoted text here. Click to load it

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"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On Tue, 28 Oct 2003 17:50:31 GMT, Spehro Pefhany

[...]
Quoted text here. Click to load it
[...]
Quoted text here. Click to load it

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.

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

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 snipped-for-privacy@invalid.invalid
: (Goran Larsson) wrote:

:>
:>> >> 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"
: snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
: Embedded software/hardware/analog  Info for designers:  http://www.speff.com

--
============================================================================
* Dennis Clark         snipped-for-privacy@frii.com                www.techtoystoday.com   *
We've slightly trimmed the long signature. Click to see the full one.
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
:
: 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.


Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On Tue, 28 Oct 2003 13:21:51 GMT, Spehro Pefhany

[...]
Quoted text here. Click to load it

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.

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

Quoted text here. Click to load it

NO it isn't.  Any non-zero value is true.  0xF0 is "true".  Doing a 1's
compliment on that results in 0x0F: also true.  Using 1's compliment to
impliment "!" is quite simply NOT C.

--
Grant Edwards                   grante             Yow!  Where's th' DAFFY
                                  at               DUCK EXHIBIT??
We've slightly trimmed the long signature. Click to see the full one.
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Quoted text here. Click to load it

I guess it requires you to think like a programmer.

Quoted text here. Click to load it

We're not talking about the majority of people.  The majority
of people can't read C period.  Or any other programming
languages.  We aren't going to stop writing software, to please
the majority of people, are we?

Quoted text here. Click to load it

I can't agree.  The concept of a boolean express is _so_ basic
to computer science that "banning" it would be silly.

--
Grant Edwards                   grante             Yow!  I hope the
                                  at               "Eurythmics" practice birth
We've slightly trimmed the long signature. Click to see the full one.
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Quoted text here. Click to load it

Nope ...  we invent COBOL so they can read it.  Of course, they
*STILL* don't understand it ...

George


Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Hi,

Quoted text here. Click to load it

I bileve that every one can agree that something like

if (a > b)
        {
        ...
        }

is ok, so where's the big leap to

c = (a > b);

and from there to

c = (a == b);

I must admit while I was learning C that the concept was slightly strange to
me, but by the same token learning a new language always presents slightly
different takes on the same subject area.

Once you get it into your head that operators such as <, > and == are
basically treated the same as +, -, and * etc I don't see the need to
expand it over multiple lines into an if/then/else kind of statement. All
you are doing is saying, if this is true the answer is true, if this is
false the answer is false, i.e. needless duplication.

You wouldn't suggest the following needs expansion would you?

c = a + b;

Thanks,
Christopher Fairbairn

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

Quoted text here. Click to load it

switch (a+b)
 {
 case 0: c = ZERO; break;
 case 1: c = ONE; break;
 case 2: c = TWO; break;
 default: c= MANY; break;
 }

;)
 
--
Grant Edwards                   grante             Yow!  Now I'm telling MISS
                                  at               PIGGY about MONEY MARKET
We've slightly trimmed the long signature. Click to see the full one.
Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
This rather lengthy thread just humbly reminds us that "I don't
understand it" doesn't necessarily mean "It's badly written".

To go a bit further, just because we don't understand something, doesn't
mean that this thing is wrong. Just because it took us hundreds of
thousands of years to figure out that the Earth was round, doesn't mean
that it should have been flat instead, to save us all some sweating.


Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
This rather lengthy thread just humbly reminds us that "I don't
understand it" doesn't necessarily mean "It's badly written".

To go a bit further, just because we don't understand something, doesn't
mean that this thing is wrong. Just because it took us hundreds of
thousands of years to figure out that the Earth was round, doesn't mean
that it should have been flat instead, to save us all some sweating.



Site Timeline