目录

零基础 C/C++ 学习路线推荐 : ​​C/C++ 学习目录​​ >> ​​C 语言基础入门​

一.##运算符简介

在 ​​C 语言​​关于 ​​define​​ 的文章中我们有介绍到:凡是以#开头的均为预处理指令,预处理又叫预编译。预编译不是编译,而是编译前的处理。这个操作是在正式编译之前由系统自动完成的。

​C​​ 语言除了​​​#​​运算符​,还有 ​##运算符 ;与 ​​#​​运算符类似,​​##​​运算符可用于类函数宏(带参宏)的替换部分。​​​##​​运算符可以把两个记号组合成一个记号。例如,可以这样做:

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 ##运算符
//@Time:2021/07/10 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

#define XVALUE(n) x##n //将符号 x 和 n 合并为一个记号
#define YVALUE(n) y##n //将符号 y 和 n 合并为一个记号
#define ZVALUE(n) z##n //将符号 z 和 n 合并为一个记号

int XVALUE(1) = 10; //等价 int x1 = 10
int YVALUE(1) = 10; //等价 int y1 = 10
int ZVALUE(1) = 10; //等价 int z1 = 10


二.##运算符使用

1.##运算符常规使用

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 ##运算符
//@Time:2021/07/10 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

#include<stdio.h>

#define XVALUE(n) x##n //将符号 x 和 n 合并为一个记号
#define YVALUE(n) y##n //将符号 y 和 n 合并为一个记号
#define ZVALUE(n) z##n //将符号 z 和 n 合并为一个记号

#define NUM(a,b,c) a##b##c

int main()
{

int XVALUE(1) = 10; //等价 int x1 = 10
int YVALUE(1) = 10; //等价 int y1 = 10
int ZVALUE(1) = 10; //等价 int z1 = 10

printf("XVALUE(1) = %d\n",XVALUE(1));
printf("NUM(1,2,3) = %d\n",NUM(1,2,3));

return 0;
}

/*
XVALUE(1) = 10
NUM(1,2,3) = 123
*/


2.##运算符定义结构体

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 ##运算符
//@Time:2021/07/10 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

#include <stdio.h>

#define STRUCT(type) typedef struct _tag_##type type;\
struct _tag_##type

STRUCT(Student)
{
char* name;
int id;
};

/*
//STRUCT(Student) 等价:

typedef struct _tag_Student Student;

struct _tag_Student
{
char* name;
int id;
};

*/

int main()
{

Student s1;
Student s2;

s1.name = "s1";
s1.id = 0;

s2.name = "s2";
s2.id = 1;

printf("Student s1 name: %s\n", s1.name);
printf("Student s1 id: %d\n", s1.id);
printf("Student s2 name: %s\n", s2.name);
printf("Student s2 id: %d\n", s2.id);

return 0;
}

/*
Student s1 name: s1
Student s1 id: 0
Student s2 name: s2
Student s2 id: 1
*/


C 语言可以先 ​[typedef](https://www.codersrc.com/archives/8854.html)​ 然后在定义,只要在某处最终把 ​struct​ 定义出来后,无论在任何地方都可以重新 ​typedef​ ,并不是必须先定义 ​struct​​typedef​;

三.猜你喜欢

  1. ​C 语言 数组下标越界和内存溢出区别​
  2. ​C 语言 指针声明和定义​
  3. ​C 语言 指针 p++ / p–​
  4. ​C 语言 p++/§++/_(p++)/_p++​
  5. ​C 语言 使用指针遍历数组​
  6. ​C 语言 指针和数组区别​
  7. ​C 语言 指针数组和数组指针区别​
  8. ​C 语言 空指针 NULL​
  9. ​C 语言 void 指针​
  10. ​C 语言 野指针​
  11. ​C 语言 函数值传递和址传递​
  12. ​C 语言 函数缺省参数​
  13. ​C 语言 函数不定长参数​
  14. ​C 语言 函数指针​
  15. ​C 语言 指针函数​
  16. ​C 语言 回调函数 callback​
  17. ​C 语言 typedef​
  18. ​C 语言 define 定义常量​
  19. ​C 语言 define 防止头文件重复包含​
  20. ​C 语言 #pragma once​
  21. ​C 语言 #include <> 与 #include “” 区别​
  22. ​C 语言 const 修饰变量​
  23. ​C 语言 const 修饰指针​
  24. C​​语言 const 修饰函数​
  25. ​C 语言 const 修饰函数参数​
  26. ​C 语言 const 和 define 区别​
  27. ​C 语言 #运算符​
  28. ​C 语言 ##运算符​