[NSRCA-discussion] Chapter-12 computer science.
Duane Beck
duane.e.beck at comcast.net
Tue Jul 3 21:16:52 AKDT 2007
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. Otherwise, if char is larger (16-bit), foo returns 256.
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
>
More information about the NSRCA-discussion
mailing list