#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?
c conditional-compilation
add a comment |
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?
c conditional-compilation
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
add a comment |
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?
c conditional-compilation
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
c conditional-compilation
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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