主要内容

MISRA C:2012 Rule 4.1

Octal and hexadecimal escape sequences shall be terminated

描述

规则定义

Octal and hexadecimal escape sequences shall be terminated 1 .

理由

如果一个八进制或十六进制转义序列后面紧跟着其他字符,可能会导致混淆。例如,字符常量 '\x1f' 由单个字符组成,而字符常量 '\x1g' 则由两个字符 '\x1''g' 组成。多字符常量表示为整数的方式由实现定义。

如果字符常量或字符串字面值中的每个八进制或十六进制转义序列都明确终止,则可以减少潜在的混淆。

故障排除

如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

在此示例中,如果转义序列没有以字符串字面值的结束符或另一个转义序列来终止,就会违反该规则。

const char *s1 = "\x41g";     /* Non-compliant */
const char *s2 = "\x41" "g";  /* Compliant - Terminated by end of literal */
const char *s3 = "\x41\x67";  /* Compliant - Terminated by another escape sequence*/

const char *c1 = "\1412";   /* Non-compliant */
const char *c2 = "\141\t";  /* Compliant - Terminated by another escape sequence*/

检查信息

组:字符集和词法约定
类别:必需
AGC 类别:必需

版本历史记录

在 R2014b 中推出


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace® Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.