[wplug] about basic programming

Lee Brinton lee at leebrinton.net
Fri Mar 4 18:35:45 EST 2005


>>> Sometime in March Juan Zuluaga assaulted the keyboard and produced:
>>>
>>
>> [SNIP]: Discussion on books.
>>
>> Perhaps someone with years of experiance in programming could
>> tell me something.  In the following program snip-it:
>>
>> #include <stdio.h>
>>
>> #define lastnum 40
>>
>> int main()
>> {
>>
>>   int num = 0;
>>
>>   while ((num == 0) || (num < lastnum))
>>     {
>>       printf("Counting %d up\n", num);
>>       num++;
>>     }
>>   return 0;
>> }
>>
>> num is only incremented to 39, one below 40.
Tom and Bill have discussed this already.

>>   Thus far I have
>> always considered it was because we stop at the number before
>> the one we define.  So I have always wrote as such.

Notice that the program does print 40 lines.  The pattern your program 
follows is extremely common in C. Normally the meat  is written as:
for(num = 0; n < lastnum; num++)
	printf("Counting %d up\n", num);

This will give you lastnum iterations from 0 to (lastnum - 1).  This is 
common because array subscripts or address offsets start at 0 in C.

>>   Yet, I
>> have not found an explination in either of my two C books.
>> Am I missing or not properly enterpreting a paragraph/sentence
>> in my books?
>
> Did that help?

Lee



More information about the wplug mailing list