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

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

Translate This Thread From English to

Threaded View
I have code written under the CCS 'C' Compiler to run on a PIC microcontroller.

Code Extract:

-------------------------------
char    a,b,c;
-------------------------------
c  = ( a == b );
-------------------------------

It also uses this syntax in "Function Calls" thus:

function ( a == b );
-------------------------------
function ( char x ) { ... }
-------------------------------

===========================================

Question:

What does this mean as my Hi-Tech 'C' Compiler doesn't understand it either?

===========================================

Harvey Twyman
About Me: http://www.Twyman.org.uk/CV

===========================================

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

The equality operator, "==", compares two values and returns a int value
of 1 if they are the same and 0 if different.  This is standard C that
should be in any implementation.  I suggest getting a C reference book
for these and similar questions.

When used with a function that takes type char, the result of (a==b), of
type int, is converted to the type defined by the prototype.  If you
don't have a prototype in scope when the function is invoked, then later
define it, as implied by your use and declaration of "function" above,
you may get an error for redeclaration of the function with different
parameter type.  To fix the problem, declare the prototype before use:

<return type> function(char);
...
  function (a == b);
...
<return type> function (char x) { ... }

where you explicitly specify the return type -- void, char, int,
whatever.

Thad

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

The statment is equivalent to:

if (a ==b)
   c = TRUE;  // TRUE is non-zero, usually 1 or -1
else
   c = FALSE;  // FALSE should be zero

I'm guessing your C compiler probably is giving you some sort of warning about
type compatability?



Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On 23 Oct 2003 16:03:25 GMT, snipped-for-privacy@aol.com (Gary Kato) wrote in
comp.arch.embedded:

Quoted text here. Click to load it

A true conditional always produces a value of 1 in C when that value
is examined numerically.  -1 is not an option.

--
Jack Klein
Home: http://JK-Technology.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




I always use:

#define FALSE  0

#define TRUE   (!FALSE)

Tanya





Re: Question About Strange 'C' Code Syntax ( Well strange to me anyway )
On Fri, 24 Oct 2003 13:54:46 +0930, the renowned "tanya"

Quoted text here. Click to load it

Perhaps so, but the expression (a == b) is an int with value 0 or 1
for conforming compilers. See 6.5.9 in ISO/IEC 9899:1999 (E).

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

There are no 9899:1999 conforming compilers (OK, so 2 claim it) the
majority are 9899:1990 + A1 IE 9899:1996

However as the discussion is about PIC compilers you are not going to
get a conforming compiler anyway.

 
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

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

Quoted text here. Click to load it

Note, however, that Harvey did specify "'C' Code Syntax" in the
subject of his post. If == produces a value other than a zero or
one, then the compiler is /not/ a C compiler - and this fact is
not altered by the hardware on which the program executes.

All bets are off if you're compiling C code with other than a C
compiler  :-P

One of my favorite single-statement C functions takes advantage
of this (rather limited) range of values produced by C's
relational operators:

    int signof(int x)
    {  return (x > 0) - (x < 0);
    }

To produce 1 if (x > 0), 0 if (x == 0), and -1 if (x < 0). I
don't need/use it very often; but haven't found a C compiler yet
that doesn't compile the expression to produce correct results.
--
Morris Dovey
West Des Moines, Iowa USA
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

Silly. Since 0 is *always* evaluated as false and !0 is *always* evaluated
as true why introduce manifest constants that risk undef/ifdef to another
possible value?

--
- Mark ->
--

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

Not true. At least with the compilers that I've used. Expressions are by
default non-zero, not "1" or "-1" or specifically any other non zero number.

Quoted text here. Click to load it

Better to do:

typedef enum
{
    FALSE = 0,
    TRUE,
} BOOL;

At least the compiler can type check when you use a real boolean.

-->Neil



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

But then you are assuming that TRUE == 1, which I was trying to avoid.

Not that that is an unreasonable assumption (in most cases), but the #define
version should work on any supposed C compiler, even if it believes that
TRUE is -1.

Kind regards,

Tanya





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

Quoted text here. Click to load it

The C standard makes it clear that !0 is always 1. Why are you trying
to avoid relying on the C standard in this matter?

Quoted text here. Click to load it

Every C compiler believes, no *knows*, that !0 is 1 in all cases. No
exceptions.

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

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

And would you be so kind as to quote the corresponding ISO paragraph?


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

Quoted text here. Click to load it

For relational operators:

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

For equality operators:

6.5.9.3 The == (equal to) and != (not equal to) operators are
analogous to the relational operators except for their lower
precedence. Each of the operators yields 1 if the specified
relation is true and 0 if it is false. The result has type int.
For any pair of operands, exactly one of these relations is true.

HTH
--
Morris Dovey
West Des Moines, Iowa USA
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 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.

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

Quoted text here. Click to load it

It's the same in ISO/IEC 9899:1999(E). Thanks!
--
Morris Dovey
West Des Moines, Iowa USA
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 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    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

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

Quoted text here. Click to load it


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.

Quoted text here. Click to load it

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)

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

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

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    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

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

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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

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

Site Timeline