Behavior of __LINE__ when used in a macroWhy use apparently meaningless do-while and if-else statements in macros?Why are these constructs using pre and post-increment undefined behavior?#define macro for debug printing in C?How to redefine a macro using its previous definitionDocumentation concerning platform-specific macros in Linux/POSIXWhy is my program slow when looping over exactly 8192 elements?Can code that is valid in both C and C++ produce different behavior when compiled in each language?How are __FILE__ and __LINE__ expanded in kernel code?Writing a variadic macro which sets specific bits in an integer (bit-mask)Is there a tool that checks what predefined macros a C file depends on?

How could the B-29 bomber back up under its own power?

Would it be possible to set up a franchise in the ancient world?

How to convince boss to spend notice period on documentation instead of new projects

Can a Warforged have a ranged weapon affixed to them like an armblade?

Parse a C++14 integer literal

Isn't Kirchhoff's junction law a violation of conservation of charge?

Latin words remembered from high school 50 years ago

Behavior of __LINE__ when used in a macro

Why didn't Daenerys' advisers suggest assassinating Cersei?

On a piano, are the effects of holding notes and the sustain pedal the same for a single chord?

In How Many Ways Can We Partition a Set Into Smaller Subsets So The Sum of the Numbers In Each Subset Is Equal?

Hotel booking: Why is Agoda much cheaper than booking.com?

Bash Read: Reading comma separated list, last element is missed

Warped chessboard

How to plot a surface from a system of equations?

"File type Zip archive (application/zip) is not supported" when opening a .pdf file

Was Tyrion always a poor strategist?

How to safely discharge oneself

Does ratifying USMCA imply a (stealth) ratification of UNCLOS?

Why is so much ransomware breakable?

Is a reptile with diamond scales possible?

How to fix "webpack Dev Server Invalid Options" in Vuejs

How do you cope with rejection?

Precedent for disabled Kings



Behavior of __LINE__ when used in a macro


Why use apparently meaningless do-while and if-else statements in macros?Why are these constructs using pre and post-increment undefined behavior?#define macro for debug printing in C?How to redefine a macro using its previous definitionDocumentation concerning platform-specific macros in Linux/POSIXWhy is my program slow when looping over exactly 8192 elements?Can code that is valid in both C and C++ produce different behavior when compiled in each language?How are __FILE__ and __LINE__ expanded in kernel code?Writing a variadic macro which sets specific bits in an integer (bit-mask)Is there a tool that checks what predefined macros a C file depends on?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








8















Why does __LINE__ evaluate differently based on whether it's used inside a function-like macro or a regular function?



For example:



#include<stdio.h>

#define A() printf("%dn",__LINE__);

int main(void)
/* 6 */ A();
/* 7 */ A(
/* 8 */ );
/* 9 */ printf("%dn",__LINE__
/* 10 */ );



I would expect to get:



6
7
9


But instead we get:



6
8
9


Note how in the function-like macro spread over lines 7 & 8, the last line occupied is used, rather than the first.



GCC's documentation is light on details: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html



Why do I care? I'm writing a parser that needs to find the line numbers of all instances of the macro A in such a way as to line up with what __LINE__ will return. It is much harder to find the last line of the macro usage rather than the first due to the need to parse possibly escaped arguments.










share|improve this question

















  • 1





    Ultimately you will need to do the parsing correctly anyway, as there may be macros within macro arguments and other complications.

    – Eric Postpischil
    4 hours ago











  • I am suprised to see, that the output changed with last gcc version. So: no worries. When you update to gcc9.1 the output will match expected(!), see godbolt. I am also suprised to see there is no double ;; on the end of the second line in main when using gcc -E.

    – Kamil Cuk
    4 hours ago


















8















Why does __LINE__ evaluate differently based on whether it's used inside a function-like macro or a regular function?



For example:



#include<stdio.h>

#define A() printf("%dn",__LINE__);

int main(void)
/* 6 */ A();
/* 7 */ A(
/* 8 */ );
/* 9 */ printf("%dn",__LINE__
/* 10 */ );



