c语言之利用#if #endif来进行注释或者运行不同的主函数

C语言不支持在注释中嵌入注释,此时可以利用#if #endf,举个例子:

#include<stdio.h>
#include<iostream>

#if(0)
int main() {
    char* b;
    b = (char*)"hello";
    printf("%c
", b[2]);
    system("pause");
    return 0;
}
#endif // 

#if(1)
int main() {
    printf("hello world
");
    system("pause");
    return 0;
}
#endif // 

其中,如果#if(0)里面的参数是0,那么被#if #endif包裹的代码就不会被编译器编译,也就可以在里面进行嵌入注释了。同时,这样带来的其中一个作用是,如果我们只想在一个cpp文件中运行多段代码,而其他的不需要的不用运行,就可以将不需要运行或者编译的代码用#if(0) #endif包裹起来。

原文地址:https://www.cnblogs.com/xiximayou/p/12121675.html