#ifndef in C being ignored?









up vote
3
down vote

favorite












I have a bit of code where I want to have logging only if DEBUG is defined. So I though I might be able to replace a Token (here: "DEBUGLOG") with the comment string "//". But how to do?



 #ifndef DEBUG
#define DEBUGLOG //
#endif

[...]
DEBUGLOG printf("Debug String"n);
[...]


There is no definition of DEBUG somwhere else in the code. But my gcc compiles this line and the program itself executes the printf();



Why?



I tried to include it in parantheses like this, but it gets an compile error:



#ifndef DEBUG
#define DEBUGLOG "//"
#endif


This is the compiler message:



beispiel.c:45:10: error: expected ‘;’ before ‘printf’
DEBUGLOG printf("Debug String"n);
^


Any hints?










share|improve this question























  • Can your compiler output its preprocessed code? If you do, you might find that comments are stripped out before preprocessing, and the macro expands to "nothing".
    – usr2564301
    2 days ago










  • Sorry, I do not get it. "comments are stripped out before preprocessing" - at this first stage it is not yet a comment. Then the preprocessor follows and replaces the string with "//". And then I have a comment in which the compiler can not deal with as step 1 is already done? Strange...
    – Christian
    2 days ago














up vote
3
down vote

favorite












I have a bit of code where I want to have logging only if DEBUG is defined. So I though I might be able to replace a Token (here: "DEBUGLOG") with the comment string "//". But how to do?



 #ifndef DEBUG
#define DEBUGLOG //
#endif

[...]
DEBUGLOG printf("Debug String"n);
[...]


There is no definition of DEBUG somwhere else in the code. But my gcc compiles this line and the program itself executes the printf();



Why?



I tried to include it in parantheses like this, but it gets an compile error:



#ifndef DEBUG
#define DEBUGLOG "//"
#endif


This is the compiler message:



beispiel.c:45:10: error: expected ‘;’ before ‘printf’
DEBUGLOG printf("Debug String"n);
^


Any hints?










share|improve this question























  • Can your compiler output its preprocessed code? If you do, you might find that comments are stripped out before preprocessing, and the macro expands to "nothing".
    – usr2564301
    2 days ago










  • Sorry, I do not get it. "comments are stripped out before preprocessing" - at this first stage it is not yet a comment. Then the preprocessor follows and replaces the string with "//". And then I have a comment in which the compiler can not deal with as step 1 is already done? Strange...
    – Christian
    2 days ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a bit of code where I want to have logging only if DEBUG is defined. So I though I might be able to replace a Token (here: "DEBUGLOG") with the comment string "//". But how to do?



 #ifndef DEBUG
#define DEBUGLOG //
#endif

[...]
DEBUGLOG printf("Debug String"n);
[...]


There is no definition of DEBUG somwhere else in the code. But my gcc compiles this line and the program itself executes the printf();



Why?



I tried to include it in parantheses like this, but it gets an compile error:



#ifndef DEBUG
#define DEBUGLOG "//"
#endif


This is the compiler message:



beispiel.c:45:10: error: expected ‘;’ before ‘printf’
DEBUGLOG printf("Debug String"n);
^


Any hints?










share|improve this question















I have a bit of code where I want to have logging only if DEBUG is defined. So I though I might be able to replace a Token (here: "DEBUGLOG") with the comment string "//". But how to do?



 #ifndef DEBUG
#define DEBUGLOG //
#endif

[...]
DEBUGLOG printf("Debug String"n);
[...]


There is no definition of DEBUG somwhere else in the code. But my gcc compiles this line and the program itself executes the printf();



Why?



I tried to include it in parantheses like this, but it gets an compile error:



#ifndef DEBUG
#define DEBUGLOG "//"
#endif


This is the compiler message:



beispiel.c:45:10: error: expected ‘;’ before ‘printf’
DEBUGLOG printf("Debug String"n);
^


Any hints?







c conditional-compilation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Sourav Ghosh

106k14129185




106k14129185










asked 2 days ago









Christian

233




233











  • Can your compiler output its preprocessed code? If you do, you might find that comments are stripped out before preprocessing, and the macro expands to "nothing".
    – usr2564301
    2 days ago










  • Sorry, I do not get it. "comments are stripped out before preprocessing" - at this first stage it is not yet a comment. Then the preprocessor follows and replaces the string with "//". And then I have a comment in which the compiler can not deal with as step 1 is already done? Strange...
    – Christian
    2 days ago
















  • Can your compiler output its preprocessed code? If you do, you might find that comments are stripped out before preprocessing, and the macro expands to "nothing".
    – usr2564301
    2 days ago










  • Sorry, I do not get it. "comments are stripped out before preprocessing" - at this first stage it is not yet a comment. Then the preprocessor follows and replaces the string with "//". And then I have a comment in which the compiler can not deal with as step 1 is already done? Strange...
    – Christian
    2 days ago















Can your compiler output its preprocessed code? If you do, you might find that comments are stripped out before preprocessing, and the macro expands to "nothing".
– usr2564301
2 days ago




Can your compiler output its preprocessed code? If you do, you might find that comments are stripped out before preprocessing, and the macro expands to "nothing".
– usr2564301
2 days ago












Sorry, I do not get it. "comments are stripped out before preprocessing" - at this first stage it is not yet a comment. Then the preprocessor follows and replaces the string with "//". And then I have a comment in which the compiler can not deal with as step 1 is already done? Strange...
– Christian
2 days ago




Sorry, I do not get it. "comments are stripped out before preprocessing" - at this first stage it is not yet a comment. Then the preprocessor follows and replaces the string with "//". And then I have a comment in which the compiler can not deal with as step 1 is already done? Strange...
– Christian
2 days ago












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










