+91 9945200008     support@uttaracomputers.com
LOGIN     REGISTER    

Placement of a statement

0
0

Shastry sir mentions that every C statement must be in a specific place. That is, each statement should appear in a place within a module which is the most appropriate place for that statement (‘Audio 34. PRE PROCESSOR II – 4’ (at the end of page 51)). I was doing one of the exercises (squeeze.c) from ‘C Preprocessor – II- Labware’ and wondered if a particular statement should occur before/after another statement.

Following is the section where I look for a space character and copy to the buffer string:

if( c == ‘ ‘ ) {
if( is_space_printed == FALSE ) {
*(buf + curnum) = c;
curnum++;
is_space_printed = TRUE;
}  else
continue;

In the above, I wonder what must be the most appropriate place for ‘is_space_printed = TRUE’ statement.  Should it be placed soon after the condition is checked (after found to be FALSE in the inner ‘if’ statement, before copying ‘c’ to the buffer) or after copying ‘c’ to the buffer or after incrementing the current position (curnum).

It _appears_ it should not matter in this case but it still seems there should be an appropriate place for that statement. Any comments?

  • kes333@hotmail.com
    It didn’t seem to save the formatting (tabs) I added in the piece of code. Sorry.
  • You must to post comments
0
0

is_space_printed = TRUE;  should be immediately after the inner if statement.  In the current context, it actually makes no difference where is is placed.  Statement placement is more like we do ordering of our day-to-day activities.  Occasionally, we would like to do something immediately due to the possibility that we might forget to do it after doing a couple of other activities.  More importantly, statement placement is important from maintenance point of view.  Software maintenance is likely to be done by another (, possibly junior, ) programmer.  Maintenance might involve insertion of several new statements some of which may even be other function invokations.  Placing an urgent statement last in a sequence of statements might make the CPU do some other activities even before that statement is done and it might be wrong logically.

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.