Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question About Strange 'C' Code Syntax ( Well strange to me anyway )

Reply
Thread Tools

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

 
 
Harvey Twyman
Guest
Posts: n/a
 
      10-23-2003
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

===========================================
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      10-23-2003
In article <(E-Mail Removed) >, 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
 
Reply With Quote
 
 
 
 
Jeremy Yallop
Guest
Posts: n/a
 
      10-23-2003
Andreas Kahari wrote:
> In article <(E-Mail Removed) >, 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.
 
Reply With Quote
 
dis
Guest
Posts: n/a
 
      10-23-2003
"Harvey Twyman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
Julian V. Noble
Guest
Posts: n/a
 
      10-23-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      10-23-2003
Andreas Kahari <(E-Mail Removed)> writes:

> In article <(E-Mail Removed) >, 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
(E-Mail Removed)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-24-2003
(E-Mail Removed) (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) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      10-24-2003
"Jeremy Yallop" <(E-Mail Removed)> 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.


 
Reply With Quote
 
August Derleth
Guest
Posts: n/a
 
      10-25-2003
(E-Mail Removed) (Harvey Twyman) wrote in
news:(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Packages : A loomingproblem? packages might no longer work? (well not on your platform or python version anyway) David Lyon Python 19 04-23-2009 11:10 PM
Anyway to Run Code Not Associated with Loading a Page? Jonathan Wood ASP .Net 10 01-03-2009 11:24 PM
Anyway to clarify this code? (dictionaries) javuchi Python 9 11-23-2005 10:59 AM
Hi Whats considered to be a well priced and well reviewed plasma tv 42 inch and above ? Hmmmmmmm DVD Video 8 09-28-2004 09:32 AM
Nasty code...but please critique it anyway :-) Michael Strorm C++ 26 11-10-2003 05:39 PM



Advertisments