2012-10-13 88 views
0

我試圖做一個簡單的C計算器,因爲我是一個新手,並認爲這將是一個好主意。這是我的代碼:簡單的C計算器錯誤

#include <stdio.h> 
main() 
{ 
char b; 
int a,c,d; 
printf("Please enter your first number: "); 
scanf("%d",&a); 
printf("Please enter your second number: "); 
scanf("%d",%c); 
printf("Enter your operation"); 
scanf("%c",&b); 
if (b == 'a') 
{ 
    d = a + c; 
    printf("The answer is: %d",d); 
} 
} 

當我編譯的代碼,這是錯誤/警告我得到:

D:...|4|warning: return type defaults to 'int'| 
D:...||In function 'main':| 
D:...|10|error: expected expression before '%' token| 

是否有人可以幫忙嗎?

+2

聲明main()爲int main(void)並返回0。 –

回答

5

變化中scanf%&

scanf("%d", &c); 
      ^

另外,代替main你想int main,你或許應該在年底返回的東西。

+0

從C99開始,返回main的東西並不是必須的。如果主體的末端沒有返回,則返回0。請參閱C99 5.1.2.2.3:'[達到}終止主函數的返回值爲0.' – halex

+0

@halex我知道,但我仍然認爲它是很好的形式。 – cnicutar

+0

我總是這樣做:)。只是想澄清 – halex

1

比較這兩種線,並告訴我們有什麼區別:

scanf("%d",&a); 
scanf("%d",%c);