开发者

Is this the correct behaviour of __LINE__

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 sh开发者_运维问答ould I do to get "line 15" ?


__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.


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...


If you insist upon using __LINE__ on the line following the call, then just do:

    ERROR_MACRO("Err in %s on line %d\n",
                __FILE__, __LINE__ - 1);

Better yet, why don't you just define a macro for your macro:

    #define MY_ERR ERROR_MACRO("Err in %s on line %d", __FILE__, __LINE__)

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