202002XX-变量的作用域

  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "cstring"

  4. #include "string"

  5. void oil(int x);

  6. using namespace std;

  7. int main()

  8. { int texas=31;

  9. int year=2011;

  10. cout<<"in main,texas="<<texas<<",&texas="<<&texas<<endl;

  11. cout<<"in main,year="<<year<<",&year="<<&year<<endl;

  12. oil(texas);

  13. cout<<"back to main,texas="<<texas<<",&texas="<<&texas<<endl;

  14. cout<<"back to main,year="<<year<<",&year="<<&year<<endl;

  15. return 0;

  16. }

  17. void oil(int x)

  18. {

  19.   int texas=5;

  20. cout<<"in oil,texas="<<texas<<",&texas="<<&texas<<endl;  

  21. cout<<"in oil,x="<<x<<",&x="<<&x<<endl;

  22. {                         //start a block

  23.   int texas=113;

  24.       cout<<"in block,texas="<<texas<<",&texas="<<&texas<<endl;   

  25.   cout<<"in block,x="<<x<<",&x="<<&x<<endl;

  26. }

  27. cout<<"in post-block,texas="<<texas<<",&texas="<<&texas<<endl; 

  28. }

运行结果:




代码2(auto和register)

#include "stdafx.h"
#include "iostream"
#include "cstring"
#include "string"
void oil(int x);
using namespace std;
int main()
{
register char  big='a';//可以提高速度,用的是cpu的寄存器
register char a='b';
register auto auto1=big+a;//auto可以自动识别数据类型,strng可不行,因为string是类。
cout<<auto1<<endl;//97+98=195
}
运行结果

posted @ 2020-05-21 08:32  财盛  阅读(129)  评论(0编辑  收藏  举报