现在的位置: 首页 > 综合 > 正文

C语言条件编译

2014年08月10日 ⁄ 综合 ⁄ 共 752字 ⁄ 字号 评论关闭

C语言中的预编译包含三种:1.宏定义2.文件包含3.条件编译,条件编译指的是满足一定条件下才进行编译,它有几种形式:

(1)#ifdef标识符

     //程序

#else

     //程序

#endif

  它的意义为如果定义了标识符,则执行程序段1,否则执行程序段2

或者用以下的形式

# ifdef
标识符

//程序

 #endif

# include <stdio.h>
# include <stdlib.h>

int main()
{
#ifdef DEBUG
	printf("debug is running\n");
#else
	printf("debug is not running\n");
#endif
    system("pause");
    return 0;
}

(2)

#ifndef
标识符

     //程序1

#else

     //程序2

#endif

它的含义是如果标识符没有被定义,则执行程序段1,否则执行程序段2

# include <stdio.h>
# include <stdlib.h>

int main()
{
#ifndef DEBUG
	printf("debug is not running\n");
#else
	printf("debug is  running\n");
#endif
    system("pause");
    return 0;
}

(3)#if表达式

     //程序1

#else

     //程序2

#endif

 它的意义为表达式的值为真时,就编译程序段1,表达式的值为假时,就编译程序段2

# include <stdio.h>
# include <stdlib.h>
# define HEX 1
int main()
{
	int i=10;

#if HEX==1
	printf("%x\n",i);
#else
	printf("%d\n",i);
#endif
	system("pause");
	return 0;
}

 

 

 

抱歉!评论已关闭.