Rules 12.1 and 12.5 and unary operators

6.12 Expressions

Moderators: misra-c, david ward

pmhill
Posts: 4
Joined: Wed Apr 08, 2009 9:17 am
Company: University of Leeds

Rules 12.1 and 12.5 and unary operators

Postby pmhill » Thu Apr 22, 2010 9:06 am

It is said in rules 12.1 and 12.5 that 12.5 is a special case of 12.1.

However, in rule 12.1, it is stated that no parentheses are required for the operand of a unary operator
while in rule 12.5 it is said that the operands of the logical operators && and || must be primary expressions;
the latter is exemplified in 12.5 with the following example:

if ( x && ( !y ) ) /* make !y primary */

So for rule 12.1, is something like:
if ( x && !y )
...
a violation?

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rules 12.1 and 12.5 and unary operators

Postby misra-c » Thu May 13, 2010 10:39 am

Rule 12.1 provides some guidance on the use of parentheses in expressions. For logical operators, Rule 12.5 provides a stricter rule and therefore overrides 12.1 in this instance.
---
Posted by and on behalf of
the MISRA C Working Group

udi
Posts: 8
Joined: Wed May 11, 2016 8:24 am
Company: Elbit

Re: Rules 12.1 and 12.5 and unary operators

Postby udi » Wed Aug 17, 2016 5:55 am

What is the rationale for unary logical not operator (!a) being treated differently than the unary minus operator (-a) ?

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rules 12.1 and 12.5 and unary operators

Postby misra-c » Fri Dec 09, 2016 8:43 am

When the MISRA-C:2004 guidelines were updated to MISRA C:2012 it was realised that there was no reason to maintain the difference between the unary operators. Rule 12.5 was removed and the requirement for parentheses is now covered only by 12.1. This means that postfix, unary and cast operands of && and || are permitted without brackets in MISRA C:2012.

If you are continuing to use MISRA-C:2004, then the justification for any deviation could include that this Rule has been relaxed for MISRA C:2012
---
Posted by and on behalf of
the MISRA C Working Group


Return to “6.12 Expressions”

Who is online

Users browsing this forum: No registered users and 1 guest