I am currently writing a program in C. I have a variable i
declared in my program. I have initialized it as such:
unsigned char i = 0x00;
Next, I have a while loop, and inside that while loop, I'm increasing the unsigned char
by 16 and displaying it to seven digits (such as 0000080
). Well, This runs perfectly fine for the first 16 values, but whenever the unsigned char
gets to 00000f0
and gets incremented again, it goes back to 0000000
. Why is this and how can I alter my code to fix this? Thanks in advance.
An unsigned char
, assuming 8 bit to a byte, has a maximum value of 255 (0xff). Any arithmetic operation that exceeds that is truncated modulo 256.
If you want to support larger values, use unsigned int
instead.