数组
int nums[5] = {1, 2, 3, 4, 5};
printf("%d\n", nums[0]); // 1
for (int i = 0; i < 5; i++) {
printf("%d ", nums[i]);
}
数组下标从 0 开始,注意越界访问——C 不会检查,越界可能造成难以排查的错误。
字符串
#include <string.h>
char s[20] = "hello";
printf("%zu\n", strlen(s)); // 5
strcat(s, " world");
printf("%s\n", s); // hello world
字符串本质是字符数组,以 '\0' 结尾。常用函数:strlen、strcpy、strcat、strcmp。