编程学习 /C 语言 /进阶提高 /数组与字符串
🅲 C 语言 进阶提高

数组与字符串

一维数组、字符数组与常用字符串函数。

📅 2026-11-23 👁 0 次阅读 🔗 参考 C 语言标准教程
数组与字符串示意图

数组

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。