C程序在使用scanf后停止工作

C program stopped working after use scanf

所以我有这个 C 代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int a;
   int b;
   int c;
   scanf("%d", &b);
   scanf("%d", &a);
   c = a + b;
   printf(c);
   return 0;
}

但是,在我为 a 和 b 插入数字后,程序停止运行。请帮我 C菜鸟在这里

printf(c);

应该是

printf("%d\n", c); /* `\n` at the end of the string flushes the `stdout` */

因为 printf 期望 const char* 作为它的第一个参数,而不是 int

在您的代码中,您的以下行是错误的:

printf(c);

因为 printf() 语法就像我在下面写的那样

printf("%d",c);

所以你现在的代码是:

#include <stdio.h>

int main()
{
   int a;
   int b;
   int c;
   scanf("%d", &b);
   scanf("%d", &a);
   c= a + b;
   printf("%d",c); //this is the correct printf() syntax 
   return 0;
}