CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

evaluation order of parameters

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

evaluation order of parameters
PostPosted: Sun Jul 30, 2006 11:51 am     Reply with quote

Can I rely on the evaluation order of a function's parameters ? will the parameters of any function (make16 in this case) evaluate always left to right ? Example:
Code:
...
Var16 = make16(read_eeprom(Ptr++), read_eeprom(Ptr++));
...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 30, 2006 4:28 pm     Reply with quote

When I saw your code, my first thought was "I would never do this".

But to provide documentation, here is one answer:
http://www.scit.wlv.ac.uk/cbook/chap3.order.html

Here is a large discussion on order of evaluation. It's not about
function parameters. It's about arithmetic evaluation, but it's still
useful. Scroll down about half way, to the example with two getc() statements in one line.
http://c-faq.com/~scs/readings/precvsooe.960725.html
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Mon Jul 31, 2006 3:41 am     Reply with quote

I was just tempted to spare a temporary variable, not realizing, that the compiler allocates one anyway (?)
Although it worked as expected, I felt it is a bad practice, and you affirmed that. Thanks.

There's another (not evaluation-order-prone) dilemma: is it a good practice to squeeze as many intructions, operators as possible into one line (making a harder to read code), like
Code:
varB = array[ptr++] & 0x0F;

vs.

varB = array[ptr];
varB &= 0x0F;
ptr++;

(I was just not trying to make a more complicated example, imagine one :-)
So my question is, that writing 'tight' (but less readable) code not specifying any particular order the compiler must evaluate the operators does help it optimizing the code ?
I suppose that preloading varB is unnecessary in the example, the compiler can do the '&=' instruction in the W register if it was not written in a separate line.

edited: when I looked at it, I saw my first example more readable :-), but anyway my question is still the same.


Last edited by libor on Mon Jul 31, 2006 9:42 am; edited 1 time in total
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Jul 31, 2006 9:08 am     Reply with quote

Quote:
varB = array[ptr++] & 0x0F;


I do this kind of stuff all the time. It's when your evaluation needs to start continuing onto the next line that it might be getting a bit long.

Ronald
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 31, 2006 10:33 am     Reply with quote

Quote:
varB = array[ptr++] & 0x0F;

This is good coding style. There's nothing wrong with it.
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Mon Jul 31, 2006 12:27 pm     Reply with quote

Thank you all. Regarding the first problem, I've come to this:
Code:
long Var16;
int VarLo, VarHi;
#locate VarLo = &Var16
#locate VarHi = &Var16 + 1

VarHi = read_eeprom[Ptr++];
VarLo = read_eeprom[Ptr++];

Is this the cleanest solution ? Or should I use a union instead with the two 'int' variables overlapping the 'long' one ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 31, 2006 2:06 pm     Reply with quote

In this case, a union of a 'long' and a struct (of two bytes) would be
considered better style. The reasons would be:

1. Better portability, in case you move to another compiler.

2. If someone else has to maintain your code in the future, it's easier
for them to understand if you use coding methods that are part of
standard C.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group