Unsigned and signed comparison

后端 未结 5 1880
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 21:40

Here is very simple code,

#include 
using namespace std;
int main() {
    unsigned int u=10;
    int i;
    int count=0;
    for (i=-1;i<=         


        
5条回答
  •  温柔的废话
    2020-11-27 22:33

    Its because -1 is casted as an unsigned int, so the for loop code is never executed.

    Try compiling with -Wall -Wextra so you can get the respective warnings (if not getting them so far, and compiling with g++)

    http://en.wikipedia.org/wiki/Two's_complement

提交回复
热议问题