作用域
块作用域 {} 隔离代码的
在一个函数内部定义的变量或者在一个块中定义的变量称为局部变量, 声明周期在块中有效
#include <iostream>
using namespace std;
int main()
{
int a=1, b=2;
++a;
++b;
{
int b=4, c;
c=a+b; //c只能在该复合语句内使用
cout<<"a="<<a<<", "<<"b="<<b<<", "<<"c="<<c<<endl;
}
cout<<"a="<<a<<", "<<"b="<<b<<endl;
}
24623
文件作用域
在函数外面定义的变量或者用extern声明的变量
, 文件是c++的编译单位, 在所有函数外定义的标识符具有文件作用域, 作用域从定义点开始一直到本文件结束
#include <iostream>
int i = 10; // 文件作用域
int main()
{
// 在内部操作全局变量 :: 符合
int i, j = 5;
i=20; //访问局部变量i
::i=::i+4; //访问全局变量i
j=::i+i; //访问全局变量i和局部变量i、j
std::cout<<"::i="<<::i<<std::endl; // 14
std::cout<<"i="<<i<<std::endl; // 20
std::cout<<"j="<<j<<std::endl; // 34
}
存储类别
局部变量的存储类别
c++中 局部变量的存储为
- auto 默认的不用写, 若没有明确的赋值, 初始值是不确定的
- static 静态局部变量被分配到静态存储中(具有记忆功能)
#include <iostream>
using namespace std;
int fun(int x) {
static int a = 3;
a += x;
return a;
}
int main() {
int k = 2, m = 1, n;
n = fun(k);
cout << "first: n=" << n << endl; // 5
n = fun(m);
cout << "second: n=" << n << endl; // 6
}
static 版本的阶乘
#include <iostream>
using namespace std;
int fac(int n)
{
static int f=1;
f=f*n;
return f;
}
int main() {
for (int i=1; i<=5; i++)
cout<<i<<"!="<<fac(i)<<endl;
}
register
适用于变量使用频繁的情况
int fac(register int n)
{
static int f=1;
f=f*n;
return(f);
}
全局变量
定义在所有函数外的变量, 均为静态存储, 作用域是定义处到源文件结束处, 可以使用 extern 和 static 对其作用域进行扩展和限制
- extern 是扩展
#include <iostream>
using namespace std;
int min(int a, int b) //定义min函数
{
int c;
c = a < b ? a : b;
return (c);
}
extern int a, b;
int main()
{
cout<<min(a, b)<<endl;
}
int a = 4, b = 7;
- static 是限制
有时候可能需要限制某些全局变量,不让别的文件使用, 可以用static做限制
字符数组
字符数组与字符串区别
- 字符串一定是一个char的数组,但char的数组未必是字符串;
- 数字0(和字符‘\0’等价)结尾的char数组就是一个字符串,但如果char数组没有以数字0结尾,
#include <iostream>
#include <string>
using namespace std;
int main (){
// 字符数组与字符串区别
// 字符串一定是一个char的数组,但char的数组未必是字符串;
// 数字0(和字符‘\0’等价)结尾的char数组就是一个字符串,但如果char数组没有以数字0结尾
// 那么就不是一个字符串,只是普通字符数组,所以字符串是一种特殊的char的数组
char c1[] = {'c', 'p', 'b'};
char a = 'b';
char c[] = "a";
int arr[] = {1, 2, 5};
cout<< "c1 = "<< c1 <<endl;
cout<< "arr = "<< arr <<endl;
char c2[] = {'c', 'p', 'b', 0};
cout<< "c2 = "<< c2 <<endl;
char c3[] = {'c', 'p', 'b', '\0'};
cout<< "c3 = "<< c3 <<endl;
char c4[] = {'c', 'p', 'b', 'a', 'q', 0};
cout<< "c4 = "<< c4 <<endl;
char c5[] = "abcdef";
cout<< "c5 = "<< c5 <<endl;
char c6[100] = {0};
cout<< "c6 = "<< c6 <<endl;
// char c7[2] = {'a', 'b', 0}; 越界
// cout<< "c7 = "<< c7 <<endl;
char c7[50] = {'1', 'a', 'b', '0', '7', 0};
cout<< "c7 = "<< c7 <<endl;
char c8[50] = {'1', 'a', 'b', 0, '7'};
cout<< "c8 = "<< c8 <<endl;
char c9[50] = {'1', 'a', 'b', '\0', '7'};
cout<< "c9 = "<< c9 <<endl;
string s = "aaaaa";
cout<< s <<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{ char a = 'a';
char c[15] = { 'I', ' ', 'a', 'm', ' ', 'a', ' ', 's', 't', 'u', 'd', 'e', 'n', 't', '.'};
for (int i = 0; i < 15 ; ++i) {
cout<< (int)c[i]<< " ";
}
cout<<endl;
cout<<"a = "<< (int)a << endl; // 97 65
}
C++规定以字符’\0’作为字符串结束标志, ‘\0’的ASCII 值是0, 称为空字符, 但是字符数组并不要求必须以’\0’作为结束标志, 可以没有, 下面都是合法的.
char c1[5] = {'G', 'o', 'o', 'd', '!'};
char c2[] = {"Good!"};
char c3[] = "Good!";
char c4[] = {'G', 'o', 'o', 'd', '!', '\0'};
编译预处理
// 求球体的表面积及体积
// 带参数的宏定义示例
#include <iostream>
using namespace std;
#define PI 3.1415926
int main( )
{
double r, s, v;
cout<<"input radius :";
cin>>r; //输入半径
s=4*PI*r*r;
v=4.0/3*PI*r*r*r;
cout<<"\A = "<<s<<"\n V = "<<v<<'\n';
}
宏定义里面可以层层运算
#include <iostream>
using namespace std;
#define R 5.0
#define PI 3.1415926
#define L 2*PI*R
#define S PI*R*R
int main( )
{
cout<<"L="<<L<<"\nS="<<S<<endl;
}
条件编译
LETTER 0 执行下面的, 其他执行上面的(true or false)
使之能将字母全改成大写输出或全改成小写输出。*/
#include <iostream>
using namespace std;
#define LETTER 6
int main( )
{
char c;
do
{
cin.get(c);// 输入一个字符然后给c
#if LETTER
if (c>='a'&& c<='z')
c=c-32;
#else
if (c>='A'&&c<='Z')
c=c+32;
#endif
cout<<c;
}
while (c!='\n');
}
枚举(enum)
// 枚举
#include <iostream>
using namespace std;
# define PI 3.14
enum color {RED=1, YELLOW, BLUE};
void print(enum color c){
switch (c) {
case RED:
cout<< "红色" <<endl;
break;
case YELLOW:
cout<< "黄色" <<endl;
break;
case BLUE:
cout<< "蓝色" <<endl;
break;
}
}
int main (){
color c1, c2;
c1 = RED;
print(c1);
return 0;
}