Consider the below program.
C++
// C++ implementation #include <bits/stdc++.h> using namespace std; int x = 0; int f1() { x = 5; return x; } int f2() { x = 10; return x; } int main() { int p = f1() + f2(); cout << ("%d ", x); getchar(); return 0; } |
C
#include <stdio.h> int x = 0; int f1() { x = 5; return x; } int f2() { x = 10; return x; } int main() { int p = f1() + f2(); printf("%d ", x); getchar(); return 0; } |
Java
class GFG { static int x = 0; static int f1() { x = 5; return x; } static int f2() { x = 10; return x; } public static void main(String[] args) { int p = f1() + f2(); System.out.printf("%d ", x); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the above approach class A(): x = 0; def f1(): A.x = 5; return A.x; def f2(): A.x = 10; return A.x; # Driver Code p = A.f1() + A.f2(); print(A.x); # This code is contributed by mits |
C#
// C# implementation of the above approach using System; class GFG { static int x = 0; static int f1() { x = 5; return x; } static int f2() { x = 10; return x; } // Driver code public static void Main(String[] args) { int p = f1() + f2(); Console.WriteLine("{0} ", x); } } // This code has been contributed // by 29AjayKumar |
PHP
<?php // PHP implementation of the above approach $x = 0; function f1() { global $x; $x = 5; return $x; } function f2() { global $x; $x = 10; return $x; } // Driver Code $p = f1() + f2(); print($x); // This code is contributed by mits ?> |
Output:
10
What would the output of the above program – ‘5’ or ’10’?
The output is undefined as the order of evaluation of f1() + f2() is not mandated by standard. The compiler is free to first call either f1() or f2(). Only when equal level precedence operators appear in an expression, the associativity comes into picture. For example, f1() + f2() + f3() will be considered as (f1() + f2()) + f3(). But among first pair, which function (the operand) evaluated first is not defined by the standard.
Thanks to Venki for suggesting the solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Order of operands for logical operators
- Operands for sizeof operator
- What is evaluation order of function parameters in C?
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Program to copy the contents of one array into another in the reverse order
- C Program to sort rows of the Matrix
- Advantages and Disadvantages of Array in C
- Types of C files after its compilation
- Food Ordering System in C
- Difference between sizeof(int *) and sizeof(int) in C/C++
- Examination Management System in C
- Difference between Static and Dynamic Memory Allocation in C
- How does Volatile qualifier of C works in Computing System

