C 预处理器和 if-else 条件语句
我在 vs2010(win32 控制台应用程序)中运行我的 C 代码。它被编译为 C++ 应用程序。
#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
条件语句插入几个{ }
,但代码仍然编译失败。我的代码有什么问题吗?
I run my C code in vs2010 (win32 console application). It was compiled as C++ application.
#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;
}
Compiler error as below.
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
Then I also tried to insert several of { }
for each of if-else
condition statement, but the code still compiled failed. What's wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编译错误是由于
#define
语句中的分号造成的。删除它们。#define
是一个预处理器宏,而不是 C 语法。它不需要分号。预处理器对YES
和NO
进行直接替换,这使得:变成:
在 if 和 else 之间生成两个语句,因此会出现编译器错误。我怀疑当您添加
{
和}
时,您会遇到不同的编译器错误,因为成为
顺便说一句,这个问题被标记为
c
,但是您的文件名是.cpp
,这是c++的常见后缀。如果您使用 C++,请务必使用bool
类型。The compile error is due to the semicolons on your
#define
statements. Remove them.#define
is a preprocessor macro, not c syntax. It doesn't need a semicolon. The preprocessor does straight substitution onYES
andNO
, which makes:Turn into:
That makes two statements between if and else, so compiler errors ensue. I suspect you get different compiler errors when you add
{
and}
due tobecoming
By the way, this question is tagged
c
, but your filename is.cpp
, which is a common suffix for c++. If you are using c++, definitely use thebool
type.首先,删除定义末尾的分号
to start with, remove the semicolons from the end of the defines
正如其他用户所提到的,删除分号应该可以解决您手头的问题。
然而,我认为指出一些在编写宏时应该注意的事情可能会很方便。
虽然有时很方便,但最好了解在 C 中使用宏的陷阱。
1) 这里没有显式类型转换。
即“NO”可以是 uint16_t 或 uint32_t。
您在上面的示例中将变量定义为 int。如果我们假设您使用的是 16 位机器,则您的变量答案是 16 位长。但是您还没有为 NO 定义类型。在这种情况下,这不是什么大问题,因为 NO 的值很小。但是,如果您将其与“答案”进行比较,那么应该清楚答案的最大值应该是多少。
如果您尝试将此代码移植到 32 位计算机,您也可能会遇到问题。
定义以下内容的良好实践:
显式类型转换:
告诉编译器执行您想要的操作的另一种方法:
如果你有数字 0xFFFF。根据它是有符号还是无符号,这对您的编译器可能有不同的含义。因此,如果您的意图是使其不带符号,则可以通过执行以下操作显式告诉编译器以这种方式对待它:
注意“U”。这告诉编译器它是未签名的。
我认为这些都是很好的做法,在编写宏时应该牢记在心。它迫使您考虑使用常量的意图。此外,早期养成的这些习惯肯定会对你的职业生涯大有裨益。
As mentioned by other users, removing the semicolons should fix your problem at hand.
However, I thought it might be handy to point out a few more things you should be aware of while writing macros.
Although handy at times, it is good to be aware of the pitfalls of using Macros in C.
1) There is no explicit type casting here.
i.e 'NO' can be a uint16_t or a uint32_t.
You defined your variable in the above example as an int. If we assume you are on a 16 bit machine, your variable answer is 16 bits long. However you have not defined a type for NO. In this case it isn't much of an issue, since the value of NO is small. However if you were comparing it against your 'answer' then it should be clear what the maximum value of answer should be.
You would also perhaps encounter issues, if you tried to port this code to a 32-bit machine.
Good practices for defining the following:
Explicit type casting:
Another way of telling the compiler to do what you want it to:
If you had the number 0xFFFF. Depending on whether it is signed or unsigned, this could have different meaning to your compiler. So if your intention was for it to be unsigned, you can explicitly tell the compiler to treat it that way, by doing the following:
Notice the 'U'. This tells the compiler it is unsigned.
I think these are good practices, that should be kept in mind while writing macros. It forces you to think about your intention for the constant's usage. Also habits like these developed early on, definitely go a long way in your career.
在
#define
语句中放置分号没有任何问题。它只是成为宏的一部分。你定义了所以,你的 main 函数就变成这样了:
虽然放置分号没有什么问题,但在这种情况下你可能想要的是删除分号,然后语句就会变成你想要的。
There is nothing wrong with placing a semicolon in a
#define
statement. It simply becomes part of the macro. You definedSo, your main function becomes like this:
While there is nothing wrong with placing a semicolon, in this case what you probably want is to remove the semicolon, and then the statement will become what you want.