C语言宏定义-跟踪调试宏

本文详细介绍了C语言中预定义宏的使用,如__LINE__,__FILE__,__func__,__DATE__,__TIME__等,并通过实用宏定义CMO_STRFILE, CMO_STRFUNC等实例演示如何在代码中追踪和调试。适合开发者理解宏在调试过程中的作用。
摘要由CSDN通过智能技术生成

C语言宏定义-跟踪调试宏

1. 简介

标准C语言预处理要求定义某些对象宏,每个预定义宏的名称一两个下划线字符开头和结尾,这些预定义宏不能被取消定义(#undef)或由编程人员重新定义。

__LINE__当前代码行的行号,表示为十进制整型常量, 注:#line可以重新设定__LINE__的值
__FILE__当前代码的文件名(全路径),表示字符串型常量
__func__ or __FUNCTION__当前代码所在的函数名,表示字符串常量
__DATE__转换的日历日期,表示为Mmm dd yyyy 形式的字符串常量,Mmm是由asctime产生的
__TIME__转换的时间,表示"hh:mm:ss"形式的字符串型常量,是有asctime产生的
__cplusplus指示c++编译器

2. 相关宏

/**
 * @def CMO_STRFILE
 * @brief Provide a string identifying the current code filename.
 */
#define CMO_STRFILE ((const char *)(__FILE__))

/**
 * @def CMO_STRFUNC
 * @brief Provide a string identifying the current function.
 */
#define CMO_STRFUNC ((const char *)(__FUNCTION__))

/**
 * @def CMO_INTLINE
 * @brief Provide a int identifying the current code filename.
 */
#define CMO_INTLINE ((int)(__LINE__))

/**
 * @def CMO_STRLINE
 * @brief Provide a string identifying the current code line.
 */
#define CMO_STRLINE CMO_STRINGIFY(__LINE__)

/**
 * @def CMO_STRLOC
 * @brief Provide a string identifying the current code position.
 */
#define CMO_STRLOC __FILE__ ":" CMO_STRINGIFY(__LINE__)

/**
 * @def CMO_STRDATETIME
 * @brief Provide a string identifying the current build date time.
 */
#define CMO_STRDATETIME __DATE__ "T" __TIME__

3. 例子

#include <stdio.h>

/**
 * @def CMO_STRINGIFY
 * @brief converts a macro argument into a character constant.
 *
 * @param[in] x - A macro argument to stringify.
 */
#define CMO_STRINGIFY(x) CMO_STRINGIFY_(x)
/** Auxiliary macro used by @ref CMO_STRINGIFY */
#define CMO_STRINGIFY_(x) #x

/**
 * @def CMO_STRFILE
 * @brief Provide a string identifying the current code filename.
 */
#define CMO_STRFILE ((const char *)(__FILE__))

/**
 * @def CMO_STRFUNC
 * @brief Provide a string identifying the current function.
 */
#define CMO_STRFUNC ((const char *)(__FUNCTION__))

/**
 * @def CMO_INTLINE
 * @brief Provide a int identifying the current code filename.
 */
#define CMO_INTLINE ((int)(__LINE__))

/**
 * @def CMO_STRLINE
 * @brief Provide a string identifying the current code line.
 */
#define CMO_STRLINE CMO_STRINGIFY(__LINE__)

/**
 * @def CMO_STRLOC
 * @brief Provide a string identifying the current code position.
 */
#define CMO_STRLOC __FILE__ ":" CMO_STRINGIFY(__LINE__)

/**
 * @def CMO_STRDATETIME
 * @brief Provide a string identifying the current build date time.
 */
#define CMO_STRDATETIME __DATE__ "T" __TIME__

int main (void)
{
    printf("%-16.16s: %s\n", "CMO_STRFILE", CMO_STRFILE);
    printf("%-16.16s: %s\n", "CMO_STRFUNC", CMO_STRFUNC);
    printf("%-16.16s: %d\n", "CMO_INTLINE", CMO_INTLINE);
    printf("%-16.16s: %s\n", "CMO_STRLINE", CMO_STRLINE);
    printf("%-16.16s: %s\n", "CMO_STRLOC", CMO_STRLOC);
    printf("%-16.16s: %s\n", "CMO_STRDATETIME", CMO_STRDATETIME);

    return (0);
}
// gcc -o test test.c && ./test

运行结果:

image-20201114181943055

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值