C 预处理器和 if-else 条件语句

发布于 2024-09-06 04:47:29 字数 1300 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜光 2024-09-13 04:47:29

编译错误是由于 #define 语句中的分号造成的。删除它们。

#define 是一个预处理器宏,而不是 C 语法。它不需要分号。预处理器对 YESNO 进行直接替换,这使得:

if ( number % 2 == 0)
    answer = YES;
else
    answer = NO;

变成:

if ( number % 2 == 0)
    answer = 1;;  // <-- Notice the two semicolons!
else
    answer = 0;;

在 if 和 else 之间生成两个语句,因此会出现编译器错误。我怀疑当您添加 {} 时,您会遇到不同的编译器错误,因为

if (isEven(17) == YES)

成为

if (isEven(17) == 1;)

顺便说一句,这个问题被标记为 c,但是您的文件名是.cpp,这是c++的常见后缀。如果您使用 C++,请务必使用 bool 类型。

bool is_even = true;
bool is_odd = false;

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 on YES and NO, which makes:

if ( number % 2 == 0)
    answer = YES;
else
    answer = NO;

Turn into:

if ( number % 2 == 0)
    answer = 1;;  // <-- Notice the two semicolons!
else
    answer = 0;;

That makes two statements between if and else, so compiler errors ensue. I suspect you get different compiler errors when you add { and } due to

if (isEven(17) == YES)

becoming

if (isEven(17) == 1;)

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 the bool type.

bool is_even = true;
bool is_odd = false;
揪着可爱 2024-09-13 04:47:29

首先,删除定义末尾的分号

#define     YES     1
#define     NO      0

to start with, remove the semicolons from the end of the defines

#define     YES     1
#define     NO      0
岁月流歌 2024-09-13 04:47:29

正如其他用户所提到的,删除分号应该可以解决您手头的问题。
然而,我认为指出一些在编写宏时应该注意的事情可能会很方便。

虽然有时很方便,但最好了解在 C 中使用宏的陷阱。

1) 这里没有显式类型转换。

即“NO”可以是 uint16_t 或 uint32_t。

您在上面的示例中将变量定义为 int。如果我们假设您使用的是 16 位机器,则您的变量答案是 16 位长。但是您还没有为 NO 定义类型。在这种情况下,这不是什么大问题,因为 NO 的值很小。但是,如果您将其与“答案”进行比较,那么应该清楚答案的最大值应该是多少。
如果您尝试将此代码移植到 32 位计算机,您也可能会遇到问题。

  • 类型转换将帮助您实现可预测的代码结果,并在编写代码时清楚地表明您的意图。

定义以下内容的良好实践:

#define     YES     1  
#define     NO      0

显式类型转换:

#define     YES  (uint16_t)   1  
#define     NO   (uint16_t)   !(YES)  

告诉编译器执行您想要的操作的另一种方法:
如果你有数字 0xFFFF。根据它是有符号还是无符号,这对您的编译器可能有不同的含义。因此,如果您的意图是使其不带符号,则可以通过执行以下操作显式告诉编译器以这种方式对待它:

#define MY_LARGE_CONSTANT    0xFFFFU

注意“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.

  • Type Casting will help you achieve a predictable outcome of your code, and also clearly indicate your intention while writing the code.

Good practices for defining the following:

#define     YES     1  
#define     NO      0

Explicit type casting:

#define     YES  (uint16_t)   1  
#define     NO   (uint16_t)   !(YES)  

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:

#define MY_LARGE_CONSTANT    0xFFFFU

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.

始终不够爱げ你 2024-09-13 04:47:29

#define 语句中放置分号没有任何问题。它只是成为宏的一部分。你定义了

#define     YES     1;

所以,你的 main 函数就变成这样了:

int main()
{
    int isEven(int number);

    if (isEven(17) == 1;)
        printf("yes "); 
    else
        printf("no ");


    if ( isEven(20) == 1;)
        printf("yes\n");
    else
        printf("no\n");

     return 0;
}

虽然放置分号没有什么问题,但在这种情况下你可能想要的是删除分号,然后语句就会变成你想要的。

There is nothing wrong with placing a semicolon in a #define statement. It simply becomes part of the macro. You defined

#define     YES     1;

So, your main function becomes like this:

int main()
{
    int isEven(int number);

    if (isEven(17) == 1;)
        printf("yes "); 
    else
        printf("no ");


    if ( isEven(20) == 1;)
        printf("yes\n");
    else
        printf("no\n");

     return 0;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文