The precedence of preprocessor operator “defined”?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm working on a c preprocessor and have found that, since "defined" is a preprocessor-only operator, its precedence level is never listed among the other c operators. Since it is unary and logical, I'd put it on the 2nd level, but...
Does anyone know the exact answer?
c operators c-preprocessor
add a comment |
I'm working on a c preprocessor and have found that, since "defined" is a preprocessor-only operator, its precedence level is never listed among the other c operators. Since it is unary and logical, I'd put it on the 2nd level, but...
Does anyone know the exact answer?
c operators c-preprocessor
Your mention of a “level” for precedence suggests you are referring to some secondary source. If you are working on a C preprocessor, you ought to be using the C standard.
– Eric Postpischil
Nov 16 '18 at 23:14
add a comment |
I'm working on a c preprocessor and have found that, since "defined" is a preprocessor-only operator, its precedence level is never listed among the other c operators. Since it is unary and logical, I'd put it on the 2nd level, but...
Does anyone know the exact answer?
c operators c-preprocessor
I'm working on a c preprocessor and have found that, since "defined" is a preprocessor-only operator, its precedence level is never listed among the other c operators. Since it is unary and logical, I'd put it on the 2nd level, but...
Does anyone know the exact answer?
c operators c-preprocessor
c operators c-preprocessor
asked Nov 16 '18 at 23:10
LajosLajos
102
102
Your mention of a “level” for precedence suggests you are referring to some secondary source. If you are working on a C preprocessor, you ought to be using the C standard.
– Eric Postpischil
Nov 16 '18 at 23:14
add a comment |
Your mention of a “level” for precedence suggests you are referring to some secondary source. If you are working on a C preprocessor, you ought to be using the C standard.
– Eric Postpischil
Nov 16 '18 at 23:14
Your mention of a “level” for precedence suggests you are referring to some secondary source. If you are working on a C preprocessor, you ought to be using the C standard.
– Eric Postpischil
Nov 16 '18 at 23:14
Your mention of a “level” for precedence suggests you are referring to some secondary source. If you are working on a C preprocessor, you ought to be using the C standard.
– Eric Postpischil
Nov 16 '18 at 23:14
add a comment |
2 Answers
2
active
oldest
votes
C 2018 6.10.1 1 says:
The expression that controls conditional inclusion shall be an integer constant expression except that… it may contain unary operator expressions of the form “
defined
identifier” or “defined (
identifier)
”… [Note: The text in quotes here is offset display text in the original.]
The phrase “unary operator expression” refers to 6.5.3 (“Unary operators”), a subsection of 6.5 (“Expressions”). Thus, defined
behaves like any of the other unary operators.
However, note that the operand must be an identifier. It cannot be the general unary-expression or cast-expression that most normal operators accept. It is those unary-expression or cast-expression symbols that bring higher-precedence operators into the grammar for unary expressions. This means something like #if defined x++
is not permitted (even prior to consideration of whether ++
may appear in an integer constant expression), so there is never any other option. “defined
identifier” never appears with any higher-precedence operator adjacent to the identifier.
add a comment |
An #if
directive is immediately followed by a constant experssion. Any defined
operators are evaluated first before the rest of the constant expression is evaluated.
Section 6.10.1p4 of the C standard states:
Prior to evaluation, macro invocations in the list of preprocessing
tokens that will become the controlling constant expression are
replaced (except for those macro names modified by the defined unary
operator), just as in normal text. If the token defined is
generated as a result of this replacement process or use of the
defined unary operator does not match one of the two specified forms
prior to macro replacement, the behavior is undefined. After all
replacements due to macro expansion and the defined unary
operator have been performed, all remaining identifiers
(including those lexically identical to keywords) are replaced
with the pp-number 0 , and then each preprocessing token is
converted into a token. The resulting tokens compose the
controlling constant expression which is evaluated according to
the rules of 6.6.
...
The references section 6.6 dictates the semantics of Constant Expressions
add a comment |
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346559%2fthe-precedence-of-preprocessor-operator-defined%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
C 2018 6.10.1 1 says:
The expression that controls conditional inclusion shall be an integer constant expression except that… it may contain unary operator expressions of the form “
defined
identifier” or “defined (
identifier)
”… [Note: The text in quotes here is offset display text in the original.]
The phrase “unary operator expression” refers to 6.5.3 (“Unary operators”), a subsection of 6.5 (“Expressions”). Thus, defined
behaves like any of the other unary operators.
However, note that the operand must be an identifier. It cannot be the general unary-expression or cast-expression that most normal operators accept. It is those unary-expression or cast-expression symbols that bring higher-precedence operators into the grammar for unary expressions. This means something like #if defined x++
is not permitted (even prior to consideration of whether ++
may appear in an integer constant expression), so there is never any other option. “defined
identifier” never appears with any higher-precedence operator adjacent to the identifier.
add a comment |
C 2018 6.10.1 1 says:
The expression that controls conditional inclusion shall be an integer constant expression except that… it may contain unary operator expressions of the form “
defined
identifier” or “defined (
identifier)
”… [Note: The text in quotes here is offset display text in the original.]
The phrase “unary operator expression” refers to 6.5.3 (“Unary operators”), a subsection of 6.5 (“Expressions”). Thus, defined
behaves like any of the other unary operators.
However, note that the operand must be an identifier. It cannot be the general unary-expression or cast-expression that most normal operators accept. It is those unary-expression or cast-expression symbols that bring higher-precedence operators into the grammar for unary expressions. This means something like #if defined x++
is not permitted (even prior to consideration of whether ++
may appear in an integer constant expression), so there is never any other option. “defined
identifier” never appears with any higher-precedence operator adjacent to the identifier.
add a comment |
C 2018 6.10.1 1 says:
The expression that controls conditional inclusion shall be an integer constant expression except that… it may contain unary operator expressions of the form “
defined
identifier” or “defined (
identifier)
”… [Note: The text in quotes here is offset display text in the original.]
The phrase “unary operator expression” refers to 6.5.3 (“Unary operators”), a subsection of 6.5 (“Expressions”). Thus, defined
behaves like any of the other unary operators.
However, note that the operand must be an identifier. It cannot be the general unary-expression or cast-expression that most normal operators accept. It is those unary-expression or cast-expression symbols that bring higher-precedence operators into the grammar for unary expressions. This means something like #if defined x++
is not permitted (even prior to consideration of whether ++
may appear in an integer constant expression), so there is never any other option. “defined
identifier” never appears with any higher-precedence operator adjacent to the identifier.
C 2018 6.10.1 1 says:
The expression that controls conditional inclusion shall be an integer constant expression except that… it may contain unary operator expressions of the form “
defined
identifier” or “defined (
identifier)
”… [Note: The text in quotes here is offset display text in the original.]
The phrase “unary operator expression” refers to 6.5.3 (“Unary operators”), a subsection of 6.5 (“Expressions”). Thus, defined
behaves like any of the other unary operators.
However, note that the operand must be an identifier. It cannot be the general unary-expression or cast-expression that most normal operators accept. It is those unary-expression or cast-expression symbols that bring higher-precedence operators into the grammar for unary expressions. This means something like #if defined x++
is not permitted (even prior to consideration of whether ++
may appear in an integer constant expression), so there is never any other option. “defined
identifier” never appears with any higher-precedence operator adjacent to the identifier.
edited Nov 16 '18 at 23:29
answered Nov 16 '18 at 23:17
Eric PostpischilEric Postpischil
80.9k890169
80.9k890169
add a comment |
add a comment |
An #if
directive is immediately followed by a constant experssion. Any defined
operators are evaluated first before the rest of the constant expression is evaluated.
Section 6.10.1p4 of the C standard states:
Prior to evaluation, macro invocations in the list of preprocessing
tokens that will become the controlling constant expression are
replaced (except for those macro names modified by the defined unary
operator), just as in normal text. If the token defined is
generated as a result of this replacement process or use of the
defined unary operator does not match one of the two specified forms
prior to macro replacement, the behavior is undefined. After all
replacements due to macro expansion and the defined unary
operator have been performed, all remaining identifiers
(including those lexically identical to keywords) are replaced
with the pp-number 0 , and then each preprocessing token is
converted into a token. The resulting tokens compose the
controlling constant expression which is evaluated according to
the rules of 6.6.
...
The references section 6.6 dictates the semantics of Constant Expressions
add a comment |
An #if
directive is immediately followed by a constant experssion. Any defined
operators are evaluated first before the rest of the constant expression is evaluated.
Section 6.10.1p4 of the C standard states:
Prior to evaluation, macro invocations in the list of preprocessing
tokens that will become the controlling constant expression are
replaced (except for those macro names modified by the defined unary
operator), just as in normal text. If the token defined is
generated as a result of this replacement process or use of the
defined unary operator does not match one of the two specified forms
prior to macro replacement, the behavior is undefined. After all
replacements due to macro expansion and the defined unary
operator have been performed, all remaining identifiers
(including those lexically identical to keywords) are replaced
with the pp-number 0 , and then each preprocessing token is
converted into a token. The resulting tokens compose the
controlling constant expression which is evaluated according to
the rules of 6.6.
...
The references section 6.6 dictates the semantics of Constant Expressions
add a comment |
An #if
directive is immediately followed by a constant experssion. Any defined
operators are evaluated first before the rest of the constant expression is evaluated.
Section 6.10.1p4 of the C standard states:
Prior to evaluation, macro invocations in the list of preprocessing
tokens that will become the controlling constant expression are
replaced (except for those macro names modified by the defined unary
operator), just as in normal text. If the token defined is
generated as a result of this replacement process or use of the
defined unary operator does not match one of the two specified forms
prior to macro replacement, the behavior is undefined. After all
replacements due to macro expansion and the defined unary
operator have been performed, all remaining identifiers
(including those lexically identical to keywords) are replaced
with the pp-number 0 , and then each preprocessing token is
converted into a token. The resulting tokens compose the
controlling constant expression which is evaluated according to
the rules of 6.6.
...
The references section 6.6 dictates the semantics of Constant Expressions
An #if
directive is immediately followed by a constant experssion. Any defined
operators are evaluated first before the rest of the constant expression is evaluated.
Section 6.10.1p4 of the C standard states:
Prior to evaluation, macro invocations in the list of preprocessing
tokens that will become the controlling constant expression are
replaced (except for those macro names modified by the defined unary
operator), just as in normal text. If the token defined is
generated as a result of this replacement process or use of the
defined unary operator does not match one of the two specified forms
prior to macro replacement, the behavior is undefined. After all
replacements due to macro expansion and the defined unary
operator have been performed, all remaining identifiers
(including those lexically identical to keywords) are replaced
with the pp-number 0 , and then each preprocessing token is
converted into a token. The resulting tokens compose the
controlling constant expression which is evaluated according to
the rules of 6.6.
...
The references section 6.6 dictates the semantics of Constant Expressions
answered Nov 16 '18 at 23:20
dbushdbush
104k14110148
104k14110148
add a comment |
add a comment |
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.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346559%2fthe-precedence-of-preprocessor-operator-defined%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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
Your mention of a “level” for precedence suggests you are referring to some secondary source. If you are working on a C preprocessor, you ought to be using the C standard.
– Eric Postpischil
Nov 16 '18 at 23:14