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

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 )
: snipped-for-privacy@ieee.org.invalid wrote in
:
:>
:>: c = a == b ? !0 : 0;
:>
:> Where's a good gun when i need it....
:>
:> you should at LEAST use ( ) around that...
:
: No need, ?: is near the bottom of the precedence barrel, == (much higher

I meant for clarity... it's much easier to see what's going on with
explicit ( ) around the equality test.  I prefer code that I can glance at
and see what's going on, without having to read the whole line. ( ) enable
that to happen more easily.  and i'm no LISP user :)

Using the ternary (not 'trinary') operator without parenthesis is no
cleaner than the orginal c = (a == b).  now, c = a == b should be taken
out and shot :)

ttyl,

--buddy

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


Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Quoted text here. Click to load it

It may be concise but it isn't clear when scanning code (= looks like ==
when scanning code you're unfamiliar with). It's better to be explicit about
it than save typing. I've seen lots of obscure bugs get induced when the
intent of the author was not clear in his code.

Quoted text here. Click to load it

Which also takes 3 readings to figure out what it's supposed to do by anyone
who isn't familiar with the code overall. It doesn't read well and it
doesn't show intent.  And if you have to go look at the book or remember
operator precedence, you've already failed the readability and
maintainability aspect.

-->Neil

-->Neil



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

Quoted text here. Click to load it
about

Of course, you're entirely correct.  At the same time, C has been around a
long time, and has enough flexibility to allow programmers several ways to
get the same task done.  It's not surprising that C has developed some
quirky idioms over the years.

One can complain about the idioms, or one can be happy that the language
allows programmers that much freedom of expression.  There's a reason the
Pascal/Ada family isn't more widely used.

At the same time, there's probably such a thing as too much freedom of
expression - I've had to maintain some extremely disgusting Perl code in my
time.  In general I just commented the old stuff out and rewrote it in a
less idiomatic style.

Kelly



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

[snip]

Quoted text here. Click to load it

if (a)
   statement
else
   statement

This is four lines that I must parse.


lval = cond ? !0 : 0;

This is one line that the brain easily "chunks" in one pass.

I simply disagree with your statement of how many passes it takes to
comprehend something as fundamental as the the ternary operator.

Regards.

--
- Mark ->
--

Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Quoted text here. Click to load it

The issue isn't the # of lines, but the readability and flow. It's *EASY* to
miss a question mark. It 'aint easy to miss the above even though it's more
verbose.

Quoted text here. Click to load it

As one who integrates code from 4 different groups around the world (a total
of 30 engineers), I'll show you quite a few who would completely blow that
statement.

Quoted text here. Click to load it

Never said it was hard to comprehend. I said it was easy to miss. Burying a
question mark in the middle of an expression is just a bad idea.

-->Neil



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

Quoted text here. Click to load it


I think the first example is much clearer.  The intent is to set the
variable c to the result of the boolean expression (a==b), and that's
exactly what the original line of code does -- they even paranthised it to
make it clearer.  Why spread it out over 4 lines of code and require me to
go look up TRUE and FALSE in some header file somewhere??

--
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
(Snip)
Quoted text here. Click to load it

Because the first example requires a different way of thinking,
and it's not the way the majority of people think.  It's more
of a "hotshot" way of thinking.  I'm not saying it's bad, but
if the code may be maintained by others in the future, then it
should be written in the 2nd method.

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

Quoted text here. Click to load it

I don't think it's a hotshot way of thinking.  Maybe if you use
"a", "b", and "c" as variable names it's obtuse, but if you use
more realistic names it can be clear.  Such as:
  matchFound = (index == key);

--
Darin Johnson
    Luxury!  In MY day, we had to make do with 5 bytes of swap...

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

Quoted text here. Click to load it

   I'll bet a dollar that FALSE is always defined as 0 or NULL or some
such equivalent. I won't bet on the value of TRUE other than it's not
0 nor NULL.

Quoted text here. Click to load it

   Perhaps the difference of opinion can be traced to how you learned
C and even what you were exposed to before learning it. I learned APL
(a compact language with lots of powerful operators) in college, and
every operator (except parentheses, which are used conventionally)
returns a value, including comparison operatiors and the assignment
operator. Furthermore, evaluation (except for operator precedence) is
right to left. I think knowing that helped me when I learned C and
read that '=' returns a value just as '==' and '!=' do (I even recall
being able to do c=(a=b) in Basic, and c=a.eq.b in ... sorry, language
drift).
   Seeing the first method is like seeing a multisyllabic word whereas
the second is like a long sentence describing what the word means.
   What was that Reader's Digest column, "It pays to increase your
