Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop condition always true

I have a For loop in C:

u8 i;
for (i=0; i <= 255; i++)
{
    //code
}

Now the compiler complains that "comparison is always true due to limited range of data type" I understand that 255 is u8 max but a for loop must have a condition. What should I put there then? Thanks.

like image 989
Maluvel Avatar asked Nov 29 '22 15:11

Maluvel


2 Answers

uint8_t i=0;
do {
    //code
}while(++i);
like image 77
BLUEPIXY Avatar answered Dec 05 '22 15:12

BLUEPIXY


what is u8? if it means 8-bit unsigned int, then 255+1 gives zero again, so the loop starts over again. you should use larger integer type. or use do-while as suggested in replies

like image 40
mangusta Avatar answered Dec 05 '22 16:12

mangusta