Signed/unsigned mismatch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tcsvikr
    New Member
    • Feb 2007
    • 18

    #1

    Signed/unsigned mismatch

    Code:
    unsigned int  _I = _N;           // _N is of type unsigned int 
    			
    for (_Grp = false; *_Pg != CHAR_MAX && '\0' < *_Pg
                                             && *_Pg < _I - _Np; _Grp = true)
    //*_Pg is constant char

    In the above code we are comparing constant char with unsigned int(*_Pg < _I - _Np). We are getting a warning of 'warning C4018: '<' : signed/unsigned mismatch' when compiled with highest warning level in VC++. Can anyone help us to get rid of this warning with necessary explaination (Without using #pragma directives) ???
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by tcsvikr
    unsigned int _I = _N; // _N is of type unsigned int

    for (_Grp = false; *_Pg != CHAR_MAX && '\0' < *_Pg
    && *_Pg < _I - _Np; _Grp = true)
    //*_Pg is constant char


    In the above code we are comparing constant char with unsigned int(*_Pg < _I - _Np). We are getting a warning of 'warning C4018: '<' : signed/unsigned mismatch' when compiled with highest warning level in VC++. Can anyone help us to get rid of this warning with necessary explaination (Without using #pragma directives) ???
    Is there an earth shattering reason for using the underscore character at the start of your variable names? It reads like a nightlmare and absolutely DOES NOT make you a super cool programmer
    An unsigned char is 0 - 255. A signed char is -128 to 127 so clearly they store incompatible values. The compiler recognises this and warns you about it. You can recast one to the other which will stop the warning. It will not stop the errors in transferring incompatible values though.

    Comment

    • tcsvikr
      New Member
      • Feb 2007
      • 18

      #3
      Originally posted by willakawill
      Is there an earth shattering reason for using the underscore character at the start of your variable names? It reads like a nightlmare and absolutely DOES NOT make you a super cool programmer
      An unsigned char is 0 - 255. A signed char is -128 to 127 so clearly they store incompatible values. The compiler recognises this and warns you about it. You can recast one to the other which will stop the warning. It will not stop the errors in transferring incompatible values though.




      I'm sorry.i have not written the code..My task is just to debug the code given to me and remove all warnings at highest level. I got your reasoning. But I don't know the default conversion of char. Will it be converted to signed int or unsigned int.

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by tcsvikr
        I'm sorry.i have not written the code..My task is just to debug the code given to me and remove all warnings at highest level. I got your reasoning. But I don't know the default conversion of char. Will it be converted to signed int or unsigned int.
        This will work:
        Code:
        *_Pg < unsigned char(_I - _Np)
        and this will work:
        Code:
        *_Pg < char(_I - _Np)
        insofar as to remove the warning. It is up to you, in the rest of the code to ensure that this value does not exceed 255 or 127 in the second case

        Comment

        Working...