the macro of watch variable value and its memory content

来源:互联网 发布:手机淘宝取消合并付款 编辑:程序博客网 时间:2024/05/22 11:36

//replace the printf() with your log function


#include "stdafx.h"
#include <stddef.h>

typedef int INT;
typedef unsigned char BYTE;
typedef short SHORT;

#define WatchVarVal(fmt, Var){\
printf(#Var##" value: "##fmt##"\n", Var);\
}

// the local variable _i must be different  from the input argument Var in this macro,otherwise error will occur.

#define WatchVarMem(Var){ \
int _i; \
printf(#Var##" addr: 0x%x"##", size: %d bytes\n", &Var, sizeof(Var));\
for(_i = 0; _i < sizeof(Var); _i++)\
printf("Byte[%d]:0x%x\n", _i, *((unsigned char *)&Var + _i));\
}

int _tmain(int argc, _TCHAR* argv[])
{
INT iVar = 1023;
double dblVar = 2.34567;

WatchVarVal("%d", iVar);
WatchVarMem(iVar);

WatchVarVal("%f", dblVar);
WatchVarMem(dblVar);

getchar();
return 0;
}

0 0