C++预处理和宏的使用详解

Test.cpp : 定义控制台应用程序的入口点。

 

1、看下面的代码并写出结果

 

考点:#ifdef#else#endif在程序中的使用。

 

2.宏定义的使用

 

考点:使用#define宏定义时需要注意的地方

 

3.代码

 

#include "stdafx.h"

#include<stdio.h>

#include<stdlib.h>

 

#define DEBUG//预处理器常量

#define SQR(x) (x*x)

 

#define STR(s)   #s

#define CONS(a,b) (int)(a##e##b)

 

int _tmain(int argc, _TCHAR* argv[])

{

//预处理

/*

int i = 0;

char c;

while(1)

{

i++;

c= getchar();

if(c != '\n')

{

getchar();

}

if(c == 'q' || c == 'Q')

{

#ifdef DEBUG//判断DEBUG是否被定义

printf("we got:%c,about to exit.\n",c);

#endif

break;

}

else

{

printf("i = %d",i);

#ifdef DEBUG

printf(", we got:%c",c);

#endif

printf("\n");

}

}

*/

//宏定义

/*

int a,b =3;

a = SQR(b + 2);//原本:a = (b+2)*(b+2)  #define SQR(x) (x*x)

//a = b + 2 * b + 2

printf("a = %d\n", a);

*/

 

4.运行结果:

 

 

 

 

 

原文链接:http://www.maiziedu.com/wiki/cplus/macro/

<!--EndFragment-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值