If you look up the Phases of translation, you will find that the Phase where preprocessor is executed (Phase 4) is after the phase where comments are replaced by a white space character (Phase 3).




Phase 3

1) The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following

...

2) Each comment is replaced by one space character



Phase 4

1) Preprocessor is executed.




So in Phase 3 the line:



#define DEBUGLOG //


becomes:



#define DEBUGLOG 


And in Phase 4 the line:



DEBUGLOG printf("Debug String"n);


becomes:



printf("Debug String"n);


And that is why your printf is executed.



And when you put it quotes ("//"), that line becomes:



"//" printf("Debug String"n);


The quotes ("") will not be removed. And this is a compiler error.






share|improve this answer






















  • Ok, got it so far. But why do I get an error when using "//"? It will become to "//" instead of // exactly, right? Is there a way to get what I want without +ifdef lines for every call?
    – Christian
    2 days ago










  • @Christian: Updated the answer to include this aspect as well.
    – P.W
    2 days 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',
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%2f53237899%2fifndef-in-c-being-ignored%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










If you look up the Phases of translation, you will find that the Phase where preprocessor is executed (Phase 4) is after the phase where comments are replaced by a white space character (Phase 3).




Phase 3

1) The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following

...

2) Each comment is replaced by one space character



Phase 4

1) Preprocessor is executed.




So in Phase 3 the line:



#define DEBUGLOG //


becomes:



#define DEBUGLOG 


And in Phase 4 the line:



DEBUGLOG printf("Debug String"n);


becomes:



printf("Debug String"n);


And that is why your printf is executed.



And when you put it quotes ("//"), that line becomes:



"//" printf("Debug String"n);


The quotes ("") will not be removed. And this is a compiler error.






share|improve this answer






















  • Ok, got it so far. But why do I get an error when using "//"? It will become to "//" instead of // exactly, right? Is there a way to get what I want without +ifdef lines for every call?
    – Christian
    2 days ago










  • @Christian: Updated the answer to include this aspect as well.
    – P.W
    2 days ago














up vote
3
down vote



accepted










If you look up the Phases of translation, you will find that the Phase where preprocessor is executed (Phase 4) is after the phase where comments are replaced by a white space character (Phase 3).




Phase 3

1) The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following

...

2) Each comment is replaced by one space character



Phase 4

1) Preprocessor is executed.




So in Phase 3 the line:



#define DEBUGLOG //


becomes:



#define DEBUGLOG 


And in Phase 4 the line:



DEBUGLOG printf("Debug String"n);


becomes:



printf("Debug String"n);


And that is why your printf is executed.



And when you put it quotes ("//"), that line becomes:



"//" printf("Debug String"n);


The quotes ("") will not be removed. And this is a compiler error.






share|improve this answer






















  • Ok, got it so far. But why do I get an error when using "//"? It will become to "//" instead of // exactly, right? Is there a way to get what I want without +ifdef lines for every call?
    – Christian
    2 days ago










  • @Christian: Updated the answer to include this aspect as well.
    – P.W
    2 days ago












up vote
3
down vote



accepted







up vote
3
down vote



accepted






If you look up the Phases of translation, you will find that the Phase where preprocessor is executed (Phase 4) is after the phase where comments are replaced by a white space character (Phase 3).




Phase 3

1) The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following

...

2) Each comment is replaced by one space character



Phase 4

1) Preprocessor is executed.




So in Phase 3 the line:



#define DEBUGLOG //


becomes:



#define DEBUGLOG 


And in Phase 4 the line:



DEBUGLOG printf("Debug String"n);


becomes:



printf("Debug String"n);


And that is why your printf is executed.



And when you put it quotes ("//"), that line becomes:



"//" printf("Debug String"n);


The quotes ("") will not be removed. And this is a compiler error.






share|improve this answer














If you look up the Phases of translation, you will find that the Phase where preprocessor is executed (Phase 4) is after the phase where comments are replaced by a white space character (Phase 3).




Phase 3

1) The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following

...

2) Each comment is replaced by one space character



Phase 4

1) Preprocessor is executed.




So in Phase 3 the line:



#define DEBUGLOG //


becomes:



#define DEBUGLOG 


And in Phase 4 the line:



DEBUGLOG printf("Debug String"n);


becomes:



printf("Debug String"n);


And that is why your printf is executed.



And when you put it quotes ("//"), that line becomes:



"//" printf("Debug String"n);


The quotes ("") will not be removed. And this is a compiler error.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









P.W

7,0241438




7,0241438











  • Ok, got it so far. But why do I get an error when using "//"? It will become to "//" instead of // exactly, right? Is there a way to get what I want without +ifdef lines for every call?
    – Christian
    2 days ago










  • @Christian: Updated the answer to include this aspect as well.
    – P.W
    2 days ago
















  • Ok, got it so far. But why do I get an error when using "//"? It will become to "//" instead of // exactly, right? Is there a way to get what I want without +ifdef lines for every call?
    – Christian
    2 days ago










  • @Christian: Updated the answer to include this aspect as well.
    – P.W
    2 days ago















Ok, got it so far. But why do I get an error when using "//"? It will become to "//" instead of // exactly, right? Is there a way to get what I want without +ifdef lines for every call?
– Christian
2 days ago




Ok, got it so far. But why do I get an error when using "//"? It will become to "//" instead of // exactly, right? Is there a way to get what I want without +ifdef lines for every call?
– Christian
2 days ago












@Christian: Updated the answer to include this aspect as well.
– P.W
2 days ago




@Christian: Updated the answer to include this aspect as well.
– P.W
2 days ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237899%2fifndef-in-c-being-ignored%23new-answer', 'question_page');

);

Post as a guest














































































這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3