“if”和“if”;哪一个最好使用[复制]

时间:2022-11-19 11:43:00

This question already has an answer here:

这个问题已经有了答案:

I learned that if or #if can both be used for condition checks. As we can check conditions using if, why would we use preprocessor #if?

我知道if或#if都可以用于条件检查。既然我们可以使用if检查条件,为什么要使用预处理器#if?

What difference will it make to my code if I use #if instead of if?

如果我使用#if而不是if,对代码有什么影响?

Which one is better to use and why?

哪个更好用,为什么用?

5 个解决方案

#1


30  

if and #if are different things with different purposes.

if和#if是具有不同目的的不同事物。

If you use the if statement, the condition is evaluated at runtime, and the code for both branches exists within the compiled program. The condition can be based on runtime information, such as the state of a variable. if is for the standard flow control in a program.

如果使用If语句,条件将在运行时进行计算,两个分支的代码都存在于编译后的程序中。条件可以基于运行时信息,比如变量的状态。if用于程序中的标准流控制。

If you use the preprocessor's #if, the condition is evaluated at compile-time, and the code for the false branch is not included in the compiled program. The condition can only be based on compile-time information (such as #define constants and the like). #if is for having different code for different compile-time environments (for instance, different code for compiling on Windows vs. *nix, that sort of thing).

如果您使用预处理器的# If,条件将在编译时进行计算,并且错误分支的代码不包含在已编译的程序中。条件只能基于编译时信息(如#define常量等)。#if用于为不同的编译时环境编写不同的代码(例如,在Windows vs. *nix上编译不同的代码,诸如此类)。

#2


12  

