c++ 常用的一些宏定义

#include<iostream>
#include <windows.h>
#include <string>
using namespace std;


#define Conn(x,y) x##y   //表示x连接y
#define tochar(x) #@x //给x加上单引号,结果返回是一个const char
#define tostring(x) #x  //给x加双引号 返回char const *

#define MEM_B(x)(*((byte*)(x))) //得到指定地址上的一个字节
#define MEM_W(x)(*((WORD*)(x))) //得到指定地址上的一个字  WORD 16-bit unsigned integer DWORD 32 - bit unsigned integer
#define OFFSETOF(type,field) ((size_t)& ((type*)0)->field)  //得到一个field在结构体(struct type)中的偏移量。
#define FSIZ(type,field) sizeof( ((type *) 0)->field )  // 得到一个结构体中field所占用的字节数 

//得到一个变量的地址(word宽度)
#define B_PTR( var ) ( (byte *) (void *) &(var) )  
#define W_PTR( var ) ( (WORD *) (void *) &(var) )

#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) ) //将一个字母转换为大写
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val)) //防止溢出的一个方法
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) ) //返回数组的个数


//ANSI标准说明了五个预定义的宏名。它们是:
#define _LINE_ __LINE__ /*(两个下划线),对应%d*/
//_FILE_ /*对应%s*/
//_DATE_ /*对应%s*/
//_TIME_ /*对应%s*/

struct Person 
{
   
	string name;
	int age;
};

void test01()
{
   
	cout << Conn(123, 444) << endl; //123444
	cout << Conn("abc", "efg") << endl; //abcefg


	auto a2 = tochar(4);
	cout << a2 << endl;  // a2='4'
	cout << typeid(a2).name() << endl;


	auto a3 = tostring(43434);
	cout << a3 << endl; //a3="43434"
	cout << typeid(a3).name() << endl;

	int bTest = 0x123456; //0001 0010 0011 0100 0101 0110
	byte m = MEM_B((&bTest));
	cout << int(m) << endl;//86 = 0101 0110  一个字节8bit

	int n = MEM_W((&bTest));/*n=0x3456*/
	cout << int(n) << endl;   //13398  = 0011 0100 0101 0110  一个WORD 16bit

	struct Person p = {
    "dsd",88 };
	cout << OFFSETOF(Person, name) << endl; //0
	cout << OFFSETOF(Person, age) << endl; //28
	cout << FSIZ(Person, age) << endl; //4
	cout << FSIZ(Person, name) << endl; //28

	int num = 97;
	cout << B_PTR(num) << endl;  // a 97
	cout << W_PTR(num) << endl;  //0019FD98

	cout << UPCASE('a') << endl; //65  = A
	int num1 = 99;
	int num2 = 99999999999999999;
	cout << INC_SAT(num1) << endl; //100
	cout << INC_SAT(num2) << endl; //1569325056

	int arr[] = {
    1,2,43,5 };
	int arr1[][4] =
	{
   
		{
   1, 2, 3, 5},
		{
    1,2,3,5 },
	};
	cout << ARR_SIZE(arr) << endl; //4
	cout << ARR_SIZE(arr1) << endl; //2

	int x = 0;
	int y = 10;
	if (y > x) {
   
		printf("Error: y is greater than x at line %d\n", _LINE_);
	}
}



int main()
{
   	
	test01();
	system("pause");
	return 0;
}

相关推荐

  1. c++ 常用一些定义

    2023-12-13 14:28:04       34 阅读
  2. 常用C语言定义

    2023-12-13 14:28:04       41 阅读
  3. iOS中常用一些以及用法

    2023-12-13 14:28:04       8 阅读
  4. C语言中定义:从常量到高级技巧

    2023-12-13 14:28:04       28 阅读
  5. C++中在定义一个时候要注意什么?

    2023-12-13 14:28:04       22 阅读
  6. C语言】定义详解与实践

    2023-12-13 14:28:04       23 阅读
  7. c++基础(6)>定义与函数区别

    2023-12-13 14:28:04       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-13 14:28:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-13 14:28:04       20 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-13 14:28:04       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-13 14:28:04       20 阅读

热门阅读

  1. uniapp中this有时打印的是undefined(作用域问题)

    2023-12-13 14:28:04       51 阅读
  2. assert 是一个断言语句

    2023-12-13 14:28:04       40 阅读
  3. 【DL-TensorFlow遇错】TensorFlow中遇错合集

    2023-12-13 14:28:04       43 阅读
  4. 创建链表时的一个小bug

    2023-12-13 14:28:04       37 阅读
  5. 网络协议的深入了解!

    2023-12-13 14:28:04       36 阅读
  6. 处理器中断的处理

    2023-12-13 14:28:04       33 阅读
  7. fmt用法

    2023-12-13 14:28:04       39 阅读
  8. [json]定义、读写

    2023-12-13 14:28:04       36 阅读
  9. 从零学算法49

    2023-12-13 14:28:04       39 阅读
  10. ssh无密码自动登录实现原理

    2023-12-13 14:28:04       38 阅读