Skip to content

Instantly share code, notes, and snippets.

@lnznt
Created April 28, 2012 06:11
Show Gist options
  • Save lnznt/2516478 to your computer and use it in GitHub Desktop.
Save lnznt/2516478 to your computer and use it in GitHub Desktop.
macro: put_d() (C language)
#ifndef INCLUDED_EZPUT_H
#define INCLUDED_EZPUT_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <stdio.h> /* printf() */
#include <ctype.h> /* isprint() */
/*
usage:
int val = 10;
char *message = "Hello";
int flag = 1;
// <stdout>
put_d(val); // val = 10
put_02x(val); // val = 0x0a
put_s(message); // message = "Hello"
put_bool(flag); // flag = true
*/
#define put_d(v_) (printf(#v_ " = %d\n", (v_)))
#define put_i(v_) (printf(#v_ " = %+i\n", (v_)))
#define put_u(v_) (printf(#v_ " = %u\n", (v_)))
#define put_o(v_) (printf(#v_ " = %#o\n", (v_)))
#define put_x(v_) (printf(#v_ " = %#x\n", (v_)))
#define put_hd(v_) (printf(#v_ " = %hd\n", (v_)))
#define put_hi(v_) (printf(#v_ " = %+hi\n", (v_)))
#define put_hu(v_) (printf(#v_ " = %hu\n", (v_)))
#define put_ho(v_) (printf(#v_ " = %#ho\n", (v_)))
#define put_hx(v_) (printf(#v_ " = %#hx\n", (v_)))
#define put_ld(v_) (printf(#v_ " = %ld\n", (v_)))
#define put_li(v_) (printf(#v_ " = %+li\n", (v_)))
#define put_lu(v_) (printf(#v_ " = %lu\n", (v_)))
#define put_lo(v_) (printf(#v_ " = %#lo\n", (v_)))
#define put_lx(v_) (printf(#v_ " = %#lx\n", (v_)))
#define put_02x(v_) (printf(#v_ " = 0x%02x\n", (v_)))
#define put_04x(v_) (printf(#v_ " = 0x%04x\n", (v_)))
#define put_08x(v_) (printf(#v_ " = 0x%08x\n", (v_)))
#define put_02hx(v_) (printf(#v_ " = 0x%02hx\n", (v_)))
#define put_04hx(v_) (printf(#v_ " = 0x%04hx\n", (v_)))
#define put_08hx(v_) (printf(#v_ " = 0x%08hx\n", (v_)))
#define put_02lx(v_) (printf(#v_ " = 0x%02lx\n", (v_)))
#define put_04lx(v_) (printf(#v_ " = 0x%04lx\n", (v_)))
#define put_08lx(v_) (printf(#v_ " = 0x%08lx\n", (v_)))
#define put_p(v_) (printf(#v_ " = (%p)\n", (v_)))
#define put_s(v_) (printf(#v_ " = %s%s%s\n" , \
(v_) ? "\"" : "" ,\
(v_) ? (v_) : "NULL" ,\
(v_) ? "\"" : "" ))
#define put_c(v_) (printf(#v_ " = %s%c%s[%#02x]\n" ,\
isprint(v_) ? "'" : " " ,\
isprint(v_) ? (v_) : '.' ,\
isprint(v_) ? "'" : " " ,\
(v_) & ((unsigned char)~0U)))
#define put_bool(v_) (printf(#v_ " = %s\n", (v_) ? "true" : "false"))
#define put_8bit(v_) (printf(#v_ " = %u%u%u%u:%u%u%u%u\n", \
((v_) >> 7) & 1U, \
((v_) >> 6) & 1U, \
((v_) >> 5) & 1U, \
((v_) >> 4) & 1U, \
((v_) >> 3) & 1U, \
((v_) >> 2) & 1U, \
((v_) >> 1) & 1U, \
(v_) & 1U ))
#define put_16bit(v_) (printf(#v_ " = %u%u%u%u:%u%u%u%u" \
" %u%u%u%u:%u%u%u%u\n", \
((v_) >> 15) & 1U, \
((v_) >> 14) & 1U, \
((v_) >> 13) & 1U, \
((v_) >> 12) & 1U, \
((v_) >> 11) & 1U, \
((v_) >> 10) & 1U, \
((v_) >> 9) & 1U, \
((v_) >> 8) & 1U, \
((v_) >> 7) & 1U, \
((v_) >> 6) & 1U, \
((v_) >> 5) & 1U, \
((v_) >> 4) & 1U, \
((v_) >> 3) & 1U, \
((v_) >> 2) & 1U, \
((v_) >> 1) & 1U, \
(v_) & 1U ))
#define put_32bit(v_) (printf(#v_ " = %u%u%u%u:%u%u%u%u" \
" %u%u%u%u:%u%u%u%u" \
" %u%u%u%u:%u%u%u%u" \
" %u%u%u%u:%u%u%u%u\n", \
((v_) >> 31) & 1U, \
((v_) >> 30) & 1U, \
((v_) >> 29) & 1U, \
((v_) >> 28) & 1U, \
((v_) >> 27) & 1U, \
((v_) >> 26) & 1U, \
((v_) >> 25) & 1U, \
((v_) >> 24) & 1U, \
((v_) >> 23) & 1U, \
((v_) >> 22) & 1U, \
((v_) >> 21) & 1U, \
((v_) >> 20) & 1U, \
((v_) >> 19) & 1U, \
((v_) >> 18) & 1U, \
((v_) >> 17) & 1U, \
((v_) >> 16) & 1U, \
((v_) >> 15) & 1U, \
((v_) >> 14) & 1U, \
((v_) >> 13) & 1U, \
((v_) >> 12) & 1U, \
((v_) >> 11) & 1U, \
((v_) >> 10) & 1U, \
((v_) >> 9) & 1U, \
((v_) >> 8) & 1U, \
((v_) >> 7) & 1U, \
((v_) >> 6) & 1U, \
((v_) >> 5) & 1U, \
((v_) >> 4) & 1U, \
((v_) >> 3) & 1U, \
((v_) >> 2) & 1U, \
((v_) >> 1) & 1U, \
(v_) & 1U ))
#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) || defined(__GNUC__)
#define put_func() (printf("%s()\n", __func__))
#else
#define put_func() (printf("function:\n"))
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* ! INCLUDED_EZPUT_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment