thepepp
thepepp

Reputation: 304

NSUInteger to int

I wrote a method tha uses myarray, defined in the same class. When I use count it always returns 0. When I use:

printf("%d", [myarray count]);

compiler says:

Format '%d' expetcs type 'int', but argument 2 has type 'NSUInteger'

why?

Upvotes: 2

Views: 2114

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

You should use %lu instead of %d. The compiler checks your format string against the parameters that you are passing to printf, sees that you are passing an unsigned but print it as a signed integer, and issues a warning. The warning indicates that for numbers greater than or equal to 2^31 printf would output a large negative number, when the data type implies a different semantic, namely, a large positive integer.

EDITED in response to comments by Josh Caswell and thepepp

Upvotes: 5

Related Questions