Brad Bourn wrote:
I have a function say like this
func(unsigned int foo) { unsigned int i, retval = 0; for (i = 0; i < foo; i++) { retval++; }
return retval; }
and I call it with
val = func(4 - 3);
Seems like gcc WILL pass the -1 to func, and func just get stuck in a loop becase i starts at 0 (which is alread greater than a negative number)
This not what I would have expected.
Is this by design?
When you pass the -1 to the function, it will be implicit cast to an unsigned int (2^32 in this case). This is a consequence of the design of your function. When you say func(unsinged int foo) you are saying that this function only receives unsigned integers. When you pass a signed integer to it, it will break, since you are violating the signature. You must assure that foo is unsigned or change your function: func (int foo) { int i; unsigned int retval = 0; for (i = 0; i < foo; ++i) { retval++; } return retval; } Notice that you also must say that the function returns an unsigned int, or it will be implicit returning an int and similar errors might occur. []s Davi de Castro Reis