首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

_cplusplus和_STDC_在一个编译器里不会被同时定义吗?解决方案

2012-03-19 
__cplusplus和__STDC__在一个编译器里不会被同时定义吗?c++primer里好像提到过可是我写了#ifdef__cplusplu

__cplusplus和__STDC__在一个编译器里不会被同时定义吗?
c++   primer   里好像提到过

可是我写了
   
        #ifdef   __cplusplus
        cout   < <   "This   is   a   c++   compiler. "   < <   endl;
        #endif

        #ifdef   __STDC__
        cout   < <   "This   is   a   c   compileer. "   < <endl;
        #endif

结果2句都打出来了,怎么回事啊?

[解决办法]
__cplusplus用来表示是否C++编译器,而__STDC__用来表示是否支持ANSI C,一些C++编译器中预先定义了__STDC__,但是处值设置为0。(和编译器相关,我试了一下,在VC2003中没有预订义__STDC__)

#ifdef __cplusplus
int printf(char*,...);// C++ declaration
#else
int printf();/* C declaration */
#endif

K&R C, ANSI C, and C++ require different header files. To make C++ header files conform to K&R C and ANSI C standards so that they are generic, use the macro _ _cplusplus to separate C++ code from C code. The macro __STDC__ is defined in both ANSI C and C++. Use this macro to separate C++ or ANSI C code from K&R C code.

[解决办法]
两个#define一个表示支持c++,一个表示支持standard c,当然可以同时出现了。
比如 Dev-c++

热点排行