word [/syntax] power."


Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Quoted text here. Click to load it

Maybe, but why have to bet at all?  We _know_ what the value of
(a==b) can be: 0 or 1.  We don't have to guess, or bet, or look
it up somewhere.

Quoted text here. Click to load it

I suppose if you had only used programming languages which
didn't have a comparison operator, you might be confused the
first time you saw one.  I can't think of any languages that
don't, though.

Quoted text here. Click to load it

If a comparison operator didn't return a value, what would be
the point of having one??

Quoted text here. Click to load it

[...]
 
--
Grant Edwards                   grante             Yow!  Finally, Zippy
                                  at               drives his 1958 RAMBLER
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 think a lot of people are stuck on the stereotypical Pascal style of
conditionals that can only appear in IF or WHILE statements.  Many
languages break out of that mold, such as Lisp for instance, where
_of_ _course_ operators return actual values.  C allows this to happen
as well, but it is more common to see it used with only a procedural
style and maybe this confuses people who have only seen that style.

--
Darin Johnson
    "Particle Man, Particle Man, doing the things a particle can"

Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Quoted text here. Click to load it

This is nonsense.  Pascal can have any form of conditional
expression you please.  You can even convert the result into
numerics (with ord()) yielding 0 or 1.

  mybool := ((a < b) AND (c > d)) OR (e = f);
  myint := ord(NOT mybool);

However IF, WHILE, UNTIL statements must be followed by a logical
expression rather than any arithmetic expression, which may be
where the confusion arises.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
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

OK, it's been over a decade since I used Pascal, so... :-)
Was this a part of a Pascal standard, or an extension?

--
Darin Johnson
    My shoes are too tight, and I have forgotten how to dance -- Babylon 5

Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Quoted text here. Click to load it

Completely standard.  See ISO7185 (standard) and ISO10206
(extended, but compatible with ISO7185).  Unfortunately the most
popular "Pascal" provider, Borland, did not adhere to the
specifications.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
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

Is that why Borland was wildly popular while "standard" Pascal was an
also-ran?

Kelly  (ducking)



Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
Quoted text here. Click to load it

Similarly you might ask why there are so many Windows systems out
there, or why non standards compliant C systems have not
survived.  Borland supplied something that was cheap, convenient,
and fast.  It would have been no harder to also make it compliant,
but they didn't, probably for marketing (a la MS) reasons.  At any
rate, this is completely OT here.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
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

Only if we _know_ our C compiler is compliant to the latest spec.
Many compilers, especially 8 bit embedded compilers, evaluate to 0 or
-1, not 0 or 1.

Jeff.

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

Quoted text here. Click to load it

Latest spec???  You must be joking!

Quoting from "The C Programming Language", (c) 1978:

Section 2.6 Relational and Logical operators:

       The unary negation operator ! converts a non-zero or true operand
       into 0, and a zero for false operand into 1.

Section 7.6 Relational operators

       The operators < (less than), > (greater than), <= (less than or equal
       to), and >= (greater than or equal to) all yeild 0 if the specified
       relations is false and 1 if it is true.

Section 7.7 Equality operators

       The == (equal to) and != (not equal to) operators are exactly
       analogous to the reltional operators except for their lower
       precedence. (Thus a<b == c<d is 1 whenever a<b and c<d have the same
       truth-value).
      
Quoted text here. Click to load it

They're broken or they're not C compilers. Take your pick.

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

What in the world are you talking about, man? The 1/0 thing goes back
to the very genesis of C- Kernighan & Ritchie..

K&R 2.6

"By definition, the numeric value of a relational or logical
expression is 1 if the relation is true, and 0 if
the relation is false."

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

Yes, and for a simple reason: negation.
It's easy to negate a boolean value with a 'NOT' instruction.

NOT 0 = (FF...)FFh = -1

Although, for optimisation reasons, a boolean "true" can be anything
that is not 0, not necessarily -1. Might prove useful in some cases.

Not only that, but even on the latest C compiler, any non-zero
integer will be treated as a "true" boolean value, which makes
the standard a little incongruent in my opinion.

For instance, 'if (a + b)' will be a 'true' condition if a != -b.
Of course, (a + b) can be any value, and not just 0 or 1.

Which is why I say: either you use a boolean type, in which case you
know what to expect, or you use an integer type. Mixing the two
can be a bit confusing.

'a == b' evaluates to a boolean type. But using a boolean value
as an integer is kinda risky. That's my opinion. That's why expressions
like 'c = (a == b)' are perfectly ok in my eyes, but stuff like
'c = (a == b) + d' is not.


Site Timeline