Search code examples
cintunsigned

unsigned int arithmetic - beginner C


At the risk of getting yelled at for posting a stupid question:

say I have the following code snippet:

unsigned int limit = 4294967296;
int math = 50000;

while(limit > 50000)
{
    limit = limit - math;
    printf("new limit is - \t%u\n", limit);
}

Would doing arithmetic between an unsigned int type and an int type have rammifications o n memory usage?

I know that an unsigned int has an order of magnitude more memory (2^31 for an int vs 2^32 for an unsigned int), but, since I'm using my unsigned int as a placeholder, i think I'm negating the danger of an overflow.

I would run this myself, but I don't have an accessible linux box right now.

Any input on this would be greatly appreciated.


Solution

  • This will loop forever since you will end up with wrap-around (i.e., limit will never be less than 0) ... is that your question?