美文网首页
3.预处理与宏

3.预处理与宏

作者: billgege | 来源:发表于2022-11-29 11:07 被阅读0次

1.文件包含

//
// Created by billge on 2022/11/22.
//
#include <stdio.h>
/**
* C语言的编译过程:源代码---->预处理器
 *              ----->宏替换后源代码----->编译器
 *              ---->中间文件---->链接器------>可执行程序
*/

/**
* 文件包含 #include, 引入头文件就是为了引入外部我们需要使用的函数,外部变量的声明等
 *
 * 在Terminal 通过gcc编译:  gcc -E .\3.1.file_include.c -o 3.1.file_include.i
 *
 *  生成的这个3.1.file_include.i 文件,修改后缀名为3.1.file_include.i.c 是可以直接运行的
 *
*/

int main(){
  puts("Hello ,World!");
  printf("Hello world!!!");

  return 0;
}

2.自定义头文件

//
// Created by billge on 2022/11/22.
//

#include <stdio.h>
//使用双引号 #include "xxx.h"
//1.首先查找当前源文件所在路径
//2.查找工程的头文件搜索路径
//#include "include/factorial.h"

//查找工程的头文件搜索路径, <>方法不会先去查找源文件所在路径
#include <factorial.h>

int main(){
  printf("3!= %d\n", FactorialTest(3));//6


  return 0;
}

3.宏函数

//
// Created by billge on 2022/11/28.
//
/**
* 宏函数: 有参数的宏都称为宏函数
*/

#define MAX(a, b) a>b?a:b
#define MAX_OPTMIZATION(a, b) (a)>(b)?(a):(b)

//定义多行宏 :判断是否是十六进制的宏函数
#define IS_HEX_CHARACTER(ch) \
((ch) >= '0' && (ch) <= '9') || \
((ch) >= 'A' && (ch) <= 'F') || \
((ch) >= 'a' && (ch) <= 'f')


//宏的参数和返回值,是没有类型的概念
//宏函数会在调用处进行代码展开
//宏的参数有可能是一个表达式。只会源码替换,不会进行求值
//能不能编译通过,完全取决于,宏函数代码替换后,语法是否正确

#include <stdio.h>

int main() {

  int max = MAX(1, 3);//1>3?1:3

  int max_2 = MAX(1, MAX(3, 4));//1 > 3 > 4 ? 3 : 4 ? 1 : 3 > 4 ? 3 : 4
  printf("max_2: %d\n", max_2); //1, 所以通常定义宏函数,一定要加括号,不然会出现不是我们预期的结果

  //优化后的宏函数替换
  int max_3 = MAX_OPTMIZATION(1, MAX_OPTMIZATION(3, 4));//(1) > ((3) > (4) ? (3) : (4)) ? (1) : ((3) > (4) ? (3) : (4))
  printf("max_3: %d\n", max_3);//4


  printf("is A a hex character? %d\n", IS_HEX_CHARACTER('A'));
  //替换多行宏函数:(('A') >= '0' && ('A') <= '9') || (('A') >= 'A' && ('A') <= 'F') || (('A') >= 'a' && ('A') <= 'f')
  //打印结果是:is A a hex character? 1

  return 0;
}

4.条件编译

//
// Created by billge on 2022/11/28.
//

#include <stdio.h>

#ifdef  __cplusplus
extern "C"{
#endif
//考虑C和C++兼容性问题的宏
int Add(int left, int right);
#ifdef __cplusplus
};
#endif

/**
* 条件编译
 * 1.#ifdef 如果定义了
 * 2.#ifndef 如果没有定义
 * 3.#if 如果。。。
 *
 * #endif
 *
 * #if defined(MACRO) ==> #ifdef MACRO
 *
*/

//#define DEBUG  //定义调试开关, 如果不在这里定义,
//则需要在CMakeLists.txt 添加target_compile_definitions(${name} PUBLIC DEBUG),要注意空格

