C语言宏和预处理

在这里插入图片描述

【1】include

#include本命令包含一个文件并在当前位置插入.一般尖括号意味着在标准库目录中搜索,
#include “filename” 引号就表示在当前目录中搜索.

【2】【#pragma】

#pragma命令可以让编程者让编译器执行某些事.
#pragma once //代表不论其他文件写了多少遍这个头 都只编译一次

学习自定义头文件

#ifndef __EXHEAD_H__
#define __EXHEAD_H__

int values=100;  //在这定义初始化
extern const char *buf="djfhj"; //初始化声明变量(变量的定义)
#endif

【3】C++ #include

整洁的新C++ #include目录不需要直接映射到filenames(没有.h), 至少对于标准库是这样

#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>


using namespace std;

【4】# 和 ## 操作符

# 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符串

#define to_string( s ) # s
//使用 ## 连结 ## 前后 的内容
#define concatenate( x, y )  x ## y

【5】宏替换

#define TRUE 1
#define FALSE 0


//#define命令的另外一个功能就是替换参数,使它 假冒创建函数一样使用. 如下的代码: 
#define absolute_value( x ) ( ((x) < 0) ? (x) : 2 )
    
#define DEBUG 100

【6】#line

更改行号为10,当前文件改为"Y预处理.cpp".

#line 10  "Y预处理.cpp"

【7】#error

当遇到一个#error时,编译器会自动输出行号而无论message的内容,本命令大多是用于调试.

#error  err_message   //终止错误 

【8】#undef

//以下操作相当于没有操作
#define MIN 19
#undef  MIN
//#undef命令取消一个先前已定义的宏变量, 譬如一个用#define定义的变量. 

_cplusplus 变量只在编译一个C++程序时定义.
STDC 变量在编译一个C程序时定义,编译C++时也有可能定义.

【9】源码

int main(int argc, char const *argv[])
{
    //参数宏函数
    int xy=100;
    cout << to_string( Hello World! ) << endl;
    cout << concatenate(x,y) << endl;


    int x = 1;
    cout<< absolute_value( x )<<endl;


    //传说中的宏判断
    #if TRUE 
        cout<<"真"<<endl;;
    #else
        cout<<"假"<<endl;
    #endif


    #ifdef DEBUG  //如果定义的情况
      cout << "This is the test version"<< endl;
    #else
      cout << "This is the production version!" << endl;
    #endif
    //如果没有定义  就定义
    #ifndef macro
        cout<<"macro"<<endl;
    #elif
        cout<<"xixi"<<endl;
    #endif
    //打印行号
    cout<<__LINE__<<endl;


    //打印当前目录 本文件名
    cout<<__FILE__<<endl;  
   
    cout<<__DATE__<<"  ";  //变量表示当前日期,格式为month/day/year(月/日/年). 


    cout<<__TIME__ <<endl; //变量描述当前的时间,格式为hour:minute:second(时:分:秒). 




    return 0;
}

【10】结果

打印
 PS C:\Users\Administrator\Desktop\C语言函数集合> g++ .\预处理.cpp
 PS C:\Users\Administrator\Desktop\C语言函数集合> ./a
 Hello World!
 100
 2
 真
 This is the test version
 macro
 83
 .\预处理.cpp
 Nov 29 2021  21:55:02
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qt历险记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值