/*Copyright(c)2014,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:满星辰
*完成日期:2014年 11月 13日
*版本号:v1.0
*
*问题描述:
*程序输入:
*程序输出:
*/
#include <iostream>
using namespace std;
int a=3, b=5;
int max(int a, int b)
{
int c;
c=a>b? a:b;
return c;
}
int main()
{
int a=8; //若无这一句,又将如何?
cout<<max(a,b)<<endl;
return 0;
}
预期:
8
实际:
若无那一句:
预期:5
实际:
学习心得:
在main函数前面 定义了两个 全局变量
则在整个程序中,这两个变量作用于整个程序
加了那一句,则在那自定义的函数里 该变量不受全局变量的影响
#include <iostream>
using namespace std;
void cude();
int main()
{
extern int x;//去掉extern及本行全删除会怎样?
x=5; //去掉这一句呢?
cude();
cout<<x<<endl;
return 0;
}
int x=10;
void cude()
{
x=x*x*x;
}
预期:125
实际:
若是去掉 extern
得 5
学习心得:
extern为局外变量
目前不是很了解
求大神指教
本文通过两个示例探讨了C++中变量的作用域问题,包括局部变量与全局变量的区别,以及extern关键字的使用方式。文章揭示了不同作用域变量在程序运行过程中的表现。

被折叠的 条评论
为什么被折叠?