we could not say which better to use, because one is used in the compilation phase (#if) and the other one is used in the runtime phase(if)

我们不能说使用哪个更好,因为一个用于编译阶段(#if),另一个用于运行时阶段(if)

#if 1
   printf("this code will be built\n");
#else
   printf("this code will not\n");
#endif

try to build the above code with gcc -E and you will see that your compiler will generate another code containing only :

尝试使用gcc -E构建上面的代码,您将看到您的编译器将生成另一个只包含以下内容的代码:

printf("this code will be build\n");

the other printf will not be present in the new code (pre processor code) and then no present in the program binary.

另一个printf将不会出现在新代码(预处理器代码)中,也不会出现在程序二进制中。

Conclusion: the #if is treated in the compilation phase but the normal if is treated when your program run

结论:#if在编译阶段处理,而在程序运行时处理正常的if

You can use the #if 0 in a part of your code inorder to avoid the compiler to compile it. it's like you have commented this part

您可以在代码的一部分中使用#if 0,以避免编译器编译它。好像你已经评论了这部分

example

例子

int main(void) {

       printf("this code will be build\n");
#if 0
       printf("this code will not\n");
#endif

}

it's equivalent to

它等于

int main(void) {

       printf("this code will be built\n");
/*
       printf("this code will not\n");
*/

}

#3


6  

Hey both are different

嘿,都是不同的

  1. #if Tests if the condition is true at the compile time.
  2. #if测试条件在编译时是否为真。
  3. if is evaluated at runtime.
  4. 如果在运行时进行评估。

#4


4  

You should use #if when the outcome of the condition is known at compile time and regular if when outcome is not known until runtime.

当条件的结果在编译时已知时,应该使用#if;当结果直到运行时才知道时,应该使用#if。

#if DEBUG

I know at compile time I am making a debug build

我知道在编译时我正在进行调试构建

if (date == DateTime.Today)

Depends on what day it is

这要看今天是哪一天。

#5


2  

Some uses of #if are:

#if的一些用法是:

  • You want to put extra prints, or checks when you build a debug version of your code
  • 您希望在构建代码的调试版本时添加额外的打印或检查
  • you want to ensure the compiler doesn't include a .h file twice
  • 您希望确保编译器不会包含两次.h文件
  • you want to write code that will use different system calls, and depending on the system it gets compiled on use the appropriate ones.
  • 您希望编写使用不同系统调用的代码,并根据使用适当的系统对其进行编译。

Because all of the above are checked at compile time this means that:

因为所有这些都是在编译时检查的,这意味着:

  • The condition must be able to be evaluated at compiletime
  • 条件必须能够在编译时进行评估
  • The produced code will not contain the branches that evaluate to false, leading to smaller code, and faster, as the condition is not checked every time the program is run.
  • 生成的代码将不包含计算为false的分支,从而导致更小的代码和更快的速度,因为条件不会在每次运行程序时被检查。

Examples:

例子:

Adding extra checks only for debug mode:

仅为调试模式添加额外检查:

#define DEBUGLEVEL 2

#if DEBUGLEVEL > 1
   printf("The value of x is: %d", x);
#end if

#if DEBUGLEVEL > 2
   printf("The address of x is: %x", &x);
   ASSERT(x > 100);
#end if

Ensuring header only gets included once:

确保标题只包含一次:

#ifndef PERSON_H
#define PERSON_H
   class Person{
        ....
   };
#end if

Having different code depending on platform:

根据平台有不同的代码:

#ifdef WINDOWS
   time = QueryPerformanceCounter(..);
#else
   time = gettimeofday(..);
#endif

#1


30  

if and #if are different things with different purposes.

if和#if是具有不同目的的不同事物。

If you use the if statement, the condition is evaluated at runtime, and the code for both branches exists within the compiled program. The condition can be based on runtime information, such as the state of a variable. if is for the standard flow control in a program.

如果使用If语句,条件将在运行时进行计算,两个分支的代码都存在于编译后的程序中。条件可以基于运行时信息,比如变量的状态。if用于程序中的标准流控制。

If you use the preprocessor's #if, the condition is evaluated at compile-time, and the code for the false branch is not included in the compiled program. The condition can only be based on compile-time information (such as #define constants and the like). #if is for having different code for different compile-time environments (for instance, different code for compiling on Windows vs. *nix, that sort of thing).

如果您使用预处理器的# If,条件将在编译时进行计算,并且错误分支的代码不包含在已编译的程序中。条件只能基于编译时信息(如#define常量等)。#if用于为不同的编译时环境编写不同的代码(例如,在Windows vs. *nix上编译不同的代码,诸如此类)。

#2


12  

we could not say which better to use, because one is used in the compilation phase (#if) and the other one is used in the runtime phase(if)

我们不能说使用哪个更好,因为一个用于编译阶段(#if),另一个用于运行时阶段(if)

#if 1
   printf("this code will be built\n");
#else
   printf("this code will not\n");
#endif

try to build the above code with gcc -E and you will see that your compiler will generate another code containing only :

尝试使用gcc -E构建上面的代码,您将看到您的编译器将生成另一个只包含以下内容的代码:

printf("this code will be build\n");

the other printf will not be present in the new code (pre processor code) and then no present in the program binary.

另一个printf将不会出现在新代码(预处理器代码)中,也不会出现在程序二进制中。

Conclusion: the #if is treated in the compilation phase but the normal if is treated when your program run

结论:#if在编译阶段处理,而在程序运行时处理正常的if

You can use the #if 0 in a part of your code inorder to avoid the compiler to compile it. it's like you have commented this part

您可以在代码的一部分中使用#if 0,以避免编译器编译它。好像你已经评论了这部分

example

例子

int main(void) {

       printf("this code will be build\n");
#if 0
       printf("this code will not\n");
#endif

}

it's equivalent to

它等于

int main(void) {

       printf("this code will be built\n");
/*
       printf("this code will not\n");
*/

}

#3


6  

Hey both are different

嘿,都是不同的

  1. #if Tests if the condition is true at the compile time.
  2. #if测试条件在编译时是否为真。
  3. if is evaluated at runtime.
  4. 如果在运行时进行评估。

#4


4  

You should use #if when the outcome of the condition is known at compile time and regular if when outcome is not known until runtime.

当条件的结果在编译时已知时,应该使用#if;当结果直到运行时才知道时,应该使用#if。

#if DEBUG

I know at compile time I am making a debug build

我知道在编译时我正在进行调试构建

if (date == DateTime.Today)

Depends on what day it is

这要看今天是哪一天。

#5


2  

Some uses of #if are:

#if的一些用法是:

  • You want to put extra prints, or checks when you build a debug version of your code
  • 您希望在构建代码的调试版本时添加额外的打印或检查
  • you want to ensure the compiler doesn't include a .h file twice
  • 您希望确保编译器不会包含两次.h文件
  • you want to write code that will use different system calls, and depending on the system it gets compiled on use the appropriate ones.
  • 您希望编写使用不同系统调用的代码,并根据使用适当的系统对其进行编译。

Because all of the above are checked at compile time this means that:

因为所有这些都是在编译时检查的,这意味着:

  • The condition must be able to be evaluated at compiletime
  • 条件必须能够在编译时进行评估
  • The produced code will not contain the branches that evaluate to false, leading to smaller code, and faster, as the condition is not checked every time the program is run.
  • 生成的代码将不包含计算为false的分支,从而导致更小的代码和更快的速度,因为条件不会在每次运行程序时被检查。

Examples:

例子:

Adding extra checks only for debug mode:

仅为调试模式添加额外检查:

#define DEBUGLEVEL 2

#if DEBUGLEVEL > 1
   printf("The value of x is: %d", x);
#end if

#if DEBUGLEVEL > 2
   printf("The address of x is: %x", &x);
   ASSERT(x > 100);
#end if

Ensuring header only gets included once:

确保标题只包含一次:

#ifndef PERSON_H
#define PERSON_H
   class Person{
        ....
   };
#end if

Having different code depending on platform:

根据平台有不同的代码:

#ifdef WINDOWS
   time = QueryPerformanceCounter(..);
#else
   time = gettimeofday(..);
#endif