簡體   English   中英

使用 scanf 后 C 程序停止工作

[英]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 noob

在您的代碼中,以下行是錯誤的:

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;
}
printf(c);

應該

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

因為printf需要一個const char*作為它的第一個參數,而不是一個int

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM