[previous] MISRA C:2012  Rule-14.1:  (Required) [next] A loop counter shall not have essentially floating type

Example Code:


#pragma PRQA_MESSAGES_OFF 1056

#include "misra.h"
#include "m3cmex.h"

extern int16_t rule_1401( void )
{
    uint16_t ui;
    uint16_t buf[10];
    float32_t ft;
    float32_t fuf[10];
    volatile _Bool blx;

    for (ui = 0u; ui < 10u; ++ui)
    {
        buf[ui] = ui + 4u;
        fuf[ui] = (float32_t)(ui);
    }

    for (ui = 0 ; buf[ui] < 10u ; ++ui )
    {
    }

    for (ft = 0.0f; ft < f32a; ++ft)                                    /* 3340 3342 */
    {
    }

    for (ui = 0 ; fuf[ui] < 5.0f ; ++ui )                               /*      3342 */
    {
    }

    ft = 0.0f;

    /* The following while loop is non-compliant because ft
       is being used as a loop counter. */

    while (ft < 1.0f)                                                   /*           */         /* Non-compliant */
    {
        ft += 0.001f;
    }


    /* ft does not change in the following loop so cannot
       be a loop counter */

    ft = get_f32();

    do                                                                  /*           */         /* Compliant */
    {
        u32a = get_u32();
    } while (((float32_t)u32a - ft) > 10.0f);

    return 1;
}


QAC messages that encompass this guideline:

3340 Floating point variable used as 'for' loop control variable.
3342 Controlling expression of 'for' loop is a floating point comparison.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents