码迷,mamicode.com
首页 > 其他好文 > 详细

MISRA C:2012 Dir-1.1(只记录常犯的错误和常用的规则)Bit-fields inlineC99,NOT support in C90 #pragma

时间:2019-11-23 22:07:25      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:ber   rac   rail   sizeof   复合   枚举   scan   reading   ++i   

0202 转义字符‘[]’中的‘-’是执行时定义的。不要这么用。

#include <stdio.h>

extern void foo(char *cptr)
{
    scanf("Total %[a-z]", cptr);                /* Message 0202 */
}

https://stackoverflow.com/questions/19569545/how-to-use-scanf-to-input-specific-letter

The problem is, most likely, that you do not check for errors, and print the string even when the scanf call fails.

With the pattern "%[A-Z]" you only scan for uppercase letters, and if the string you input doesn‘t begin with upper case letters scan will fail. This of course means that the string input will never be filled, and will contain random data which you then print.

I recommend you read e.g. this reference. It will tell you that the function will return the number of items successfully scanned, which should be 1 in your case. If the scanf function in your code doesn‘t return 1 then there is a problem.

So you need to do something like

if (scanf(...) == 1)
    printf(...);
else
    printf("Something went wrong when reading input\n");

0320 在for语句中声明

extern void foo(void)
{
    for (int i = 0; i < 10; ++i)           /* Message 0320 and also 3220 */
    {

    }
}

0604  声明出现在复合语句中的语句之后

extern void foo(void)
{
  int i;

  i = 1;

  float f;                /* Message 0604 */

  f = 2.5F;

}
extern void foo(void)
{
  int i;

  float f;             
  i = 1;

  f = 2.5F;

}

0612 Size of object ‘%s‘ exceeds 32767 bytes - program does not conform strictly to ISO:C90.

char x[40][1000];         /* Message 0612 */

0930 Trailing comma at the end of an enumerator-list. 枚举列表末尾的逗号。

enum ED { ZERO=0, TWO=2, FOUR=4, };         /* Message 0930 */

1031 Initializer for ‘struct‘, ‘union‘ or array type is not a constant expression.

struct ST {int a; int b;};

extern void foo(int p1, int p2, int p3)
{
    int var = p1;                         /* Scalar object - OK */

    int buf1[] = {1, 2, 3};

    int buf2[3]= {p1, p2, p3};            /* Message 1031 */

    struct ST sx = {p1, p2};              /* Message 1031 */
}
Constant Expressions
Constant expressions may never contain (except as an operand to the sizeof operator):

Assignment operators
Increment or decrement operators
Comma operators
Function calls
Integral constant expressions
... are used in case expressions, to specify array bounds, to specify the size of a bit-field and as enumerator constants.

They may contain:

Integral constants
Enum constants
Character constants
sizeof expressions
Floating constants cast to an integer type
... but may not contain (except as an argument to the sizeof operator):

Arrays
* operator
& operator
Structure member operations

1053 在此初始化列表中使用了标识符

int arr[40] = { [0] = 1, [10] = 2, [30] = 3 };          /* Message 1053 */

1054 A compound literal has been used.

struct ST { int im; float fm; };

extern struct ST stx;

void foo(void)
{
    stx = (struct ST) {45, 1.23F};              /* Message 1054 */
}

1055 The keyword ‘inline‘ has been used.

static inline int foo(int a, int b)     /* Message 1055 */
{
    return (a * 10) + b;
}

3116 Unrecognized #pragma arguments ‘%s‘ This #pragma directive has been ignored.

 




MISRA C:2012 Dir-1.1(只记录常犯的错误和常用的规则)Bit-fields inlineC99,NOT support in C90 #pragma

标签:ber   rac   rail   sizeof   复合   枚举   scan   reading   ++i   

原文地址:https://www.cnblogs.com/focus-z/p/11919743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!