《C语言 —— 封装printf》

1.封装printf

#define DEBUG       1
#ifdef DEBUG
 
    #define LOG(format,...) printf("FILE: "__FILE__",Func: %s, Line: %d\nmsg: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)

    #define ERROR(format,...)                                                       \
    do{                                                                                \
        fprintf(stderr, "[ERROR  at:]%s  func: %s  line: %d\nerror:"format"\n",     \
                             __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);      \
    }while(0)  

    #define PrintArray1D(arr, m)           \
    do                                     \
    {                                      \
        if (m > 0) {                       \
            int  i = 0;                    \
            printf("arr:");                \
            for (i = 0; i < m; i++) {      \
               if(arr[i] == '\0')          \
                   printf(" ");            \
               else                        \
                   printf("%c ", arr[i]);  \
            }                              \
            printf("\n");                  \
        }                                  \
    }while(0) 
    
    #define PrintArray2D(arr, m, n)     \
    do                                  \
    {                                   \
        int  i = 0;                     \
        int  j = 0;                     \
        for(i=0;i<m;i++)                \
        {                               \
          for(j=0;j<n;j++)              \
          {                             \
           printf("%f  ",arr[i][j]);    \
          }                             \
           printf("\n");                \
        }                               \
    }while(0)
 
#else
    #define LOG(format,...)             NULL 
    #define ERROR(format,...)           NULL 
    #define PrintArray1D(arr,m)         NULL
    #define PrintArray2D(arr,m,n)       NULL                     
#endif

 

posted @ 2023-02-08 14:30  一个不知道干嘛的小萌新  阅读(279)  评论(0编辑  收藏  举报