<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[MISRA Discussion Forums - 8.14 Control statement expressions]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Thu, 05 Nov 2026 13:03:32 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[14.3 and enum constants in macro expansion]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1660</link>
			<pubDate>Thu, 19 Oct 2023 07:30:58 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=6269">Timofei</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1660</guid>
			<description><![CDATA[Hi,<br />
<br />
We have a configuration macro with a value being an enum constant. We use that macro in an if-statement, and it violates the rule 14.3.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum my_enum {<br />
    MY_ENUM<br />
};<br />
<br />
#define CONF_MACRO MY_ENUM<br />
<br />
if (CONF_MACRO == MY_ENUM) {...}</code></div></div><br />
<br />
Note that in the case of enum constants we cannot convert the if-statement to a preprocessor #if-statement since the values of enum constants are not known at the preprocessing step. How can we solve this issue?]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
We have a configuration macro with a value being an enum constant. We use that macro in an if-statement, and it violates the rule 14.3.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum my_enum {<br />
    MY_ENUM<br />
};<br />
<br />
#define CONF_MACRO MY_ENUM<br />
<br />
if (CONF_MACRO == MY_ENUM) {...}</code></div></div><br />
<br />
Note that in the case of enum constants we cannot convert the if-statement to a preprocessor #if-statement since the values of enum constants are not known at the preprocessing step. How can we solve this issue?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[for loop iterator being comapred to a variable of another type]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1583</link>
			<pubDate>Mon, 11 Oct 2021 22:14:33 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5726">ZESS</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1583</guid>
			<description><![CDATA[So I had this bug where I was trying to compare my "for loop" iterator to a variable "var":<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>for (u_int8_t i=0; i&lt;var; i++) ....</code></div></div><br />
<br />
"var" was coded on 16 bits while "i" was coded on 8 bits , this led to an infinite loop as "i" could never reach "var".<br />
I was wondering why this was not raised as a MISRA warning while compilation phase, I thought at first that I was disabling some rules but turned out I was not. I looked for a MISRA rule prohibiting this kind of implementation, but I could not find any.<br />
<br />
I'm not sure if it's related to the tool I'm using or indeed there is no such rule that prevents comparing two variables with two different data types especially if they are used as break condition to loops.]]></description>
			<content:encoded><![CDATA[So I had this bug where I was trying to compare my "for loop" iterator to a variable "var":<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>for (u_int8_t i=0; i&lt;var; i++) ....</code></div></div><br />
<br />
"var" was coded on 16 bits while "i" was coded on 8 bits , this led to an infinite loop as "i" could never reach "var".<br />
I was wondering why this was not raised as a MISRA warning while compilation phase, I thought at first that I was disabling some rules but turned out I was not. I looked for a MISRA rule prohibiting this kind of implementation, but I could not find any.<br />
<br />
I'm not sure if it's related to the tool I'm using or indeed there is no such rule that prevents comparing two variables with two different data types especially if they are used as break condition to loops.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 14.3 and preprocessor constants]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1581</link>
			<pubDate>Fri, 01 Oct 2021 09:31:24 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5715">hummelvario</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1581</guid>
			<description><![CDATA[Hi, <br />
<br />
<br />
I have a question regarding a reported MISRA violation in our code, which isn't completely clear to me after reading the MISRA Guidelines.<br />
<br />
Does the MISRA C:2012 Rule 14.3 (Controlling expressions shall not be invariant) also apply to predefined constants, which include function-lika macros?<br />
<br />
Example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#define ROUND_TO(type, val) ((type) (((val) &lt; 0.0) ? ((val) - 0.5) : ((val) + 0.5)))<br />
<br />
#define RMS_TO_AVERAGE  ROUND_TO(uint16_t, ((70.0) / (1.11)))</code></div></div><br />
<br />
In this case the expression ((val) &lt; 0.0) is invariant, but it is only once used initialising a constant (RMS_TO_AVERAGE).<br />
<br />
<br />
Regards, Kevin]]></description>
			<content:encoded><![CDATA[Hi, <br />
<br />
<br />
I have a question regarding a reported MISRA violation in our code, which isn't completely clear to me after reading the MISRA Guidelines.<br />
<br />
Does the MISRA C:2012 Rule 14.3 (Controlling expressions shall not be invariant) also apply to predefined constants, which include function-lika macros?<br />
<br />
Example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#define ROUND_TO(type, val) ((type) (((val) &lt; 0.0) ? ((val) - 0.5) : ((val) + 0.5)))<br />
<br />
#define RMS_TO_AVERAGE  ROUND_TO(uint16_t, ((70.0) / (1.11)))</code></div></div><br />
<br />
In this case the expression ((val) &lt; 0.0) is invariant, but it is only once used initialising a constant (RMS_TO_AVERAGE).<br />
<br />
<br />
Regards, Kevin]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 14.2 and system level]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1352</link>
			<pubDate>Wed, 14 Jun 2017 09:13:41 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4742">xiangke</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1352</guid>
			<description><![CDATA[As We know, analysis of scope is classified as single translation unit and system.<br />
Rule 14.2 is system analyzed scope, <br />
In my opinion, a For loop is always placed on a function, it is a code block. a loop counter it uses has no linkage.why the analysis for it shall be dependent on the other module, can anybody give a example for explanation?<br />
thanks very much in advance!]]></description>
			<content:encoded><![CDATA[As We know, analysis of scope is classified as single translation unit and system.<br />
Rule 14.2 is system analyzed scope, <br />
In my opinion, a For loop is always placed on a function, it is a code block. a loop counter it uses has no linkage.why the analysis for it shall be dependent on the other module, can anybody give a example for explanation?<br />
thanks very much in advance!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 14.4 and the explanation is not enough to understand why]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1344</link>
			<pubDate>Tue, 06 Jun 2017 09:54:43 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4742">xiangke</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1344</guid>
			<description><![CDATA[MISRA C 2012 guideline says:<br />
Strong typing requires the controlling expression of an if statement or iteration-statement to have<br />
essentially Boolean type.<br />
<br />
For example:<br />
typedef unsigned short tudtKalEvents;<br />
#define  udtSTOP_EVENT (tudtKalEvents)0x0001;<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if(udtEvents &amp; (tudtKalEvents) udtSTOP_EVENT)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vidStop();<br />
&nbsp;&nbsp;}</code></div></div>I think the code is ok, I use the &amp; statement as the conditional expression. As we know, the value is not equal to zero, the condition shall be judged to be true,<br />
why We must use the boolean type?]]></description>
			<content:encoded><![CDATA[MISRA C 2012 guideline says:<br />
Strong typing requires the controlling expression of an if statement or iteration-statement to have<br />
essentially Boolean type.<br />
<br />
For example:<br />
typedef unsigned short tudtKalEvents;<br />
#define  udtSTOP_EVENT (tudtKalEvents)0x0001;<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if(udtEvents &amp; (tudtKalEvents) udtSTOP_EVENT)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vidStop();<br />
&nbsp;&nbsp;}</code></div></div>I think the code is ok, I use the &amp; statement as the conditional expression. As we know, the value is not equal to zero, the condition shall be judged to be true,<br />
why We must use the boolean type?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Question about 14.2 and the controlling variable]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1228</link>
			<pubDate>Tue, 23 Feb 2016 09:43:24 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1600">triboix</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1228</guid>
			<description><![CDATA[Hello,<br />
<br />
14.2 states that the "controlling variable" must be of essentially boolean type.<br />
<br />
Does that mean that the following code is non-compliant?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct stuff<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;uint32_t id;<br />
};<br />
struct stuff stuff_array[MAXSTUFF];<br />
<br />
struct stuff* stuff_lookup(uint32_t id)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;uint8_t i;<br />
&nbsp;&nbsp;&nbsp;&nbsp;struct stuff* found = NULL;<br />
&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; (i &lt; MAXSTUFF) &amp;&amp; (found == NULL); i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (stuff_array[i].id == id) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found = &amp;(stuff_array[i]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return found;<br />
}</code></div></div><br />
Thanks a lot for any help!]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
14.2 states that the "controlling variable" must be of essentially boolean type.<br />
<br />
Does that mean that the following code is non-compliant?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct stuff<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;uint32_t id;<br />
};<br />
struct stuff stuff_array[MAXSTUFF];<br />
<br />
struct stuff* stuff_lookup(uint32_t id)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;uint8_t i;<br />
&nbsp;&nbsp;&nbsp;&nbsp;struct stuff* found = NULL;<br />
&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; (i &lt; MAXSTUFF) &amp;&amp; (found == NULL); i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (stuff_array[i].id == id) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found = &amp;(stuff_array[i]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return found;<br />
}</code></div></div><br />
Thanks a lot for any help!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[14.3 and do-while within logical-and operator]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1193</link>
			<pubDate>Mon, 06 Jul 2015 05:58:18 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1443">lovewar</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1193</guid>
			<description><![CDATA[Would the following example be compliant with rule #14.3?<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>do {<br />
&nbsp;&nbsp;&nbsp;&nbsp; // code..<br />
 } while ( (s8a &lt; 10) &amp;&amp; (s8a &gt; 20) );&nbsp;&nbsp;/* is &amp;&amp; operator an essentially boolean controlling expression ? */</code></div></div>]]></description>
			<content:encoded><![CDATA[Would the following example be compliant with rule #14.3?<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>do {<br />
&nbsp;&nbsp;&nbsp;&nbsp; // code..<br />
 } while ( (s8a &lt; 10) &amp;&amp; (s8a &gt; 20) );&nbsp;&nbsp;/* is &amp;&amp; operator an essentially boolean controlling expression ? */</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 14.2 What is meant by assign"]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1149</link>
			<pubDate>Fri, 13 Feb 2015 12:15:46 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=196">misra-c</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1149</guid>
			<description><![CDATA[In the 1st clause what is meant by â€œassign a value to the loop counterâ€.  May this â€œvalueâ€ be an expression with side-effects? <div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int index;<br />
for ( set_val(&amp;index) ;&nbsp;&nbsp;index &lt; 10 ; index++) // Is this compliant if set_val assigns to index</code></div></div>Note: the 2nd and 3rd clauses explicitly refer to expressions and whether side-effects are permitted.]]></description>
			<content:encoded><![CDATA[In the 1st clause what is meant by â€œassign a value to the loop counterâ€.  May this â€œvalueâ€ be an expression with side-effects? <div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int index;<br />
for ( set_val(&amp;index) ;&nbsp;&nbsp;index &lt; 10 ; index++) // Is this compliant if set_val assigns to index</code></div></div>Note: the 2nd and 3rd clauses explicitly refer to expressions and whether side-effects are permitted.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[8.14 and "involved in a decision to exit the loop"]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=961</link>
			<pubDate>Wed, 01 May 2013 14:23:12 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=64">gs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=961</guid>
			<description><![CDATA[Section 8.14 defines a <span style="font-style: italic;" class="mycode_i">loop counter</span> as "an object, array element or member of a structure or union which"<ul class="mycode_list"><li>1. has scalar type,<br />
2. varies monotonically on each iteration of a given instance of a loop, and<br />
3. is involved in a decision to exit the loop.</li>
</ul>
My question is, "To what extent does the item in question need to be "involved in a decision to exit the loop"? For example, consider the code:<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
if( g(x) )<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
...</code></div></div>Is 'x' "involved in a decision to exit the loop"? <br />
<br />
What of<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
if( h() )<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
...</code></div></div>where 'h()' returns a value based on, say, a static variable, 'q'; is 'q' "involved in a decision to exit the loop"?]]></description>
			<content:encoded><![CDATA[Section 8.14 defines a <span style="font-style: italic;" class="mycode_i">loop counter</span> as "an object, array element or member of a structure or union which"<ul class="mycode_list"><li>1. has scalar type,<br />
2. varies monotonically on each iteration of a given instance of a loop, and<br />
3. is involved in a decision to exit the loop.</li>
</ul>
My question is, "To what extent does the item in question need to be "involved in a decision to exit the loop"? For example, consider the code:<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
if( g(x) )<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
...</code></div></div>Is 'x' "involved in a decision to exit the loop"? <br />
<br />
What of<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
if( h() )<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
...</code></div></div>where 'h()' returns a value based on, say, a static variable, 'q'; is 'q' "involved in a decision to exit the loop"?]]></content:encoded>
		</item>
	</channel>
</rss>