what does the signed/unsigned comparison warning mean?

前端 未结 5 471
南旧
南旧 2021-01-29 06:42
auto.cpp: In function ‘int autooo(unsigned int)’:
auto.cpp:33:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

5条回答
  •  日久生厌
    2021-01-29 07:07

    Type cast the code before you were comparing the signed and unsigned code to avoid warning

    int a;
    unsigned int b;
    
    if(a==b) gives warning
    
    if(a == (int)b)
    

    will resolve your issue

    EDIT

    Blind casting will lead to some unexpected results

    The warning is because ranges for signed and unsigned are different.

    Casting will work fine when signed integer you were used for comparison was greater than zero.

    so have check whether the signed integer is greater that zero before comparing

    More info here

提交回复
热议问题