zoukankan      html  css  js  c++  java
  • 预处理#

    一、预编译

      预编译又称预处理,是整个编译过程中最先做的工作,主要是做代码文本的替换工作。主要处理#开头的指令,如拷贝#include包含头文件中的内容,替换#define定义的宏,条件编译#if等。

    二、#和##的作用

      #和##都是运算符。#是把宏参数转化为字符串的运算符,##是把两个宏参数连接的字符串。

    例1:

    #include <stdio.h>
    #define STR(arg) #arg
    #define STR2(arg) arg

    void main()
    {
    printf("STR result:%s ",STR("OK"));
    printf("STR result:%s ",STR(OK));
    printf("STR2 result:%s ",STR2("OK"));
    }

    结果:

    STR result:"OK"
    STR result:OK
    STR2 result:OK

    例2:

    #include <stdio.h>
    #define ADL(a,b) (a##b)

    void main()
    {
    printf("ADL result:%d ",ADL(2,3));
    }

    结果:

    ADL result:23

    三、避免文件头被重复

    1、使用条件编译

    #ifndef _TE_H

    #define _TE_H

    ......

    #endif

    例:(错误)

    /////////////////////////////////////////////////////////////////////////////////////////////////////

    te.c:

    #include <stdio.h>
    #include "te.h"
    #include "te.h"

    extern int i;

    void main()
    {
    printf("%d ",i);
    }

    te.h:

    int i=3;

    结果:

    te.h:1: error: redefinition of 'i'
    te.h:1: note: previous definition of 'i' was here

    /////////////////////////////////////////////////////////////////////////////////////////////////////

    2、使用#pragma once

    例:(正确)

    /////////////////////////////////////////////////////////////////////////////////////////////////////

    te.c:

    #include <stdio.h>
    #include "te.h"
    #include "te.h"

    extern int i;

    void main()
    {
    printf("%d ",i);
    }

    te.h:

    #pragma once

    int i=3;

    结果:

    3

    /////////////////////////////////////////////////////////////////////////////////////////////////////

  • 相关阅读:
    VMware安装Centos7超详细过程(图文)
    linux中yum与rpm区别
    xshell连接本地虚拟机中的centos
    虚拟主机安装 CentOS 8 出现 “ pane is dead ” 故障解决方案
    python字符串前面添加(u,r,b)的功能
    from . import XXX
    我关注的博客
    并发与并行的区别
    GSAP学习(二)——载入
    GSAP学习(一)——什么是GSAP
  • 原文地址:https://www.cnblogs.com/Mr-Wenyan/p/7235309.html
Copyright © 2011-2022 走看看