初识C语言(2)
作用域和生命周期
1.作用域:即一段程序中所用到的名字并不总是有效/可用的,而限定这个名字的可用性的代码范围就是这个名字的作用域。
2.生命周期:变量的创建到变量的销毁之间的一个时间段。
常量
一共分为4个:字面常量、const修饰的常量、#define定义的标识符常量、枚举常量。
下面展示一些 代码。
#include<stdio.h>
enum sex
{
male,
female,
secret
};
//括号中的这三个元素为枚举常量
#include<stdio.h>
int main()
{
3.14;
5;
return 0;
}
//括号里的为字面常量(在遇到实际问题时几乎不用,不常见)
#include<stdio.h>
int main()
{
const float a=1.253; //const 修饰的常量
#define min 2 //#define的标识符常量
注:标识符常量即可以写在括号里又可以写在括号外;
const 修饰的常量 具有常属性(不能被改变的属性)
return 0;
}
如下 const 修饰的常量 具有常属性(不能被改变的属性)
#include<stdio.h>
int main()
{
const int a=10;
int arr1[a]={0,1};
return 0;
}
运行结果 报错:int arr1[a]={0,1};【】里必须是常量,常变量不可以。
字符串
定义:由“”引起来的一串字符串称为字符串面值或字符串
重点:字符串的结束标志是一个\0的转义字符;在计算字符串长度时\0不算进长度。
转义字符(出题时很容易掉入陷阱)
着重强调:?, \t, \ddd(ddd表示1-3个8进制的数字 切记8进制0到7), \xdd(dd表示2个16进制数字), \n,\
列题:
#include<stdio.h>
int main()
{
printf("%d\n",strlen("c:\test\328\test.c"));
return 0;
}
运行结果为:14
注释
1./xxxxxx/ 注:不能嵌套注释,会报错
2.//xxxx