Examples for 12.9 - unary minus operator

6.12 Expressions

Moderators: misra-c, david ward

Locked
Manni
Posts: 11
Joined: Mon Feb 20, 2006 12:28 pm

Examples for 12.9 - unary minus operator

Post by Manni » Mon Apr 24, 2006 1:00 pm

Hi

Question about Rule 12.9: The unary minus operator shall not be applied to an expression whose underlying type is unsigned.

Code: Select all

uint32_t u_int_a;
uint32_t u_int_b;

u_int_a = -u_int_b; 	
1. Is this incorrect?
2. Shows this example, what the rule 12.9 mean?



4. Is this correct? (a = int)

Code: Select all

uint32_t int_a;
uint32_t u_int_b;

u_int_a = -(int32_t)u_int_b;

5. Is this correct, too? (a = uint)

Code: Select all

uint32_t u_int_a;
uint32_t u_int_b;

u_int_a = -(int32_t)u_int_b;
6. I think it it is correct to rule 12.9, but breach 10.1. Right?


7. Have somebody good examples for rule breaks? And how to solve it?

best regards,
Manni
best regards,
Manni

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

Post by misra-c » Thu Apr 27, 2006 11:18 am

Code: Select all

uint32_t u_int_a; 
uint32_t u_int_b; 

u_int_a = -u_int_b; 


This is a violation of rule 12.9 since the underlying type of u_int_b is unsigned.

----

Code: Select all

int32_t int_a;    /* changed from uint32_t */
uint32_t u_int_b; 

int_a = -(int32_t)u_int_b; /* changed u_int_a to int_a */

This is allowed.

----

Code: Select all

uint32_t u_int_a; 
uint32_t u_int_b; 

u_int_a = -(int32_t)u_int_b;


This is compliant with rule 12.9, but violates 10.1. because of the change of signedness on assignment.
---
Posted by and on behalf of
the MISRA C Working Group

Locked

Return to “6.12 Expressions”