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

Discussion in 'C Programming' started by Harvey Twyman, Oct 23, 2003.

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

    ===========================================
     
    Harvey Twyman, Oct 23, 2003
    #1
    1. Advertising

  2. In article <>, 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
     
    Andreas Kahari, Oct 23, 2003
    #2
    1. Advertising

  3. Andreas Kahari wrote:
    > In article <>, 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.
     
    Jeremy Yallop, Oct 23, 2003
    #3
  4. Harvey Twyman

    dis Guest

    "Harvey Twyman" <> wrote in message
    news:...

    > 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.
     
    dis, Oct 23, 2003
    #4
  5. 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://galileo.phys.virginia.edu/~jvn/

    "Science knows only one commandment: contribute to science."
    -- Bertolt Brecht, "Galileo".
     
    Julian V. Noble, Oct 23, 2003
    #5
  6. Harvey Twyman

    Micah Cowan Guest

    Andreas Kahari <> writes:

    > In article <>, 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
     
    Micah Cowan, Oct 23, 2003
    #6
  7. (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) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Oct 24, 2003
    #7
  8. Harvey Twyman

    Simon Biber Guest

    "Jeremy Yallop" <> 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.
     
    Simon Biber, Oct 24, 2003
    #8
  9. (Harvey Twyman) wrote in
    news: 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.
     
    August Derleth, Oct 25, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mdb
    Replies:
    3
    Views:
    4,170
    =?Utf-8?B?TmVpbA==?=
    Sep 20, 2004
  2. =?Utf-8?B?TmVpbA==?=
    Replies:
    1
    Views:
    668
    Mark Newby
    Apr 27, 2005
  3. Michael Strorm
    Replies:
    26
    Views:
    774
    J. Campbell
    Nov 10, 2003
  4. javuchi
    Replies:
    9
    Views:
    303
    Bengt Richter
    Nov 23, 2005
  5. Jonathan Wood
    Replies:
    10
    Views:
    543
    Jonathan Wood
    Jan 3, 2009
Loading...

Share This Page