这是 __LINE__ 的正确行为吗?
15 ERROR_MACRO("Error is in %s on line %d\n",
16 __FILE__, __LINE__);
我得到以下输出:
错误位于 tmp.c 第 16 行
即使我以这种形式使用上面的行,我也得到相同的输出:
15 ERROR_MACRO("Error is in %s on line %d\n", \
16 __FILE__, __LINE__);
我不应该得到“第 15 行”而不是“第 16 行”吗?
我应该怎么做才能到达“15 号线”?
15 ERROR_MACRO("Error is in %s on line %d\n",
16 __FILE__, __LINE__);
I am getting following output:
Error is in tmp.c on line 16
I am getting same output, even if I use the above line in this form :
15 ERROR_MACRO("Error is in %s on line %d\n", \
16 __FILE__, __LINE__);
Shouldn't I get "line 15" instead of "line 16" ?
What should I do to get "line 15" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
__LINE__
始终扩展为其出现的确切行号。编译器如何报告跨多行代码的错误取决于编译器,但大多数编译器都会按照语句开始的行进行报告(因为大多数错误无法本地化为单个字符)。没有宏可以确定当前语句出现在哪一行,因为预处理通常发生在编译器甚至开始考虑语句之前。
__LINE__
always expands to the exact line number that it appears on. It's up to the compiler how it reports errors for code that spans multiple lines, but most compilers go by the line that the statement started on (since most errors cannot be localized to a single character).There is no macro which can determine what line the current statement appears on, as preprocessing typically occurs before the compiler even starts thinking about statements.
将 ERROR_MACRO 移至代码中的第 15 行?
__LINE__
是当前文件中的行号。除非您移动代码,否则没有(合法)方法可以更改它......move the ERROR_MACRO to line 15 in your code?
__LINE__
is the line number within the current file. There is no (legal) way to change it unless you move your code...如果您坚持在调用后的行上使用
__LINE__
,那么只需执行以下操作:更好的是,为什么不为您的宏定义一个宏:
现在您可以只调用一个简短的
MY_ERR;
而不用担心行长度限制(我认为这就是你这样做的原因)。If you insist upon using
__LINE__
on the line following the call, then just do:Better yet, why don't you just define a macro for your macro:
Now you can just call a short
MY_ERR;
instead of worrying about line length restrictions (which is why you're doing this, I assume).