3.预处理与宏

2022-11-29  本文已影响0人  billgege

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;
}
上一篇 下一篇

猜你喜欢

热点阅读