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

H

Harvey Twyman

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

===========================================
 
A

Andreas Kahari

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

Code Extract:

Declares three char varaiables.

Assigns the result of the comparison between a and b to c (zero
if they are not eqal, non-zero otherwise). The type of c should
probably be "int", not "char".
It also uses this syntax in "Function Calls" thus:

function ( a == b );

Call function with the value zero if a equals b, otherwise call
it with a non-zero value.
function ( char x ) { ... }

Should probably be "int x".


In what way does you compiler not like this?
 
J

Jeremy Yallop

Andreas said:
Assigns the result of the comparison between a and b to c (zero
if they are not eqal, non-zero otherwise).

Specifically, 1 otherwise.
Call function with the value zero if a equals b, otherwise call
it with a non-zero value.

Specifically, call it with the value 1.

Jeremy.
 
D

dis

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

Code Extract:

Assigns to c the value of the expression a==b, which is either 0 or 1,
assuming the comparison is well defined.
It also uses this syntax in "Function Calls" thus:

function ( a == b );

Calls function with the value of the expression a==b for the first argument.
a==b is either 0 or 1, assuming the comparison is well defined.
===========================================

Question:

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

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

See above.
 
J

Julian V. Noble

Harvey said:
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

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

It ought to work. There seems to be no prohibition against
assigning the result of a logical operation to a 'char' type.
But maybe your Hi-Tech 'C' Compiler would prefer assignment
to an integer?

Either is permissible, as the example shows.

int main(void)
{
char a,b,c,d;
int k;

a = 'w';
b = 'z';
d = 'w';

c = ( a == b );
k = ( a == b );
printf( " %c%c%c%d%d\n", a,b,d,c,k ); // outputs wzw00

c = ( a == d );
k = ( a == d );
printf( " %c%c%c%d%d\n", a,b,d,c,k ); // outputs wzw11
return 0;
}

--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
M

Micah Cowan

Andreas Kahari said:
Declares three char varaiables.

Assigns the result of the comparison between a and b to c (zero
if they are not eqal, non-zero otherwise). The type of c should
probably be "int", not "char".

Specifically, one "otherwise". As to whether it should be a char
or an int: char may save a little space, and the values 0 and 1
are definitely representable in a char. No problems.
 
K

Keith Thompson

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?

It means either that the code fragments you posted don't correspond to
what you actually fed to the compiler, or that your compiler is broken
(more precisely, that your compiler doesn't conform to the C
standard).

If you'll post a small complete self-contained program (I suggest no
more than 20 lines) that exhibits the problem, along with the error
message you get when you try to compile it, we can help you determine
which it is.
 
S

Simon Biber

Jeremy Yallop said:
Specifically, call it with the value 1.

No, wrong way around. Call it with the value 1 if a equals b,
otherwise call it with the value 0.
 
A

August Derleth

(e-mail address removed) (Harvey Twyman) wrote in
44:54a:
I have code written under the CCS 'C' Compiler to run on a PIC
microcontroller.

Code Extract:

Please, don't do this. At least post something that will compile, run, and
demonstrates the problem. The lines below, while mostly syntactically
correct, will not compile to anything remotely testable.

This much is obvious, and is perfectly correct C.

This is, too, obvious to someone who knows C, but perhaps less than
obvious to someone who is not well-versed in how C handles the values of
comparison ops. If a and b compare equal, c will get the value 1. If they
compare unequal, c will get the value 0.

Note that those values are ints, not chars. Your compiler could reasonably
diagnose that as a problem. For safety, declare c as an int.
-------------------------------

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

function ( a == b );

A perfectly valid subroutine call. function() gets the value 1 if a == b
is true, and 0 if it is not.

See above about trying to use the char type to hold the result of a
comparison op. Also, declare a return type for the subroutine. Use the
void keyword if it does not return a value, like so:

void func(int x)
{
printf("%d\n", x);
}

/* I think printf() does return a value, but ignoring it like that will
not create incorrect code. */
-------------------------------

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

Question:

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

If your 'C' compiler does not understand the foregoing, it is not
Standards-conformant and probably is not the correct tool for the job.

Unless, of course, the code you /didn't/ post is wrong. See, I really
can't tell from over here. Post a compilable version of your code and
we'll all consider it and tell you if it's non-Standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,151
Messages
2,570,855
Members
47,395
Latest member
GennieGinn

Latest Threads

Top