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;
}







1















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?










share|improve this question























  • 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


















1















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?










share|improve this question























  • 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














1












1








1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer

































    0














    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






    share|improve this answer
























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









      2














      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.






      share|improve this answer






























        2














        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.






        share|improve this answer




























          2












          2








          2







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 23:29

























          answered Nov 16 '18 at 23:17









          Eric PostpischilEric Postpischil

          80.9k890169




          80.9k890169

























              0














              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






              share|improve this answer




























                0














                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






                share|improve this answer


























                  0












                  0








                  0







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 23:20









                  dbushdbush

                  104k14110148




                  104k14110148






























                      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%2f53346559%2fthe-precedence-of-preprocessor-operator-defined%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python