Home > OS >  How do I make an Int in C have a limit?
How do I make an Int in C have a limit?

Time:11-11

Is there any way I could ensure an int doesn't cross a certain limit? and if it needs to (if the program is adding numbers to the int and it crosses the limit) it goes back to 0 and does the job from there?

CodePudding user response:

It sounds like you want the modulo operator %.

If the upper limit is limit then

value = value % limit;  // Or value %= limit

will "reset" the value back to zero if it's about to pass the limit.

  • Related