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

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.

 




posted @   清风oo  阅读(741)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示