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 5952079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:37:49+00:00 2026-05-22T17:37:49+00:00

I have the following C code: a = (b == B1) ? A1 :

  • 0

I have the following C code:

a = (b == B1) ? A1 : A2;

a, b, A1, A2 and B1 are all of type unsigned char. A1, A2 and B1 are all constants.

When compiling under VC++ I see the following warning:

warning C4244: '=' : conversion from 'int ' to 'unsigned char ', possible loss of data

I don’t understand this warning – none of the variables are of type int. Presumably some kind of implicit conversion is happening, but why?

Strangely, the following code (which is functionally identical) compiles with no warnings:

if (b == B1) {
  a = A1;
} else {
  a = A2;
}

So far as I’m aware, the two code extracts should be identical.

  • 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-05-22T17:37:49+00:00Added an answer on May 22, 2026 at 5:37 pm

    In C language arithmetic arguments of ternary ?: operator are subjected to usual arithmetic conversions, i.e. they are promoted to int before any further computations are performed. It doesn’t really matter whether they are constants or not. What matters is that they have type unsigned char (as you said) and unsigned char under ?: is always promoted first. Basically, C language never performs any computations in types smaller than int. Everything smaller is converted to int first.

    This is what happens in your case as well. Basically, your

    a = (b == B1) ? A1 : A2;
    

    is interpreted by C language as

    a = ((int) b == (int) B1) ? (int) A1 : (int) A2;
    

    and this is why you get the warning. Again, the fact that A1 and A2 are constants plays no role whatsoever.

    The

    a = A1;
    

    does not subject the right-hand side to integral promotion, which is why there’s no warning here. Moreover, in this trivial case (direct assignment) even if A1 was explicitly declared as an int constant, most compilers would not issue a warning if they could see that the constant is in range of the target type unsigned char. The case with ?: is more complicated, so the compilers might revert to the generic behavior and issue a warning.

    • 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 to zip all the files and then save it
I have the following code: // Obtain the string names of all the elements
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
To select all Scheduling s which are active, I have the following code: var
I have the following code: ViewData.Model = from m in dataModel.Items where m.Loaned ==
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I'm trying to use opengl in C#. I have following code which fails with
I have the following code in a web.config file of the default IIS site.

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.