I would expect to get:



6
7
9


But instead we get:



6
8
9


Note how in the function-like macro spread over lines 7 & 8, the last line occupied is used, rather than the first.



GCC's documentation is light on details: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html



Why do I care? I'm writing a parser that needs to find the line numbers of all instances of the macro A in such a way as to line up with what __LINE__ will return. It is much harder to find the last line of the macro usage rather than the first due to the need to parse possibly escaped arguments.










share|improve this question

















  • 1





    Ultimately you will need to do the parsing correctly anyway, as there may be macros within macro arguments and other complications.

    – Eric Postpischil
    4 hours ago











  • I am suprised to see, that the output changed with last gcc version. So: no worries. When you update to gcc9.1 the output will match expected(!), see godbolt. I am also suprised to see there is no double ;; on the end of the second line in main when using gcc -E.

    – Kamil Cuk
    4 hours ago














8












8








8








Why does __LINE__ evaluate differently based on whether it's used inside a function-like macro or a regular function?



For example:



#include<stdio.h>

#define A() printf("%dn",__LINE__);

int main(void)
/* 6 */ A();
/* 7 */ A(
/* 8 */ );
/* 9 */ printf("%dn",__LINE__
/* 10 */ );



I would expect to get:



6
7
9


But instead we get:



6
8
9


Note how in the function-like macro spread over lines 7 & 8, the last line occupied is used, rather than the first.



GCC's documentation is light on details: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html



Why do I care? I'm writing a parser that needs to find the line numbers of all instances of the macro A in such a way as to line up with what __LINE__ will return. It is much harder to find the last line of the macro usage rather than the first due to the need to parse possibly escaped arguments.










share|improve this question














Why does __LINE__ evaluate differently based on whether it's used inside a function-like macro or a regular function?



For example:



#include<stdio.h>

#define A() printf("%dn",__LINE__);

int main(void)
/* 6 */ A();
/* 7 */ A(
/* 8 */ );
/* 9 */ printf("%dn",__LINE__
/* 10 */ );



I would expect to get:



6
7
9


But instead we get:



6
8
9


Note how in the function-like macro spread over lines 7 & 8, the last line occupied is used, rather than the first.



GCC's documentation is light on details: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html



Why do I care? I'm writing a parser that needs to find the line numbers of all instances of the macro A in such a way as to line up with what __LINE__ will return. It is much harder to find the last line of the macro usage rather than the first due to the need to parse possibly escaped arguments.







c gcc c-preprocessor






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









Chris MerckChris Merck

19310




19310







  • 1





    Ultimately you will need to do the parsing correctly anyway, as there may be macros within macro arguments and other complications.

    – Eric Postpischil
    4 hours ago











  • I am suprised to see, that the output changed with last gcc version. So: no worries. When you update to gcc9.1 the output will match expected(!), see godbolt. I am also suprised to see there is no double ;; on the end of the second line in main when using gcc -E.

    – Kamil Cuk
    4 hours ago













  • 1





    Ultimately you will need to do the parsing correctly anyway, as there may be macros within macro arguments and other complications.

    – Eric Postpischil
    4 hours ago











  • I am suprised to see, that the output changed with last gcc version. So: no worries. When you update to gcc9.1 the output will match expected(!), see godbolt. I am also suprised to see there is no double ;; on the end of the second line in main when using gcc -E.

    – Kamil Cuk
    4 hours ago








1




1





Ultimately you will need to do the parsing correctly anyway, as there may be macros within macro arguments and other complications.

– Eric Postpischil
4 hours ago





Ultimately you will need to do the parsing correctly anyway, as there may be macros within macro arguments and other complications.

– Eric Postpischil
4 hours ago













I am suprised to see, that the output changed with last gcc version. So: no worries. When you update to gcc9.1 the output will match expected(!), see godbolt. I am also suprised to see there is no double ;; on the end of the second line in main when using gcc -E.

– Kamil Cuk
4 hours ago






I am suprised to see, that the output changed with last gcc version. So: no worries. When you update to gcc9.1 the output will match expected(!), see godbolt. I am also suprised to see there is no double ;; on the end of the second line in main when using gcc -E.

