输出

  1. // io_utils.h
  2. #ifndef BASICC_IO_UTILS_IO_UTILS_H_
  3. #define BASICC_IO_UTILS_IO_UTILS_H_
  4. #include <stdio.h>
  5. #include <limits.h>
  6. void PrintBinary(unsigned int value);
  7. //#define PRINT_METADATA
  8. #ifdef PRINT_METADATA
  9. # define PRINTLNF(format, ...) printf("("__FILE__":%d) %s: "format"\n", __LINE__, __FUNCTION__ , ##__VA_ARGS__)
  10. #else
  11. # define PRINTLNF(format, ...) printf(format"\n", ##__VA_ARGS__)
  12. #endif
  13. #define PRINT_CHAR(char_value) PRINTLNF(#char_value": %c", char_value)
  14. #define PRINT_WCHAR(char_value) PRINTLNF(#char_value": %lc", char_value)
  15. #define PRINT_INT(int_value) PRINTLNF(#int_value": %d", int_value)
  16. #define PRINT_LONG(long_value) PRINTLNF(#long_value": %ld", long_value)
  17. #define PRINT_LLONG(long_value) PRINTLNF(#long_value": %lld", long_value)
  18. #define PRINT_BINARY(int_value) PrintBinary((unsigned int) int_value);
  19. #define PRINT_HEX(int_value) PRINTLNF(#int_value": %#x", int_value)
  20. #define PRINT_BOOL(bool_value) PRINTLNF(#bool_value": %s", bool_value ? "true" : "false")
  21. #define PRINT_DOUBLE(double_value) PRINTLNF(#double_value": %g", double_value)
  22. #define PRINT_STRING(string_value) PRINTLNF(#string_value": %s", string_value)
  23. #define PRINT_ARRAY(format, array, length) \
  24. { int array_index; \
  25. for (array_index = 0; array_index < length; ++array_index) { \
  26. printf(format, array[array_index]); \
  27. };\
  28. printf("\n"); }
  29. #define PRINT_INT_ARRAY_LN(array, length) \
  30. { int i; \
  31. for (i = 0; i < length; ++i) { \
  32. PRINTLNF(#array"[%d]: %d", i, array[i]); \
  33. }}
  34. #define PRINT_INT_ARRAY(array, length) PRINT_ARRAY("%d, ", array, length)
  35. #define PRINT_CHAR_ARRAY(array, length) PRINT_ARRAY("%c, ", array, length)
  36. #define PRINT_DOUBLE_ARRAY(array, length) PRINT_ARRAY("%g, ", array, length)
  37. #endif //BASICC_IO_UTILS_IO_UTILS_H_