if else 和#if #else #endif 的区别
The differences between if else and #if #else #endif
我对 if
/else
和 #if
/#else
/#endif
结构感到困惑。
- 它们有什么区别?
- 我应该在哪些特定情况下使用它们?
if(...)
和 else(...)
条件在运行时计算。 #if
、#else
由预处理器在编译时间之前计算。
Can I ask what's the differences between them?
#if
#else
和 #endif
是给编译器的指令,只编译它们之间的代码,如果一个编译级条件(比如一个宏被定义或有一个特定的值)得到满足。
if
和 else
是编译算法的一部分。
What kind of specific situations for me to choose each of them?
预编译条件用于禁用部分代码,在它们没有意义的情况下(例如在 Linux 下编译时调用特定于 Windows 的 API)。它们是开发跨平台代码的关键(例如)。
I am confused about the if/else and #if/#else/#endif. It seems that
they have the same logic functionality.
Can I ask what's the differences between them?
#if
、#else
和#endif
属于预处理。它们 未执行 而是用于文本替换的说明。您可以将它们视为您通常在文本编辑器中找到的一种自动 "search & replace" 功能。
if
和 else
是 运行 时间结构。您可以将它们视为在程序 运行s.
时执行
假设您有这个程序:
#include <iostream>
#define VALUE 1
int main()
{
#if VALUE == 1
std::cout << "one\n";
#else
std::cout << "not one\n";
#endif
}
当您告诉编译器编译此程序时,预处理器将在实际编译 "real" C++ 代码之前进行文本替换。就好像程序是:
#include <iostream>
int main()
{
std::cout << "one\n";
}
现在,从技术上讲,您可以在此处使用 if
:
#include <iostream>
#define VALUE 1
int main()
{
if (VALUE == 1)
{
std::cout << "one\n";
}
else
{
std::cout << "not one\n";
}
}
但是在 C++ 中,您 不要将 #define
用于常量 。你会得到类似的东西:
#include <iostream>
int const value = 1;
int main()
{
if (value == 1)
{
std::cout << "one\n";
}
else
{
std::cout << "not one\n";
}
}
也许这个值只有在程序执行时才知道,例如通过用户输入。那么你显然不能使用 #if
,它只能在程序 运行 之前起作用。您必须使用 if
:
#include <iostream>
int main()
{
int value;
std::cin >> value; // gross simplification here
if (value == 1)
{
std::cout << "one\n";
}
else
{
std::cout << "not one\n";
}
}
What kind of specific situations for me to choose each of them?
初学者的一个很好的指导方针是:仅对 include guards 使用 #if
(或实际上:#ifndef
)。当遇到只能由预处理器解决的问题时,请考虑进一步使用 #if
。
我对 if
/else
和 #if
/#else
/#endif
结构感到困惑。
- 它们有什么区别?
- 我应该在哪些特定情况下使用它们?
if(...)
和 else(...)
条件在运行时计算。 #if
、#else
由预处理器在编译时间之前计算。
Can I ask what's the differences between them?
#if
#else
和 #endif
是给编译器的指令,只编译它们之间的代码,如果一个编译级条件(比如一个宏被定义或有一个特定的值)得到满足。
if
和 else
是编译算法的一部分。
What kind of specific situations for me to choose each of them?
预编译条件用于禁用部分代码,在它们没有意义的情况下(例如在 Linux 下编译时调用特定于 Windows 的 API)。它们是开发跨平台代码的关键(例如)。
I am confused about the if/else and #if/#else/#endif. It seems that they have the same logic functionality.
Can I ask what's the differences between them?
#if
、#else
和#endif
属于预处理。它们 未执行 而是用于文本替换的说明。您可以将它们视为您通常在文本编辑器中找到的一种自动 "search & replace" 功能。
if
和 else
是 运行 时间结构。您可以将它们视为在程序 运行s.
假设您有这个程序:
#include <iostream>
#define VALUE 1
int main()
{
#if VALUE == 1
std::cout << "one\n";
#else
std::cout << "not one\n";
#endif
}
当您告诉编译器编译此程序时,预处理器将在实际编译 "real" C++ 代码之前进行文本替换。就好像程序是:
#include <iostream>
int main()
{
std::cout << "one\n";
}
现在,从技术上讲,您可以在此处使用 if
:
#include <iostream>
#define VALUE 1
int main()
{
if (VALUE == 1)
{
std::cout << "one\n";
}
else
{
std::cout << "not one\n";
}
}
但是在 C++ 中,您 不要将 #define
用于常量 。你会得到类似的东西:
#include <iostream>
int const value = 1;
int main()
{
if (value == 1)
{
std::cout << "one\n";
}
else
{
std::cout << "not one\n";
}
}
也许这个值只有在程序执行时才知道,例如通过用户输入。那么你显然不能使用 #if
,它只能在程序 运行 之前起作用。您必须使用 if
:
#include <iostream>
int main()
{
int value;
std::cin >> value; // gross simplification here
if (value == 1)
{
std::cout << "one\n";
}
else
{
std::cout << "not one\n";
}
}
What kind of specific situations for me to choose each of them?
初学者的一个很好的指导方针是:仅对 include guards 使用 #if
(或实际上:#ifndef
)。当遇到只能由预处理器解决的问题时,请考虑进一步使用 #if
。