code
从K&R中改编过来
#include <stdio.h>
#include <ctype.h>
#ifndef CXXU
#define CXXU 1
#define dprint(expr) printf(#expr " = %d @%%d\n", expr)
#define gprint(expr) printf(#expr " = %g\n", expr)
#define fprint(expr) printf(#expr " = %f\n", expr)
#define cprint(expr) printf(#expr " = %c @%%c\n", expr)
#define sprint(expr) printf(#expr " = %s @%%s\n", expr)
#define sprintln(expr) printf(expr "\n")
#define pprint(expr) printf(#expr " = %p &%%p\n", &expr)
#define pprint_addr(expr) printf(#expr " = %p %%p\n", expr)
#endif
int getint(int *pn);
int main()
{
int integer = 0;
int res = getint(&integer);
dprint(res);
dprint(integer);
}
int getint(int *pn)
{
int c, sign;
while (isspace(c = getchar()))
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-')
{
ungetc(c, stdin);
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getchar();
for (*pn = 0; isdigit(c); c = getchar())
{
*pn = 10 * *pn + (c - '0');
}
*pn *= sign;
if (c != EOF)
ungetc(c, stdin);
sprintln();
sprintln("last readed char:(it probably a white space char,so will will print it with %%d");
dprint(c);
return c;
}
调试宏(打印宏)与对齐
预览效果
code
使用制表符对齐的效果不太稳定,使用指定下限宽度的方法可以使得长度差异较大的输出串依然可以对齐
#ifndef CXXU
#define CXXU 1
#define sizeint__ "25"
#define sizestr__ "%" sizeint__ "s"
#define dprint(expr) printf(sizestr__ " = %d @%%d\n", #expr, expr)
#define ldprint(expr) printf(sizestr__ " = %ld @%%ld\n", #expr, expr)
#define cprint(expr) printf(sizestr__ " = %c @%%c\n", #expr, expr)
#define sprint(expr) printf(sizestr__ " = %s @%%s\n", #expr, expr)
#define gprint(expr) printf(sizestr__ " = %g\n", #expr, expr)
#define fprint(expr) printf(sizestr__ " = %f\n", #expr, expr)
#define sprintln(expr) printf(expr "\n")
#define pprint(expr) printf(sizestr__ " = %p &var%%p\n", "&" #expr, &expr)
#define pprinta(expr) printf(sizestr__ " = %p %%p (pointer:" #expr ")\n", #expr, expr)
#endif
|