By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

===========================================
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <6d**************************@posting.google.com >, Harvey Twyman wrote:
I have code written under the CCS 'C' Compiler to run on a PIC microcontroller.

Code Extract:

-------------------------------
char a,b,c;
Declares three char varaiables.
-------------------------------
c = ( a == b );
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?
--
Andreas Kähäri
Nov 13 '05 #2

P: n/a
Andreas Kahari wrote:
In article <6d**************************@posting.google.com >, Harvey Twyman wrote:
c = ( a == b );


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


Specifically, 1 otherwise.
function ( a == b );


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.
Nov 13 '05 #3

P: n/a
dis
"Harvey Twyman" <ha***********@yahoo.com> wrote in message
news:6d**************************@posting.google.c om...
I have code written under the CCS 'C' Compiler to run on a PIC microcontroller.
Code Extract:

-------------------------------
char a,b,c;
-------------------------------
c = ( a == b );
-------------------------------
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 );
-------------------------------
function ( char x ) { ... }
-------------------------------
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.
Nov 13 '05 #4

P: n/a
Harvey Twyman wrote:

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
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #5

P: n/a
Andreas Kahari <ak*******@freeshell.org> writes:
In article <6d**************************@posting.google.com >, Harvey Twyman wrote:
I have code written under the CCS 'C' Compiler to run on a PIC microcontroller.

Code Extract:

-------------------------------
char a,b,c;


Declares three char varaiables.
-------------------------------
c = ( a == b );


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.

--
Micah J. Cowan
mi***@cowan.name
Nov 13 '05 #6

P: n/a
ha***********@yahoo.com (Harvey Twyman) writes:
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.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #7

P: n/a
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote:
function ( a == b );


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.


No, wrong way around. Call it with the value 1 if a equals b,
otherwise call it with the value 0.
Nov 13 '05 #8

P: n/a
ha***********@yahoo.com (Harvey Twyman) wrote in
news:6d**************************@posting.google.c om on Thu 23 Oct 2003
09: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.

-------------------------------
char a,b,c;
This much is obvious, and is perfectly correct C.
-------------------------------
c = ( a == b );
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.
-------------------------------
function ( char x ) { ... }
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.

Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.