void dump(char *message) {
#ifdef DEBUG
  puts(message);
#endif
}

int main() {
  dump("main start");

  printf("Hello world!");

  dump("main end.");

  printf("  __STDC_VERSION__:%ld\n", __STDC_VERSION__);//打印C语言版本  __STDC_VERSION__:201112

  //如何做宏当中使用if,else
#if __STDC_VERSION__ >= 201112
  puts("C11!!");
#elif __STDC_VERSION__ >= 199901
  puts("C99!!");
#else
  puts("maybe C90?");
#endif




}

5.案例:实现PRINTLNF

//
// Created by billge on 2022/11/29.
//

/**
 * 案例:实现PRINTLNF
 *
 */


#include <stdio.h>
#include <stdarg.h>


void Printlnf(char const *const format, ...) {
  va_list args;
      va_start(args, format);
  vprintf(format, args);
  printf("\n");
      va_end(args);
}

//"Hello ""world"====>等价于  "Hello world";
//加两个##可以去掉逗号,
//__FILE__
//__LINE__
//__FUNCTION__   只有编译的时候才会有的
#define PRINTLNF(format,...) printf("("__FILE__":%d) %s : "format"\n",__LINE__,__FUNCTION__ ,##__VA_ARGS__)

//#value 等于value的字面量
#define PRINT_INT(value) PRINTLNF(#value": %d",value)

int main() {
  int value = 2;
  Printlnf("Hello world! %d",value);//Hello world! 2
  Printlnf("=======================");
  Printlnf("Hello world2! %d",value);

  PRINTLNF("***********************");//(C:\work_space_one\c_study\3.5.printlnf.c:31) main : ***********************
  PRINTLNF("macro define method, %d",value);//(C:\work_space_one\c_study\3.5.printlnf.c:32) main : macro define method, 2

  int x = 3;
  PRINT_INT(x); //(C:\work_space_one\c_study\3.5.printlnf.c:35) main : x: 3
  PRINT_INT(3+4);//(C:\work_space_one\c_study\3.5.printlnf.c:36) main : 3+4: 7


  return 0;
}

相关文章

  • 3.预处理与宏

    1.文件包含 2.自定义头文件 3.宏函数 4.条件编译 5.案例:实现PRINTLNF

  • const 关键字 - iOS

    一、const与宏的区别 处理时刻:宏是预处理时(即在预处理时把宏展开,替换为原来的值, 没有类型,不做类型检查)...

  • C语言预处理指令

    预处理指令 宏定义 宏定义会在预处理的时候, 用宏定义的值来替换宏的名称 格式: #define 宏名称 宏值 应...

  • 基础C语言知识串串香11☞宏定义与预处理、函数和函数库

    六、C语言宏定义与预处理、函数和函数库 6.1、编译工具链 源码.c->(预处理)->预处理过的.i文件->(编译...

  • 10/19

    今天老师讲了预处理命令,宏定义分为无参宏定义,带参宏定义和条件编译。宏定义包括宏名和宏展开,和函数相比预处理有很多...

  • iOS--宏定义

    预处理 C语言的预处理(预编译)主要有三个方面的内容: 1.宏定义; 2.文件包含; 3.条件编译。“#”是编译器...

  • Android NDK开发-静态库、动态库编译原理和语法详解

    编译流程 编译分为四大过程:1.预处理2.编译3.汇编4.链接 预处理 1.完成宏替换、文件引入,以及去除空行、注...

  • iOS常用宏定义大全

    宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时...

  • 10.19

    今天主要讲了预处理 主要是预处理的概念和预处理命令的使用 包括宏定义“文件包含”处理条件编译等等宏定义是老师重...

  • iOS预处理宏使用指南

    预处理宏使用指南 预处理就是对源文件进行编译前,先对预处理部分进行处理,然后对处理后的代码进行编译。 常用宏说明 ...

网友评论

      本文标题:3.预处理与宏

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