吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → 操作数的求值顺序

操作数的求值顺序

来源:网络 | 更新时间:2022-01-10 09:11:39
考虑下面的程序。 
// 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;
}
#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;
}
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);
}
}
# 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);
// 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);
}
}
<?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);

?>
<script>
x = 0;

function f1()
{
x = 5;
return x;
}

function f2()
{
x = 10;
return x;
}


var p = f1() + f2();
document.write(x);

</script>
输出: 
10
上述程序的输出是什么——“5”或“10”?  输出未定义,因为 f1() + f2() 的评估顺序不是标准规定的。编译器可以自由地首先调用 f1() 或 f2()。只有当相等级别的优先级运算符出现在表达式中时,关联性才会显现。例如,f1() + f2() + f3() 将被视为 (f1() + f2()) + f3()。但是在第一对中,标准没有定义首先评估的函数(操作数)。   

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com