60 The right hand operand of a logical && or || operator shall not contain side effects.
如下面的代码不是允许的:
[cpp] view plain copy
1. if ( ( a == 3 ) && b++ )
2. {
3. c = 1;
4. }
5. if ( ( a == 3 ) || b++ )
6. {
7. c = 1;
8. }
9. if ( ( a == 3 ) || func33 ( ) )
10. {
11. c = 1;
12. }
61 The operands of a logical && or || shall be primary-expressions.
用”()” 显示说明每一个优先级.
62 The operands of logical operators (&&, || and !) should be effectively Boolean. Expressions that are effectively Boolean should not be used as operands to operators othe
r than (&&, || and !).
63 bitwise operators shall not be applied to exprands whose underlying type is signed
64 The right hand operand of a shift operator shall lie between zero and one less than the width in
bits of the underlying type of the left hand operand.
65 The unary minus operator shall not be applied to an expression whose underlying type is unsigned.
66 The comma operator shall not be used.
67 Evaluation of constant unsigned integer expressions should not lead to wrap-around.
68 The underlying bit representations of floatingpoint values shall not be used.
69 The increment (++) and decrement (--) operators should not be mixed with other operators in an
expression.
经常在网上或书上看到类似于下面的题目:
x = y+++z; x = ?
看到misra-c标准后, 我不由的感叹:这个世界还有很多有意义的事等着我们程序员去做.
70 Assignment operators shall not be used in expressions that yield a Boolean value
如:
[cpp] view plain copy
1. if ( ( a = b ) != 0 )
2. {
3. c = 1;
4. }
editordoesnotcontainamaintype
71 Tests of a value against zero should be made explicit, unless the operand is effectively Boolean.
72 Floating-point expressions shall not be tested for equality or inequality.
73 The controlling expression of a for statement shall not contain any objects of floating type.
74 The three expressions of a for statement shall be concerned only with loop control
75 Numeric variables being used within a for loop for iteration counting shall not be modified in the
body of the loop.
76 Boolean operations whose results are invariant shall not be permitted.
77 There shall be no unreachable code.
78 All non-null statements shall either :
a) have at least one side-effect however
executed, or
b) cause control flow to change.
79 Before preprocessing, a null statement shall only occur on a line by itself; it may be followed by a comment provided that the first character following the null statement is a white space character.
78和79的意思是
非空语句必须要么产生副作用( side-effect) (副作用是指表达式执行后对程序运行环境造成的影响。赋值语句、自 增操作等都是典型的具有副作用的操作。) ;或者使程序流程改变。 例如,下面的代码是不允许的: ? x > = 3 ;
错误在于x 和3 比较的结果被丢弃了
80 The goto statement shall not be used.
81 The continue statement shall not be used.
82 For any iteration statement there shall be at most one break statement used for loop termination.
83 A function shall have a single point of exit at the end of the function.
84 The statement forming the body of a switch, while, do ... while or for statement shall be a
compound statement.
85 An if (expression) construct shall be followed by a compound statement. The else keyword shall be
followed by either a compound statement, or another if statement.
84和85的意思是 if,while等语句要加括号
86 All if … else if constructs shall be terminated with an else clause.
87 A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement.(switch语句的主体要有括号)
88 An unconditional break statement shall terminate every non?empty switch clause.
89 The final clause of a switch statement shall be the default clause.
90 A switch expression shall not represent a value that is effectively Boolean.
91 Every switch statement shall have at least one case clause.
92 Functions shall not be defined with a variable number of arguments.
如下面的代码是不允许的
static void rule69 ( SC_8 *fmt, ... ) ;
93 Identifiers shall be given for all of the parameters in a function prototype declaration
94 The identifiers used in the declaration and definition of a function shall be identical.
95 Functions with no parameters shall be declared with parameter type void.
96 The number of arguments passed to a function shall match the number of parameters.
97 All exit paths from a function with non-void return type shall have an explicit return statement with
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论