c++ - 假定动态作用域,以下 C++ 程序的输出是什么?

标签 c++ function dynamic global-variables scoping

<分区>

假设动态作用域,以下 C++ 程序的输出是什么? 我有 turboc++ 编译器,其中显示的输出使用静态范围,答案如下: 8个 6个 50 现在,我怀疑假设动态范围的输出将是 任何一个 207 104 52 - 或者 - 207 104 50

#include<iostream.h>
#include<conio.h>
int n=1;
void printn(int x)
{
  cout<<x+n<<"\n";
}
void increment()
{
  n=n+2;
  printn(n);
}
void main()
{
  clrscr();
  int n;
  n=200;
  printn(7);
  n=50;
  increment();
  cout<<n;
  getch();
}

最佳答案

任何符合标准的编译器都会给你错误并且不输出任何东西,因为你

#include<iostream.h>

之后你使用

cout << ...

没有用 std:: 限定它或者有一个 using 指令并且因为

void main()

在你修复这些之后,任何符合标准的 C++ 编译器都会输出

8 
6 
50

关于c++ - 假定动态作用域,以下 C++ 程序的输出是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13696368/

相关文章:

c++ - 显示对象中的字符数组

javascript - 从另一个页面调用 javascript 函数

VB.NET 函数返回

C# 无法从 'out string' 转换为 'out dynamic'

php - 修改javascript以输出表单名称而不是表单id

C++ 模板化映射包含自身

c++ - 有效地对定义顺序的 vector 子集进行排序

javascript - 评估代码的正确方法

javascript - 在javascript中初始化 'multidimensional'对象

c++ - C++函数中局部类的使用