00array_define.c
#include <stdio.h>
int main(void)
{
// 数组和指针都是复合类型, 它们从另外一种基础类型(基本类型, 结构类型)派生.
// 定义数组时,
// 1. 确定数组元素的类型.
// 2. 显式或隐式的确定数组的元素个数.
// 这里的 '[]' 是声明说明符号.
int ary[5];
int a[] = { 36, 48, 12, 6, -3 };
int i = 0;
printf("sizeof ary = %u, sizeof ary[0] = %u\n", sizeof(ary), sizeof(ary[0]));
printf("sizeof ary / sizeof ary[0] = %u\n", sizeof(ary) / sizeof(ary[0]));
printf("----------------------------------\n");
// 这里的 '[]' 是运算符, 索引访问运算符号.
for (i = 0; i < 5; i++)
printf("a[%d] = %d\n", i, a[i]);
// 越界 越界产生不确定的运行行为.
// a[5] = 4;
return 0;
}
运行结果:
sizeof ary = 20, sizeof ary[0] = 4
sizeof ary / sizeof ary[0] = 5
----------------------------------
a[0] = 36
a[1] = 48
a[2] = 12
a[3] = 6
a[4] = -3
01array_name.c
#include <stdio.h>
int main(void)
{
int a[5] = { 36, 48, 12, 18, };
int b[5];
// 数组名代表数组首元素地址. 数组名是一个常量值.
// b = a; // 非法.
printf("a = %p, &a[0] = %p\n", a, &a[0]);
printf("a = %p, &a[1] = %p\n", a, &a[1]);
return 0;
}
运行结果:
a = 0xbfaea908, &a[0] = 0xbfaea908
a = 0xbfaea908, &a[1] = 0xbfaea90c
02array_arg.c
#include <stdio.h>
// 当数组作为函数参数时, 退化为一个指针.
// 即当作为函数参数时, 如下形参等价.
// --> int a[10] == int a[] == int *a
void test(int a[10])
{
printf("in test, sizeof a = %u\n", sizeof(a));
}
void rand_a(int a[], int len)
{
int i = 0;
for (i = 0; i < len; i++)
a[i] = rand() % 100;
}
void print_a(int *a, int len)
{
int i = 0;
for (i = 0; i < len; i++)
printf("%d ", a[i]);
putchar('\n');
}
int main(void)
{
int a[10] = {0};
printf("in main, sizeof a = %u\n", sizeof(a));
printf("---------------------\n");
test(a);
printf("=====================\n");
rand_a(a, sizeof(a) / sizeof(a[0]));
print_a(a, sizeof(a) / sizeof(a[0]));
return 0;
}
运行结果:
in main, sizeof a = 40
---------------------
in test, sizeof a = 4
=====================
83 86 77 15 93 35 86 92 49 21
03c99_define.c
#include <stdio.h>
int main(void)
{
// C99
int a[] = { [0] = 32, [3] = 45, [4] = 16 };
int i = 0;
for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
printf("a[%d] = %d\n", i, a[i]);
return 0;
}
运行结果:
a[0] = 32
a[1] = 0
a[2] = 0
a[3] = 45
a[4] = 16
04array_operation.c
#include <stdio.h>
void rand_a(int a[], int len)
{
int i = 0;
for (i = 0; i < len; i++)
a[i] = rand() % 100;
}
void print_a(int a[], int len)
{
int i = 0;
for (i = 0; i < len; i++)
printf("%d ", a[i]);
putchar('\n');
}
int max(int a[], int len)
{
int i = 0, max = a[0];
for (i = 0; i < len; i++)
{
if (a[i] > max)
max = a[i];
}
return max;
}
int min(int a[], int len)
{
int i = 0, min = a[0];
for (i = 0; i < len; i++)
{
if (a[i] < min)
min = a[i];
}
return min;
}
void reverse(int a[], int len)
{
int i = 0;
for (i = 0; i < len / 2; i++)
{
a[i] ^= a[len-i-1];
a[len-i-1] ^= a[i];
a[i] ^= a[len-i-1];
}
}
void bubble_sort(int a[], int len)
{
int i = 0, j = 0;
for (i = 0; i < len - 1; i++)
{
for (j = 0; j < len - i - 1; j++)
{
if (a[j] > a[j+1])
{
a[j] ^= a[j+1];
a[j+1] ^= a[j];
a[j] ^= a[j+1];
}
}
}
}
int main(void)
{
int a[10] = {0};
rand_a(a, 10);
print_a(a, 10);
printf("--------------------\n");
printf("max = %d\n", max(a, 10));
printf("min = %d\n", min(a, 10));
printf("--------------------\n");
printf("reverse:\n");
reverse(a, 10);
print_a(a, 10);
printf("--------------------\n");
printf("sort:\n");
bubble_sort(a, 10);
print_a(a, 10);
return 0;
}
运行结果:
83 86 77 15 93 35 86 92 49 21
--------------------
max = 93
min = 15
--------------------
reverse:
21 49 92 86 35 93 15 77 86 83
--------------------
sort:
15 21 35 49 77 83 86 86 92 93
05char_array.c
#include <stdio.h>
#include <string.h>
int main(void)
{
// char str[] = {'c', 'h', 'i', 'n', 'a', ' ', 'u', 'n', 'i', 'x', '\0' };
char str[] = "china unix";
// 在内存中, 默认字符串以 '\0' 结束. 即 '\0' 是字符串结束的标志.
// '\0' 结尾字符 NUL字符 空字符 ascii编码为 0
// '0' 0字符 ascii编码为 48
// sizeof 是一个运算符, 它在编译期求值(数组的元素个数).
printf("sizeof str = %u\n", sizeof(str));
// strlen 是库函数, 它在运行期求值(不包含'\0'的字符个数).
printf("length of str = %u\n", strlen(str));
printf("------------------------\n");
printf("sizeof \"china unix\" = %u\n", sizeof "china unix"); // 要用 \ 来显示双引号
printf("length of \"china unix\" = %u\n", strlen("china unix"));
printf("========================\n");
// s 空间放不下 '\0', 错误.
char s[10] = "china unix";
// s1 空间足够放下字符串. 没有赋 '\0'.
// 给了数组初始化式后, 未初始化的部分自动赋0(就是'\0')
// 如下初始化式是正确的.
char s1[10] = { 'C', 'H', 'I', 'N', 'A' };
printf("s[10] = %s\n", s); // 会把s1也给打印出来, 因为s1在栈中是放在了s之后的, 且s1有尾零
return 0;
}
运行结果:
sizeof str = 11
length of str = 10
------------------------
sizeof "china unix" = 11
length of "china unix" = 10
========================
s[10] = china unixCHINA
06out_of_range.c
#include <stdio.h>
int main(void)
{
int b = 25;
int a[5] = { 1, 2, 3, 4, 10 };
int i = 0;
// 屏蔽与不屏蔽的运行结果不一样
printf("&b = %p\n", &b);
printf("a = %p\n", a);
printf("&i = %p\n", &i);
// i == 5 时越界了.
for (i = 0; i < 6; i++)
printf("a[%d] = %d\n", i, a[i]);
a[5] = 36; // 越界
// 这是一种巧合.
printf("b = %d\n", b);
return 0;
}
运行结果(屏蔽掉3句打印地址值的printf):
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 10
a[5] = 5 --> 随机值
b = 25
运行结果(不屏蔽3句打印地址值的printf):
&b = 0xbfe0aca8
a = 0xbfe0ac94
&i = 0xbfe0acac
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 10
a[5] = 25 --> b的值
b = 36 --> b被修改了
07infinite_loop.c
#include <stdio.h>
int main(void)
{
int a[5] = { 12, 34, 56, 78, 90 };
int i = 0;
printf("a = %p\n", a);
printf("&i = %p\n", &i);
// 越界访问导致无限循环(这是一种巧合).
for (i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
a[5] = 2; // 越界, 相当于 i = 2;
sleep(1);
}
return 0;
}
运行结果:
a = 0xbf9eeef8
&i = 0xbf9eef0c
12
78
78
78
78
78
.
.
.
08puts.c
#include <stdio.h>
int main(void)
{
char s1[10] = "helloworld"; // 没有尾零
char s2[] = "HELLO"; // 有尾零
// 输出到 '\0' 停止, 自动加换行. 等价于 printf("%s\n", s1);
puts(s1);
printf("--------------\n");
puts(s2);
return 0;
}
运行结果:
helloworldHELLO
--------------
HELLO
09gets.c
#include <stdio.h>
int main(void)
{
char s[1024] = {0};
printf("pls input a string: ");
gets(s); // scanf("%s", s);
printf("-----------\n");
printf("you inputed: ");
puts(s); // printf("%s\n", s);
return 0;
}
10my_atoi.c
#include <stdio.h>
int judge_format(char s[])
{
int i = 0;
while (s[i] != '\0')
{
if (!('0' <= s[i] && s[i] <= '9'))
break;
i++;
}
if (s[i] == '\0') // 表示已到尽头
return 1;
else
return 0;
}
unsigned int my_atoi(char s[])
{
int i = 0;
unsigned int n = 0;
while (s[i] != '\0')
{
n *= 10;
n += s[i++] - '0';
}
return n;
}
int main(void)
{
char s[128] = {0};
unsigned int a = 0;
retry:
printf("pls input a string: ");
gets(s);
if (judge_format(s))
{
a = my_atoi(s);
printf("a = %u\n", a);
}
else
{
printf("pls input a integer.\n");
goto retry;
}
return 0;
}
exercise1.c
#include <stdio.h>
#include <string.h>
int insert_char(char s[], int len, int index, char ch)// 指定下标插入
{
int i = 0, s_len = strlen(s);
if((s_len + 1) >= len) // 包括将要插入的字符在内, 是否大于等于len, 等于的话, 尾零就放不下了
{
printf("input string too long!\n");
return -1;
}
if(index < 0 || index > s_len)
{
printf("input wrong index!\n");
return -1;
}
i = s_len;
while(i >= index)
{
s[i+1] = s[i]; // 往后挪一位
i--;
}
s[index] = ch;
return 0;
}
int main(void)
{
char s[128] = {0};
char ch = '0';
int index = 0, ret = 0;
printf("input a string: ");
gets(s);
retry:
printf("input a index to insert your char: ");
ret = scanf("%d", &index);
// 清空输入缓冲区.
while(getchar() != '\n')
;
if(ret == 0)
{
printf("input wrong format, pls input again.\n");
goto retry;
}
printf("input the character you wanna insert: ");
ch = getchar();
if(0 == insert_char(s, sizeof(s)/sizeof(s[0]), index, ch))
{
printf("insert succeed!\n");
puts(s);
}
else
{
printf("insert failed!\n");
}
return 0;
}
往字符串中插入字符
运行结果:
input a string: abcdefghjiklmn
input a index to insert your char: 5
input the character you wanna insert: W
insert succeed!
abcdeWfghjiklmn
exercise2.c
#include <stdio.h>
int my_strlen(const char src[]) // 获取字符串长度
{
int len = 0;
while(src[len++] != '\0')
;
return len - 1;
}
void my_strcpy(char dest[], const char src[]) // 拷贝字符串
{
int i = 0;
while(src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0'; // 最后别忘了再赋一个尾零
}
void my_strcat(char dest[], const char src[]) // 在一个字符串的结尾追加另一个字符串
{
int d_len = my_strlen(dest);
my_strcpy(dest + d_len, src); // dest 先偏移 d_len 个单位, 再传给 my_strcpy
}
int my_strcmp(const char s1[], const char s2[]) // 比较两个字符串
{
int i = 0;
while((s1[i] != '\0') && (s2[i] != '\0'))
{
if(s1[i] != s2[i])
break;
i++;
}
return (s1[i] - s2[i]);
}
char *my_strchr(char *src, int ch) // 在src中查找第一次c出现的位置, 并返回该位置的指针
{
int i = 0;
while(src[i] != '\0')
{
if(src[i] == ch)
return (src+i);
i++;
}
return NULL;
}
char *my_strstr(char parent[], char child[]) // 在parent中查找第一次child出现的位置, 并返回该位置的指针
{
int i = 0, j = 0, start_pos = 0;
for(i = 0; parent[i] != '\0'; i++)
{
if(parent[i] == child[0]) // 和 child 的首字符相等了再进来继续判断
{
start_pos = i;
for(j = 1; child[j] != '\0'; j++) // j 从 1 开始
{
if(parent[++start_pos] != child[j])
break;
}
if(child[j] == '\0')
return (parent+i); // i 保存的就是起始位置
}
}
return NULL;
}
int main(void)
{
char dest[1024] = {0}, src[1024] = {0};
int ret = 0;
char *str = NULL;
printf("please input a string: ");
gets(src);
printf("len = %d\n", my_strlen(src));
printf("------------------\n");
printf("src = %s\n", src);
my_strcpy(dest, src);
printf("dest = %s\n", dest);
printf("------------------\n");
my_strcat(dest, src);
printf("dest = %s\n", dest);
printf("------------------\n");
ret = my_strcmp(dest, src);
if(ret == 0)
printf("dest == src\n");
if(ret > 0)
printf("dest > src\n");
if(ret < 0)
printf("dest < src\n");
printf("------------------\n");
str = my_strchr(dest, '5');
printf("str = %s\n", str);
printf("------------------\n");
str = my_strstr(dest, "abcd");
printf("str = %s\n", str);
return 0;
}
实现 my_strlen, my_strcpy, my_strcat, my_strcmp, my_strchr, my_strstr
运行结果:
please input a string: 0123456789abcde
len = 15
------------------
src = 0123456789abcde
dest = 0123456789abcde
------------------
dest = 0123456789abcde0123456789abcde
------------------
dest > src
------------------
str = 56789abcde0123456789abcde
------------------
str = abcde0123456789abcde
exercise3.c
#include <stdio.h>
#include <string.h>
void reverse(char s[], int len)
{
int i;
for (i = 0; i < len/2; i++)
{
s[i] ^= s[len-i-1];
s[len-i-1] ^= s[i];
s[i] ^= s[len-i-1];
}
}
void add(char s1[], char s2[], char sum[])
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int len_max = len1 > len2 ? len1 : len2;
// 为了更方便的运用数组操作, 需要倒置一下字符串
reverse(s1, len1);
reverse(s2, len2);
int i = 0, digit = 0, carry = 0;
for (i = 0, digit = 0; i < len_max; i++, digit = 0)
{
if (s1[i] != '\0')
digit += s1[i] - '0';
if (s2[i] != '\0')
digit += s2[i] - '0';
digit += sum[i]; // 加上前一次可能的进位.
sum[i] = digit % 10;
carry = digit / 10;
if (carry != 0)
sum[i + 1] = carry;
sum[i] += '0'; // 转成字符
}
if (sum[i] != 0)
sum[i] += '0';
reverse(sum, strlen(sum)); // 最后再逆转回来
reverse(s1, len1);
reverse(s2, len2);
}
int main(void)
{
char s1[128] = {0}, s2[128] = {0}, sum[128] = {0};
printf("input s1: ");
gets(s1);
printf("input s2: ");
gets(s2);
add(s1, s2, sum);
printf("%s + %s = %s\n", s1, s2, sum);
return 0;
}
字符串加法, 如: "123" + "999" = "1122"
实现大数据的相加.
运行结果:
input s1: 658495129756984566587566458
input s2: 498956238486532169463235544
658495129756984566587566458 + 498956238486532169463235544 = 1157451368243516736050802002