[NSRCA-discussion] Chapter-12 computer science.
Martin X. Moleski, SJ
moleski at canisius.edu
Tue Jul 3 21:31:14 AKDT 2007
Duane Beck wrote:
> Depends on whether char basic type is 8-bit or larger. Most decent
> compilers should issue a warning for comparing with an integer constant
> out of range. Assuming default char type is 8-bit signed, when cnt is
> 127, cnt increments to -128 (by wrapping around adjacent two's
> complement values) and the loop continues unterminated. If default char
> type is 8-bit unsigned, it's still unterminated because after 255, cnt
> will increment to 0. So, in both conditions, cnt is always less than
> 256.
I'm with you up to here--I missed the significance
of knowing what type of var cnt is.
> Otherwise, if char is larger (16-bit), foo returns 256.
Seems to me that if the loop terminates,
num has to be 255. cnt and num are both
initialized to 0 and incremented under
the same conditions (cnt < 256). I don't
seem how num can get beyond 255 when
starting with cnt from 0.
Marty
>
> Duane
>
> Martin X. Moleski, SJ wrote:
>> John Pavlick wrote:
>>> OK, what does this do?
>>> char foo(void)
>>> {
>>> char cnt, num;
>>>
>>> for(cnt = 0, num = 0; cnt < 256; cnt++)
>>> {
>>> num++;
>>> }
>>> return num;
>>> }
>>
>> Initializes cnt and num to 0.
>>
>> Increments cnt and num from 0 to 255
>> by passing through the for loop.
>>
>> Returns 255.
>>
>>
>> Marty
>> _______________________________________________
>> NSRCA-discussion mailing list
>> NSRCA-discussion at lists.nsrca.org
>> http://lists.nsrca.org/mailman/listinfo/nsrca-discussion
>>
>
> _______________________________________________
> NSRCA-discussion mailing list
> NSRCA-discussion at lists.nsrca.org
> http://lists.nsrca.org/mailman/listinfo/nsrca-discussion
More information about the NSRCA-discussion
mailing list