Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:54:40+00:00 2026-06-13T01:54:40+00:00

I have the following C code: unsigned int a; unsigned char b, c; void

  • 0

I have the following C code:

unsigned int a;
unsigned char b, c;
void test(void) {
    if (a < b)
        return;
    if (a < (b ? b : c))
        return;
}

When I compile it (with Microsoft cl, from MS SDK 7, -W3 warning level), the second comparison emits a warning: C4018, signed/unsigned mismatch. The first comparison emits no warning.

I’ve checked MS docs on the conditional operator and they says that if both operands are of same type, the result will be of the same type, so it should work as the first comparison. Am I missing something?

UPD: tested with gcc -Wall -Wextra -pedantic and got no warnings whatsoever.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:54:41+00:00Added an answer on June 13, 2026 at 1:54 am

    This is probably due to the arithmetic conversion rules: First, any integer type of conversion rank less than int (eg unsigned char) will promote to int or unsigned int.

    Whether the result will be int or unsigned int does not (directly) depend on the signedness of the original type, but its range: int is used even for unsigned types as long as all values can be represented, which is the case for unsigned char on mainstream architectures.

    Second, as both operands end up with the same conversion rank, but one is unsigned, the other operand will be converted to an unsigned type as well.

    Semantically, your expressions read

    a < (unsigned int)(int)b
    

    and

    a < (unsigned int)(b ? (int)b : (int)c)
    

    The compiler is apparently smart enough to notice that the first case cannot cause problems, but fails for the second one.

    Steve Jessop’s comment nicely explains how this could happen:

    I would imagine that in the first case the compiler thinks, “I have a comparison operator whose operand types are unsigned int and unsigned char. No need for a warning, now let’s apply promotion followed by usual conversion”.

    In the second case it thinks, “I have a comparison operator whose operand types are unsigned int and int (which I derived as the type of the conditional expression on the RHS). Best warn about that!”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I have the following code: volatile unsigned int * address; char * array=0x10000008; Please
I have the following C code: static void* heap; static unsigned int ptr; int
I have the exercise with following code int FindFirstSet(unsigned BitMap, unsigned start) { unsigned
I have the following code: tmp_data = simulated_data[index_data]; unsigned char *dem_content_buff; dem_content_buff = new
I have the following code: main.c #include checksum.h void main() { char *Buf =GPGGA204502.005106.9813N11402.2921W1090.91065.02M-16.27M;
I have the following code: struct VRfile{ char sessionID[10]; char file[20]; int first; };
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for loading image from url in xml parsing endElement method
In the following code: #include stdio.h signed char a= 0x80; unsigned char b= 0x01;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.