– Kamil Cuk
4 hours ago













1 Answer
1






active

oldest

votes


















7














The C implementation does not replace the A() macro until it sees the closing ). That ) appears on line 8, so that is the point at which macro replacement occurs.



The specifics of __LINE__ with regard to macro replacement are not well specified by the C standard. You should likely not rely on a particular behavior here. Certainly the C implementation cannot replace the A() macro while it has read only up to line 7, as it does not know what is coming yet. Once it has seen the closing ), then, as it replaces the macro, it might consider the replacement tokens to be occurring on line 7 or on line 8 or on some mix—the C standard is not specific about this; line numbers are largely irrelevant to C semantics at this point, and the __LINE__ macro is largely a convenience for debugging and other development work, not a feature for production programs (although it may have some uses for them).



In the printf, the C implementation recognizes the __LINE__ macro as soon as it sees the end of the line. (Actually, the parsing is more involved; the input has been tokenized, but the effect is the __LINE__ token is recognized when the end-of-line character is examined.) It is on line 9, so it is replaced by 9. The fact it is an argument to printf is irrelevant. The C implementation does not have the process the printf in order to replace the __LINE__ token that appears on line 9; they do not interact.






share|improve this answer




















  • 1





    The first point here is important, but the second is crucial. The standard provides no basis for judging the observed behavior either more or less correct than the OP's expected behavior.

    – John Bollinger
    4 hours ago











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56193082%2fbehavior-of-line-when-used-in-a-macro%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














The C implementation does not replace the A() macro until it sees the closing ). That ) appears on line 8, so that is the point at which macro replacement occurs.



The specifics of __LINE__ with regard to macro replacement are not well specified by the C standard. You should likely not rely on a particular behavior here. Certainly the C implementation cannot replace the A() macro while it has read only up to line 7, as it does not know what is coming yet. Once it has seen the closing ), then, as it replaces the macro, it might consider the replacement tokens to be occurring on line 7 or on line 8 or on some mix—the C standard is not specific about this; line numbers are largely irrelevant to C semantics at this point, and the __LINE__ macro is largely a convenience for debugging and other development work, not a feature for production programs (although it may have some uses for them).



In the printf, the C implementation recognizes the __LINE__ macro as soon as it sees the end of the line. (Actually, the parsing is more involved; the input has been tokenized, but the effect is the __LINE__ token is recognized when the end-of-line character is examined.) It is on line 9, so it is replaced by 9. The fact it is an argument to printf is irrelevant. The C implementation does not have the process the printf in order to replace the __LINE__ token that appears on line 9; they do not interact.






share|improve this answer




















  • 1





    The first point here is important, but the second is crucial. The standard provides no basis for judging the observed behavior either more or less correct than the OP's expected behavior.

    – John Bollinger
    4 hours ago















7














The C implementation does not replace the A() macro until it sees the closing ). That ) appears on line 8, so that is the point at which macro replacement occurs.



The specifics of __LINE__ with regard to macro replacement are not well specified by the C standard. You should likely not rely on a particular behavior here. Certainly the C implementation cannot replace the A() macro while it has read only up to line 7, as it does not know what is coming yet. Once it has seen the closing ), then, as it replaces the macro, it might consider the replacement tokens to be occurring on line 7 or on line 8 or on some mix—the C standard is not specific about this; line numbers are largely irrelevant to C semantics at this point, and the __LINE__ macro is largely a convenience for debugging and other development work, not a feature for production programs (although it may have some uses for them).



In the printf, the C implementation recognizes the __LINE__ macro as soon as it sees the end of the line. (Actually, the parsing is more involved; the input has been tokenized, but the effect is the __LINE__ token is recognized when the end-of-line character is examined.) It is on line 9, so it is replaced by 9. The fact it is an argument to printf is irrelevant. The C implementation does not have the process the printf in order to replace the __LINE__ token that appears on line 9; they do not interact.






