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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:28:18+00:00 2026-05-23T04:28:18+00:00

What can I do to avoid MISRA giving this error for the code below?

  • 0

What can I do to avoid MISRA giving this error for the code below? I tried casting with (unit16_t). But then it didn’t allow an explicit conversion.

Illegal implicit conversion from underlying MISRA type “unsigned char” to “unsigned int” in complex expression (MISRA C 2004 rule 10.1)

 uint8_t rate = 3U; 
 uint8_t percentage = 130U;      
 uint16_t basic_units = rate * percentage;
  • 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-23T04:28:19+00:00Added an answer on May 23, 2026 at 4:28 am

    The problem is that both rate and percentage are silently promoted by the integer promotions to type “int”. The multiplication is therefore performed on a signed type.

    MISRA compatible code is to either rewrite the code as

     uint16_t basic_units = (uint16_t)rate * (uint16_t)percentage;
    

    or do as MISRA suggests, immediately typecast the result of an expression to its “underlying type”:

     uint16_t basic_units = (uint8_t)(rate * percentage);
    

    EDIT: Clarification follows.

    ISO 9899:1999 6.3.1.1 2

    If an int can represent all values of the original type, the value is converted to an int;
    otherwise, it is converted to an unsigned int. These are called the integer
    promotions
    .

    Informative text from MISRA-C:

    MISRA-C:2004 6.10.3 Dangerous type conversions:

    /–/

    – Change of signedness in arithmetic operations: Integral promotion will often result in two unsigned operands yielding a result of type (signed) int. For example, the addition of two 16-bit unsigned operands will yield a signed 32-bit result if int is 32 bits but an unsigned 16-bit result if int is 16 bits.

    I’m actually not sure whether the 2nd line of mine above would satisfy MISRA or not, at second thought I may have confused MISRA 10.1 with 10.5, where the latter enforces an immediate cast to underlying type, but only in case of certain bitwise operators.

    I tested both lines with LDRA static code analysis and it didn’t complain (but gives some incorrect, non-related warnings), but then LDRA also performs very poorly at MISRA-C.

    Anyway, the problem in the original question is that rate and percentage are both implicitly converted by the integer promotions to type int which is signed, since int can represent all values of a uint8_t. So it becomes

    uint16_t basic units = (int)rate * (int)percentage.
    

    To prevent this you have to typecast explicitly. After giving it more thought, I’d go with the 1st line of my two above.

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

Sidebar

Related Questions

http://en.wikipedia.org/wiki/Diamond_problem I know what it means, but what steps can I take to avoid
When embedding assembly code into a C/C++ program, you can avoid clobbering registers by
Is there a way I can avoid using this for multiple pattern checks? Can
How can you avoid circular dependencies when you're designing two classes with a producer/consumer
How can I avoid name mangling in C++?
For example, how can I avoid writing the 'func_name' twice? #ifndef TEST_FUN # define
I can understand wanting to avoid having to use a cursor due to the
Can you use a Spring-WS WebserviceTemplate for calling a webservice and avoid that it
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Why are pipes considered dangerous to use? What can be done to avoid these

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.