Reputation: 7540
MISRA 2004 rule 5.1 states that all identifiers must have the first 31 characters unique. What is the reason for this rule? Is it a technical limitation with some compilers?
Upvotes: 3
Views: 11569
Reputation: 42109
The C standards only guarantee that a certain number of initial characters in identifiers are significant. For C99 this is 31 characters for external identifiers. Even this is a huge step up from ANSI/IS C, which guarantees only 6 significant characters for external identifiers… (So if you're wondering why so many old C functions have unpronounceable names, this is one reason.)
In practice compilers tend to support a higher number of significant characters in identifiers (and IIRC the C standard even has a footnote encouraging this), but MISRA probably wanted to pick a “safe” limit already guaranteed by the then-most-recent C standard, C99, without imposing the limit of 6 that would be guaranteed by C90 which MISRA 2004 otherwise follows.
edit: Since it has been questioned twice in the comments, let me clarify: MISRA 2004 does not follow C99, and there is no hard evidence that the C99 standard contributed to MISRA's chosen limit of specifically 31 characters. However, the limit does not come from C90 (ISO C), because C90 specifies a limit of 6 characters. So, one must either accept that MISRA picked the number 31 independently, or followed the example of C99 in this particular decision. Of course it might be that both picked the same number due to that being the lower bound in popular compilers of the day, but at the very least it can be argued that the example of the older C99 validates the choice.
Upvotes: 7
Reputation: 213276
MISRA-C:2004 follows the C90 standard, which only requires the 6 first characters of an identifier to be treated as distinct ones. You can read the rationale in the MISRA document.
MISRA-C:2004 Rule 14:
The ISO standard requires external identifiers to be distinct in the first 6 characters. However compliance with this severe and unhelpful restriction is considered an unnecessary limitation since most compilers/linkers allow at least 31 character significance (as for internal identifiers).
The ISO standard referred to is ISO 9899:1990 (C90). The purpose of the rule is ensure that you are using a sane, safe compiler with enough characters of significance.
Upvotes: 2