局部变量与全局变量
变量的作用域和生命周期
作用域
限定一段程序代码中用到的名字 的可用的代码范围就是这个名字的作用域
int b = 50; //全局变量
int main()
{
int a = 10; //局部变量
{
int a = 20;
}
printf("a=%d", a);
printf("b=%d\n", b);
system("pause");
}
a=10b=50
请按任意键继续. . .
在同一个工程的其他文件调用这个文件内的全局变量时,需要用
extern int g_val
声明此变量
生命周期
局部变量的生命周期:进入作用域生命周期开始,出作用域生命周期结束
全局变量的生命周期是整个程序的生命周期
生命周期是变量的创建和销毁之间的时间段
定义常量的方法
常量的分类
字面常量
const修饰的常变量
int main()
{
int num = 10;
const int a = 20;
num = 15;
// a = 25; //此行会报错
printf("num=%d\t",num);
printf("a=%d\t",a);
system("pause");
}
num=15 a=20 请按任意键继续. . .
const修饰变量后,变量不能改变.因此叫做"常变量".其本质是个变量,被const修饰之后不能改变.
#define定义的标识符常量
#define anumber 10000
int main()
{
// #define定义的标识符常量
int n = anumber ;
printf("n = %d\n",n);
system("pause");
}
n = 10000
请按任意键继续. . .
枚举常量
enum sex //枚举
{
male,
female,
secret //枚举的未来可能取值
};
int main()
{
// 枚举常量:可以一一列举的常量
enum sex s = male; //定义变量只能在枚举可能值里面选择
printf("%d\n",male);
printf("%d\n",female);
printf("%d\n",secret);
system("pause");
}
0
1
2
请按任意键继续. . .
枚举常量的默认值是从0开始的,可以通过"赋初值"的方式进行修改
字符串,转义字符,注释
字符串
"Hello.\n"
通过这种创引号引起的一串字符串称为"字符串字面值(string literal)",或者简称为:字符串
字符串的结束标志是一个"\0"的转义字符,在计算字符串长度的时候"\0"算是结束的标志,不算做字符串的内容
转义字符
int main()
{
printf("\\,\t,\\t");
system("pause");
}
\, ,\t请按任意键继续. . .
选择,循环语句
选择语句的例子
int main()
{
SetConsoleOutputCP(65001);
int input = 0;
printf("选择输入范例,1/0>");
scanf("%d",&input);
if(input==1)
printf("输入的是1\n");
else
printf("输入的是0\n");
system("pause");
}
选择输入范例,1/0>1
输入的是1
Press any key to continue . . .
选择输入范例,1/0>0
输入的是0
Press any key to continue . . .
循环语句的例子
int main()
{
SetConsoleOutputCP(65001);
int sum = 1;
int i = 0;
while(sum<100)
{
printf("这是第%d行代码\t",sum);
sum++;
i++;
if(i%5==0)
printf("\n");
}
system("pause");
}
这是第1行代码 这是第2行代码 这是第3行代码 这是第4行代码 这是第5行代码
......(省略一部分)
这是第96行代码 这是第97行代码 这是第98行代码 这是第99行代码 Press any key to continue . . .
函数,数组
函数的例子
int Add(int x,int y)
{
int z = 0;
z = x + y;
return z;
}
int main()
{
int num1 = 0;
int num2 = 0;
printf("please input num1 and num2:");
scanf("%d%d", &num1, &num2);
int sum =Add(num1,num2);
printf("%d\n",sum);
system("pause");
}
please input num1 and num2:2 7
9
请按任意键继续. . .
数组的例子
int main()
{
int i = 0;
int array[10] = {0,1,2,3,4,5,6,7,8,9};
char ch[5] = {'a','b','c'}; //不完全初始化,剩下的都是"0"
while(i<10)
{
printf("%d\t" , array[i]);
i++;
}
int i = 0;
}
1 2 3 4 5 6 7 8 9 请按任意键继续. . .
操作符
算术操作符 | + - * / % |
移位操作符(二进制位) | >> << |
位操作符 | & ^ | |
赋值操作符 | = += -= *= /= &= ^= |= >>= <<= |
单目操作符 | ! - + &(取地址) sizeof ~(二进制位取反) -- ++ * (类型) |
逻辑操作符 | && 逻辑与 || 逻辑或 |
条件操作符 | exp1 ? exp2 : exp3 :1成立,则2计算,否则3计算 |
逗号表达式 |
int main()
{
SetConsoleOutputCP(65001);
int a = 0;
int b = 1;
int c = 2;
int d = 3;
int e = 4;
int f = ( a=b+2,c=a-4,b=c+2);//逗号表达式,从左向右进行计算
printf("按位取反:~:%d\n",~a); //按位取反
printf("前置++:%d\n",++b); //前置++
printf("三元操作符:%d\n", c > d ? c : d );//三元操作符
printf("逗号表达式:%d\n",f );//逗号表达式
system("pause");
}
按位取反:~:-4
前置++:2
三元操作符:3
逗号表达式:1
Press any key to continue . . .. . .
关键字
c语言提供的关键字,其不可以自行创建.关键字不能做变量名
auto 每个局部变量都是由此修饰的,自动创建自动销毁的"自动变量"
break 循环里面用来跳出的
case switch......case......语句使用的关键字
char 字符类型
const 修饰常变量
continue 类似break
default 默认的意思,也是在switch......case......语句
do 用在一个循环里面
double 双精度浮点数
else 用在if......else......语句中
enum 枚举
extern 声明外部符号的用途
float 单精度浮点型
for for循环
goto 用于goto语句
if 用于if语句
int 表示整形
long 表示长整形
register 寄存器关键字,用它生成的变量建议放在寄存器里面
return 返回
short 短整形
signed 有符号的;数字是有正有负的,signed表示有符号的,即有正负的
unsigned 无符号的
sizeof 求大小长度的量
static 静态的
struct 结构
switch 用在switch......case......语句中
typedef 类型的定义
union 联合体,共用体
void 空
volatile 读不懂
while while循环
常见关键字
register
register int num = 100//建议将num变量放在寄存器里
计算机中,数据可以存储到''寄存器/高速缓存/内存/硬盘''上,其中寄存器的读取速度最快容量最小.大量频繁被使用到的数据,我们就可以放置在寄存器中.提升处理速度
typedef
类型重定义
typedef unsigned int u_int;
int main()
{
unsigned int num1 = 100;
u_int num2 = 100; //两者作用效果一样
}
static
静态的
void test()
{
int a = 1;
a++;
printf("%d ",a);
}
int main()
{
int i = 0;
while(i<10)
{
test();
i++;
}
system("pause");
}
2 2 2 2 2 2 2 2 2 2 请按任意键继续. . .
void test()
{
static int a = 1;
a++;
printf("%d ",a);
}
int main()
{
int i = 0;
while(i<10)
{
test();
i++;
}
system("pause");
}
2 3 4 5 6 7 8 9 10 11 请按任意键继续. . .
static修饰局部变量的时候,a变量生成后就不销毁了,因此a就不断留下来,不断+1.
本质是其改变了变量的存储类型,从而改变了变量的生命周期.静态变量和全局变量的生命周期是一样的,与程序的生命周期是一样的
static修饰全局变量,就可以限制全局变量仅仅在本源文件里面使用,全局变量的外部链接属性变成了内部连接属性
define
定义宏
#define ADD(x,y) ((x)+(y))
int main()
{
printf("%d\n",4*ADD(2,3));
system("pause");
}
20
请按任意键继续. . .