Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2568. MISRA. Both operands of an opera…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V2568. MISRA. Both operands of an operator should be of the same type category.

Dec 19 2019

This diagnostic rule is based on the software development guidelines developed by MISRA (Motor Industry Software Reliability Association).

This diagnostic rule applies only to code written in C. If the entity types of operands do not match when converting arithmetic types, this can lead to non-obvious issues.

The MISRA standard defines an essential type model, where variables can have the following types:

  • Boolean for Boolean values true/false: '_Bool';
  • signed for signed integers or unnamed enums: 'signed char', 'signed short', 'signed int', 'signed long', 'signed long long', 'enum { .... };';
  • unsigned for unsigned integers: 'unsigned char', 'unsigned short', 'unsigned int', 'unsigned long', 'unsigned long long';
  • floating for floating-point values: 'float', 'double', 'long double';
  • character for characters only: 'char';
  • named enum for a named set of user-defined values: 'enum name { .... };'.

This model does not include pointers.

The C language allows much freedom in casting between arithmetic types, but it can also lead to hidden problems such as loss of sign, value, or precision. Despite its strictness, the MISRA standard does allow conversions between arithmetic types when the operands have the same essential types.

Exception: the essential types of the left and right operands of the operators '+', '-', '+=', and '-=' can be 'character' and 'signed' / 'unsigned' respectively.

Example of non-compliant code:

enum { A };
int i;
unsigned u;
void foo()
{
  A + u;
  0.f - i;
  A > (_Bool)0;
}

Example of code considered compliant from the viewpoint of this diagnostic:

void foo(unsigned short x, _Bool b)
{
    x + 1UL;
    if (b && x > 4U) ....
}

This diagnostic is classified as:

  • MISRA-C-10.4
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam