for循环条件始终为true

Mal*_*vel 4 c types for-loop range

我在C中有一个For循环:

u8 i;
for (i=0; i <= 255; i++)
{
    //code
}
Run Code Online (Sandbox Code Playgroud)

现在编译器抱怨"由于数据类型的范围有限,比较总是正确的"我知道255是u8 max但是for循环必须有条件.那我应该放在那里?谢谢.

BLU*_*IXY 7

uint8_t i=0;
do {
    //code
}while(++i);
Run Code Online (Sandbox Code Playgroud)


man*_*sta 5

是什么u8?如果它意味着8位无符号整数,则255 + 1再次给出零,因此循环重新开始.你应该使用更大的整数类型.或do-while按照回复中的建议使用