如何解决C程序中的分段错误

How to solve segmentation error in C program

伙计们,我遇到了一个分段错误,这个错误更具体一点:

Segmentation fault

有谁知道如何解决这个问题?我的代码很简单不知道为什么会报错,有谁知道怎么一步步解决的吗?

我正在使用此命令将我的 c 文件转换为可执行文件 gcc test.c -o test 然后我 运行 我的 test 文件 ./test,但后来我明白了我上面说的错误。

我的代码在c:

#include <stdio.h>

int main(){
int age;
int aget;
int total;
printf("Input your age: ");
scanf("%i", &idade);
printf("Input your age two: ");
scanf("%i", &age);
base = age + aget;
printf(total);
return 0;
}

printf 需要格式字符串。这里我们需要告诉它 %d for integer in decimal.

total = idade + age;
printf("%d\n", total);

关于如何使用 printf 的一些随机资源:https://man7.org/linux/man-pages/man3/printf.3.html

变量 base 未声明。 变量 idade 未声明。 如果您使用的是 base,它应该是 printf("%d",base)

#include <stdio.h>

int main(){
    int age;
    int aget;
    int total;
    printf("Input your age: ");
    scanf("%i", &age);
    printf("Input your age two: ");
    scanf("%i", &aget);
    total = age + aget;
    printf("%d",total);
    return 0;
}