我在vs2010(win32控制檯應用程序)中運行我的C代碼。它被編譯爲C++應用程序。C預處理器和if-else條件語句
#include "stdafx.h"
#define YES 1;
#define NO 0;
// function to determine if an integer is even
int isEven(int number)
{
int answer;
if (number % 2 == 0)
answer = YES;
else
answer = NO;
return answer;
}
int main()
{
int isEven(int number);
if (isEven(17) == YES)
printf("yes ");
else
printf("no ");
if (isEven(20) == YES)
printf("yes\n");
else
printf("no\n");
return 0;
}
編譯錯誤如下。
p300.cpp(18): error C2181: illegal else without matching if
p300.cpp(30): error C2143: syntax error : missing ')' before ';'
p300.cpp(30): error C2059: syntax error : ')'
p300.cpp(31): warning C4390: ';' : empty controlled statement found; is this the intent?
p300.cpp(33): error C2181: illegal else without matching if
p300.cpp(37): error C2143: syntax error : missing ')' before ';'
p300.cpp(37): error C2059: syntax error : ')'
p300.cpp(38): warning C4390: ';' : empty controlled statement found; is this the intent?
p300.cpp(40): error C2181: illegal else without matching if
然後,我還試圖插入幾個{ }
每個if-else
條件語句,但代碼仍然編譯時失敗。我的代碼有什麼問題?
我想補充一點,你可以減少功能: INT ISEVEN(INT數){!返回(數& 1);} – Reinderien 2010-06-22 02:30:12
@Reinderien好了,如果不使用他承認不必要的YES和NO定義'回報(編號: 1)?NO:YES;'雖然 – 2010-06-22 02:33:15