Skip to main content

IAR Embedded Workbench for RX 5.20

MISRAC++2023-9.4.1

In this section:
Synopsis

(Required) All if... elseif constructs shall be terminated with an else statement

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

If ... else if constructs that are not terminated with an else clause were detected. This check is identical to MISRAC++2008-6-4-2, MISRAC2004-14.10, MISRAC2012-Rule-15.7.

Coding standards
MISRA C:2004 14.10

(Required) All if ... else if constructs shall be terminated with an else clause.

MISRA C:2012 Rule-15.7

(Required) All if ... else if constructs shall be terminated with an else statement

MISRA C++ 2008 6-4-2

(Required) All if ... else if constructs shall be terminated with an else clause.

Code examples

The following code example fails the check and will give a warning:

#if __cplusplus
  #include <cstdlib>
	#include <cstdio>
#else
  #include <stdlib.h>
#endif

void example(void) {
	if (!rand()) {
		printf("The first random number is 0");
	} else if (!rand()) {
		printf("The second random number is 0");
	}
}

The following code example passes the check and will not give a warning about this issue:

#if __cplusplus
	#include <cstdlib>
	#include <cstdio>
#else
	#include <stdlib.h>
#endif

void example(void) {
	if (!rand()) {
		printf("The first random number is 0");
	} else if (!rand()) {
		printf("The second random number is 0");
	} else {
		printf("Neither random number was 0");
	}
}