美文网首页
C++预处理和宏的使用详解

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

作者: 随波逐流007 | 来源:发表于2016-10-14 18:25 被阅读15次

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

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

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

    2.宏定义的使用

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

    3.代码

    #include "stdafx.h"

    #include

    #include

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

    相关文章

      网友评论

          本文标题:C++预处理和宏的使用详解

          本文链接:https://www.haomeiwen.com/subject/gaqnyttx.html