能得到FUNC谁自称为LINENO? (C / C ++) [英] Can func get the lineno who call itself? (C/C++)

查看:96
本文介绍了能得到FUNC谁自称为LINENO? (C / C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,因为下面的code discribe本身。结果

I've a problem , as the following code discribe itself.

1  #include<stdlib.h>
2  #include<stdio.h>
3  void log()
4  {
5         printf("Log [Line:%d]\n",__LINE__);
6  }
7  int main()
8  {
9         log();
10        log();
11 }

预期的结果是搜索
日志[行:9]结果
日志[行:10]结果
但是,事实是搜索
日志[行:5]结果
日志[行:5]结果

The expected result is
Log [Line:9]
Log [Line:10]
But, the fact is
Log [Line:5]
Log [Line:5]

没有意外,订单已在pre-加工阶段取代为5结果
我的问题是,如何设计日志功能来获得期望的结果呢?结果
谢谢!

No surprising, LINE has been substituted at the pre-process stage as 5.
My Question is, how to design the log function to get the expected result?
Thanks!

推荐答案

您需要编写一个宏:

#define LOG printf("Log [Line:%d]\n",__LINE__)

然后使用它:

int main() {
    LOG;
    LOG;
}

这工作,因为宏在其使用点扩大,给 __ __ LINE 宏正确的值。

This works because the macro is expanded at its point of use, giving the __LINE__ macro the correct value.

这篇关于能得到FUNC谁自称为LINENO? (C / C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