1.字符数组
c1[10] = {'h','e','l','l','o'}; //无\0
c1[10] = "hello"; //有\0
测量字符串长度strlen(c1) = 5
字符数组与字符串的区别
#include <stdio.h>
#include <string.h>
int main()
{
char a[] = "hello";
char* b = "hello";
printf("sizeof : %d %d\n", sizeof(a), sizeof(b));
printf("strlen : %d %d\n", strlen(a), strlen(b));
return 0;
}
结果:
sizeof : 6 4
strlen : 5 5
综上:
sizeof 测量字符数组的时候即长度加\0所以显示6位,而字符串则测得的是地址长度,32位系统默认地址长度4
strlen 测量字符数组即显示长度,不加\0,所以5位,字符串也是。
scanf后面的输入一定要有空间的
2.strcat(str1,str2) 衔接
将第二个字符串接到第一个字符串后并以第一个字符串的地址输出(
需要考虑str1的大小,一定要足够放最后的结果,否则将会产生越界)
可以使用下述句子确保不越界
if(strlen(str1) + strlen(str2) + 1 <= sizeof(str1))
3.strcpy(str1,str2) 复制
将字符串2复制到字符串1中。
strncpy(a,b,2)
将字符串b中前两个字符复制到字符串a中。
错误:a1 = b1 字符串不能这样等于,因为字符串空间不能改变
4.strcmp(s1,s2) 比较大小
strncmp(s1,s2,n) 比较前n个大小
5.预处理
- #开头
- 独占一行
- 不加
6.int a = 10;
int不光是数据型也是关键字
7.const int a = 10;
a只读不能修改
const int *p1 = &a;
保护了*p1,及内容而非指针地址,地址可变
int const *p2 = &a;
保护了*p1,及内容而非指针地址,地址可变
int * const p3 = &a;
保护了p3,及地址
int const * const p4 = &a;
既保护了p4又保护了*p4,及内容和地址都保护了
8.static
- 修饰局部变量:只在初运行时初始化一次且只有一次
- 修改全局变量:将作用范围由全工程可见下降到本文本可见
9.extern表示当前变量为外部变量(告诉编译器到别处寻找该变量)