C语言---常见错误与语言规范

转载标明出处!

常见错误与语言规范

常见错误

变量名错误
  • 变量名不能使用关键字
int double// 编译失败
  • 变量名不能和函数名同名
int printf;
  • 变量没定义,就直接使用
parm = 20;
  • 变量的输入与使用顺序不当
int age;
int sum;
    sum = age + 16;
    scanf("%d", &age);
    printf("%d\n", sum);

image-20210317131707185

  • 变量严格区分大小写
int c = 5;
printf("%d", C);
  • 忘了加分号 ( ; ) (;) ;
int c = 5
标准输入与输出
  • 变量计算,忘了加占位符。
int c = 5;
int d = 5;
// 计算相加之和
printf("sum = c + d");
  • 输入字符的格式与要求不一致
scanf("%d%d%d", &c1, &c2);
  • 忘了加取址符,隐藏错误。
int c;
scanf("%d", c);

语法规范

  • switch…case 由于粗心漏写 break 语句。
swich(num)
{
    case 0: printf("A -B\n");
    case 1: printf("C -D\n");
    case 2: printf("E -F\n");
    case 3: printf("G -H\n");
    default:printf(" ");
}

应写成:

swich(num)
{
    case 0: {printf("A -B\n"); break;}
    case 1: {printf("C -D\n"); break;}
    case 2: {printf("E -F\n"); break;}}
    case 3: {printf("G -H\n"); break;}
    default: {printf(" ");}
}

常见错误提示信息

常见错误

error: array subscript is not an integer(数组下标不是整数)

错误写法:

int arr[100];
double i = 3.0;

    arr[i];

报错信息:

image-20210317140238677

推荐修改成:

int arr[100];
int i = 3.0;

    arr[i];
error: expected declaration or statement at end of input(花括号不匹配)

错误写法:

    int param_1 = 1;
    if(param_1 == 1) {
        if(param > 0) {
    }

报错信息:

image-20210317141144296

推荐修改成:

int param_1 = 1;
    if(param_1 == 1) {
        if(param > 0) {
            // 请输入
        }
    }
error: redefinition of ‘fun_a’(函数重复定义)

错误写法:

void fun_a(void)
{

}

void fun_a(void)
{

}

报错信息:

image-20210317141544873

推荐修改成:

void fun_a(void)
{

}
error: ‘a’ undeclared (first use in this function)(a 变量未声明)

错误写法:

a = 1;

报错信息:

image-20210317141836705

推荐修改成:

int a = 1;
error: expected ‘)’ before ‘{’ token(缺少园括号)

错误写法:

int sum = 0;
    if(((sum * 100 + 10) == 100) {}

报错信息:

image-20210317144818006

推荐修改成:

int sum = 0;
    if(((sum * 100) + 10) == 100) {}

段错误

​ 段错误是计算机软件运行过程中出现一段特殊的错误。当程序试图访问一段不允许访问(只读)的内存区域会发生段错误。

使用未经初始化或已释放的地址
#include <stdio.h>

int main(int argc, const char* argv[])
{
    int param = 100;
    int *str = &param;

    str = NULL;
    printf("str = %d\n", *str);
    system("pause");
    return 0;
}
访问受系统保护的内存地址
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *str = (int *)0X20000000;
    *str = 1000;
	system("pause");

    return 0;
}
访问只读的内存
#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *str = "hello";
    *str = "hello kkb";

    printf("%s\n", str);
    system("pause");

    return 0;
}
数组越界
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int arr[10] = {0};

    for(int i = 0; i < 100; i++) {
        arr[i] = 100;
    }
    system("pause");
    return 0;
}
堆栈溢出
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int arr[100000000] = {0};
    system("pause");
    return 0;
}

常见警告

warning: implicit declaration of function ‘printf’

错误写法:

int main()
{
    printf("Hello!\n");

    return 0;
}

报错信息:

image-20210317142312323

推荐修改成:

#include <stdio.h>

int main()
{
    printf("Hello!\n");

    return 0;
}
warning: implicit declaration of function ‘fun_a’ [-Wimplicit-function-declaration]

错误写法:

#include <stdio.h>

int main()
{
    fun_a();
    return 0;
}

void fun_a(void) {}

报错信息:

image-20210317145105146

推荐修改成:

#include <stdio.h>

void fun_a(void);

int main()
{
    fun_a();
    return 0;
}

void fun_a(void) {}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值