主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

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.