MDGSF Software Engineer

[C/C++] define宏定义

2017-05-26

#ifndef DEFINE_HJ
#define DEFINE_HJ

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define Conn(x, y) x##y //connect x and y
#define ToChar(x) #@x //wrap x with single quote, return char
#define ToString(x) #x //wrap x with double quote, return string

#define MEM_B(x) (*((uint8_t*)(&x)))
#define MEM_W(x) (*((uint16_t*)(&x)))

#define ARR_SIZE(a) (( (sizeof(a)) / (sizeof(a[0])) ))

#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif

#endif
#include <stdio.h>
#include <iostream>
#include "define.h"

using namespace std;
int main()
{
    int n = Conn(123, 456);
    char * str = Conn("Hello", "World");

    printf("n = %d, str = %s\n", n, str); //n = 123456, str = HelloWorld

    char a = ToChar(1);
    printf("a = %c\n", a); //a = 1

    char * pc = ToString(123321);
    printf("pc = %s\n", pc); //pc = 123321

    int iNum = 0x123456;
    printf("%x %x\n", MEM_B(iNum), MEM_W(iNum)); //56 3456

    printf("%s %d %s %s %s\n", __FILE__, __LINE__, __FUNCTION__, __DATE__, __TIME__);

    return 0;
}

weixingongzhonghao

Comments

Content