share|improve this answer




















  • 1





    The first point here is important, but the second is crucial. The standard provides no basis for judging the observed behavior either more or less correct than the OP's expected behavior.

    – John Bollinger
    4 hours ago













7












7








7







The C implementation does not replace the A() macro until it sees the closing ). That ) appears on line 8, so that is the point at which macro replacement occurs.



The specifics of __LINE__ with regard to macro replacement are not well specified by the C standard. You should likely not rely on a particular behavior here. Certainly the C implementation cannot replace the A() macro while it has read only up to line 7, as it does not know what is coming yet. Once it has seen the closing ), then, as it replaces the macro, it might consider the replacement tokens to be occurring on line 7 or on line 8 or on some mix—the C standard is not specific about this; line numbers are largely irrelevant to C semantics at this point, and the __LINE__ macro is largely a convenience for debugging and other development work, not a feature for production programs (although it may have some uses for them).



In the printf, the C implementation recognizes the __LINE__ macro as soon as it sees the end of the line. (Actually, the parsing is more involved; the input has been tokenized, but the effect is the __LINE__ token is recognized when the end-of-line character is examined.) It is on line 9, so it is replaced by 9. The fact it is an argument to printf is irrelevant. The C implementation does not have the process the printf in order to replace the __LINE__ token that appears on line 9; they do not interact.






share|improve this answer















The C implementation does not replace the A() macro until it sees the closing ). That ) appears on line 8, so that is the point at which macro replacement occurs.



The specifics of __LINE__ with regard to macro replacement are not well specified by the C standard. You should likely not rely on a particular behavior here. Certainly the C implementation cannot replace the A() macro while it has read only up to line 7, as it does not know what is coming yet. Once it has seen the closing ), then, as it replaces the macro, it might consider the replacement tokens to be occurring on line 7 or on line 8 or on some mix—the C standard is not specific about this; line numbers are largely irrelevant to C semantics at this point, and the __LINE__ macro is largely a convenience for debugging and other development work, not a feature for production programs (although it may have some uses for them).



In the printf, the C implementation recognizes the __LINE__ macro as soon as it sees the end of the line. (Actually, the parsing is more involved; the input has been tokenized, but the effect is the __LINE__ token is recognized when the end-of-line character is examined.) It is on line 9, so it is replaced by 9. The fact it is an argument to printf is irrelevant. The C implementation does not have the process the printf in order to replace the __LINE__ token that appears on line 9; they do not interact.







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









Eric PostpischilEric Postpischil

83.2k890169




83.2k890169







  • 1





    The first point here is important, but the second is crucial. The standard provides no basis for judging the observed behavior either more or less correct than the OP's expected behavior.

    – John Bollinger
    4 hours ago












  • 1





    The first point here is important, but the second is crucial. The standard provides no basis for judging the observed behavior either more or less correct than the OP's expected behavior.

    – John Bollinger
    4 hours ago







1




1





The first point here is important, but the second is crucial. The standard provides no basis for judging the observed behavior either more or less correct than the OP's expected behavior.

– John Bollinger
4 hours ago





The first point here is important, but the second is crucial. The standard provides no basis for judging the observed behavior either more or less correct than the OP's expected behavior.

– John Bollinger
4 hours ago



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56193082%2fbehavior-of-line-when-used-in-a-macro%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Log på Navigationsmenu

Wonderful Copenhagen (sang) Eksterne henvisninger | NavigationsmenurSide på frankloesser.comWonderful Copenhagen

Detroit Tigers Spis treści Historia | Skład zespołu | Sukcesy | Członkowie Baseball Hall of Fame | Zastrzeżone numery | Przypisy | Menu nawigacyjneEncyclopedia of Detroit - Detroit TigersTigers Stadium, Detroit, MITigers Timeline 1900sDetroit Tigers Team History & EncyclopediaTigers Timeline 1910s1935 World Series1945 World Series1945 World Series1984 World SeriesComerica Park, Detroit, MI2006 World Series2012 World SeriesDetroit Tigers 40-Man RosterDetroit Tigers Coaching StaffTigers Hall of FamersTigers Retired Numberse