piclist 2001\04\12\145239a >
Thread: Coding style
www.piclist.com/techref/index.htm?key=coding+style
flavicon
face BY : Mike Mansheim email (remove spam text)



----- Forwarded by Michael J Mansheim/Graco on 04/12/01 01:45 PM -----

>> Yes, this your assumptions are correct, and this behaviour is required
by
>> the ANSI standard.

> Microntroller C's tend to drift away from "full ansi compliance."  Does
> anyone know for sure if any of the popular PIC C's WON'T handle the
> short-cutting of the boolean expression correctly?

There seemed to be a bit of interest in this question, so I thought I would
test it.
With CCS, it works as Bob says it should.

Here's the code if anyone wants to be sure I tested correctly:

To check different values of F1 & F2, I just changed them and re-compiled.
I
used a breakpoint to see if function2() was entered.
With F1 = 0 and F2 = 1, result = 0 and function2() was not entered -
exactly
the desired behavior!
In case anyone is wondering about testing globals in the test functions
rather
than passing arguments (which would be cleaner), I did it this way so the
test
would look more like the original question (i.e. functions called that do
not
take arguments).
By the way, from an earlier discussion, ints are 8 bits in the CCS
compiler.

unsigned int F1, F2;

unsigned int test(void);
unsigned int function1(void);
unsigned int function2(void);

void main(void)
{
    unsigned int result;

    F1 = 0;
    F2 = 1;

    result = test();
}

unsigned int test(void)
{
    return function1() && function2();
}

unsigned int function1(void)
{
    unsigned int return_value;

    if (F1 == 1) return_value = 1;
    else       return_value = 0;

    return return_value;
}

unsigned int function2(void)
{
    unsigned int return_value;

    if (F2 == 1) return_value = 1;
    else       return_value = 0;

    return return_value;
}
Just for yuks, I tried the original method, where test() would look like:

unsigned int test(void)
{
    if (!function1()) return 0;
    if (!function2()) return 0;
    return 1;
}

because this looked fine to me, and is probably how I would have done
it.  Not being a C guru, I didn't know you could do what Bob posted.
Not surprisingly, this compiles larger.  So the 'cleaner' look also
compiles more efficiently - not always the case with C.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


<OFA8D60092.7AEE7384-ON86256A2C.0066DCAD@graco.com>

See also: www.piclist.com/techref/index.htm?key=coding+style
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Coding style

month overview.

new search...