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

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

 

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

 

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

 

2.宏定义的使用

 

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

 

3.代码

扫描二维码关注公众号,回复: 567355 查看本文章

 

#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-->

猜你喜欢

转载自2789593579.iteye.com/blog/2330712