Order of function call

后端 未结 6 1653
执念已碎
执念已碎 2020-12-11 18:06

for the expression

(func1() * func2()) + func3()

will func1() * func2() be evaluated first as it has brackets or can the functions be calle

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 18:55

    Precedence of operators has got nothing to do anything with the order of evaluation of operands.

    The C or C++ Standard doesn't determine the order in which the functions would be called. .

    The order of evaluation of subexpressions, including

    • the arguments of a function call and
    • operands of operators (e.g., +, -, =, * , /), with the exception of:
      • the binary logical operators (&& and ||),
      • the ternary conditional operator (?:), and
      • the comma operator (,)

    is Unspecified

    For example

      int Hello()
      {
           return printf("Hello"); /* printf() returns the number of 
                                      characters successfully printed by it
                                   */
      }
    
      int World()
      {
           return printf("World !");
      }
    
      int main()
      {
    
          int a = Hello() + World(); //might print Hello World! or World! Hello
          /**             ^
                          | 
                    Functions can be called in either order
          **/
          return 0;
      } 
    

提交回复
热议问题