//类型修饰符
数组名[数组元素个数] = {初始化};
//定义数组的时候[]中必须是常量表达式,
不可以是变量;
/*
int age[5] = {21, 18, 25, 20, 18};
int array[10] = {0};//代表数组中有10个元素, 每个都是0;
int array1[8] = {1};//代表数组中有8个元素,第一个是1,其余的为0;
int age2[2 + 3] = {0};
*/
//定义数组和使用数组的最大区别是,
前面是否有类型修饰符
//使用数组元素[]中可以是变量,
数组元素下标是从0开始到数组元素个数-1;
/*
int age[5] = {21, 18, 25, 20, 18};
printf("%d\n", age[1]);
age[1] = 23;
printf("%d\n", age[1]);
int i = 0;//使用数组可以用变量.
age[i] = 16;
printf("%d\n", age[i]);
//sizeof( )是一个运算符, 用来测算一个变量或者一个数据类型所占的内存空间大小
//sizeof( )用%lu来输出.得到的值是无符号长整型;
//数组所占的内存空间 = 数组类型 * 元素个数;
printf("%lu\n", sizeof(age[i]));
*/
/**
*
int a[3] = {1, 2, 3};
float b[2] = {1.0, 2.0};
double c[] = {3.23344459};
printf("%lu %lu %lu", sizeof(a), sizeof(b),
sizeof(c));
*/
/*
打印数组
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d\t", a[i]);
}
*/
/*
//数组各个元素的和
int b[10] = {0};
int sum = 0;
for (int i = 0; i < 10; i++) {
b[i] = arc4random() % (70 - 30 + 1) +
30;
printf("%d\t", b[i]);
sum += b[i];
}
printf("\n%d\n", sum);
//数组复制
int c[10] = {0};
for (int i = 0; i < 10; i++) {
c[i] = b[i];
printf("%d\t", c[i]);
}
printf("\n");
//数组相加
int d[10] = {0};
for (int i = 0; i < 10; i++) {
d[i] = b[i] + c[i];
printf("%d\t", d[i]);
}
*/
/*
int a[10] = {0};
int b[10] = {0};
int c[10] = {0};
for (int i = 0; i < 10; i++) {
a[i] = arc4random() % (40 - 20 + 1) +
20;
printf("%d\t", a[i]);
}
printf("\n");
for (int i = 0; i < 10; i++) {
b[i] = arc4random() % (40 - 20 + 1) +
20;
printf("%d\t", b[i]);
}
printf("\n");
for (int i = 0; i < 10; i++) {
c[i] = a[i] + b[i];
printf("%d\t", c[i]);
}
*/
/*
int a[10] = {0};
for (int i = 0; i < 10; i++) {
a[i] = arc4random() % 101;
printf("%d\t", a[i]);
}
printf("\n");
//升序排列
//外层循环用来控制比较趟数
for (int i = 0; i < 10 - 1; i++) {
//内层循环用来控制每趟的比较次数(两两进行比较)
for (int j = 0; j < 10 - 1 - i;
j++) {
//如果前面的数大于后面的, 则进行交换
if (a[j] > a[j + 1])
{
int temp =
a[j];
a[j] = a[j
+ 1];
a[j + 1] =
temp;
}
}
}
for (int i = 0; i < 10; i++) {
printf("%d\t", a[i]);
}
printf("\n");
//降序排列
for (int i = 0; i < 10 - 1; i++) {
for (int j = 0; j < 10 - 1 - i;
j++) {
if (a[j] < a[j + 1])
{
int temp =
a[j];
a[j] = a[j
+ 1];
a[j + 1] =
temp;
}
}
}
for (int i = 0; i < 10; i++) {
printf("%d\t", a[i]);
}
*/
/*
//字符数组有10个元素, 后面的都是0;
char str[10] = {‘i‘, ‘P‘, ‘h‘, ‘o‘, ‘n‘, ‘e‘};
//字符数组有7个元素
char s[] = {‘A‘, ‘n‘, ‘d‘, ‘r‘, ‘o‘, ‘i‘, ‘d‘};
printf("%lu %lu\n", sizeof(str), sizeof(s));
//下面的定义方式, 系统会自动帮我们补一个‘\0‘,‘\0‘用来标记字符串的结束
char str1[10] = "iPhone";
char s1[] = "Android";
//char s1[7] = "Android";如果我们要写上数组元素个数,
那么元素个数,必须是字符个数 + 1;
//char s1[8] = "Android";这样写.
printf("%c\n",s1[7]);
printf("%lu %lu\n", sizeof(str1), sizeof(s1));
*/
/*
//字符数组也叫做字符串
char str[6] = "iPhone";
//%s, 输出整个字符串, 输出表列里面用数组名
printf("%s", str);
*/
/*
char str[10] = "iPhone";
str[3] = ‘\0‘;
printf("%s", str);//str遇到\0结束.
printf("\n%c", str[4]);
*/
/*strlen( ):
char str[10] = "iPhone";
str[4] = ‘\0‘;
//strlen( )是一个函数
//用来计算一个字符串的长度(字符个数)
//从第一个字符开始,到‘\0‘结束
printf("%lu", strlen(str));
*/
/*strcpy( ):
//字符串拷贝函数
//逐个拷贝每个字符, 到‘\0‘结束, 也会把‘\0‘拷贝过去
//被拷入的字符串, 内存空间要足够的大.
char str[10] = "iPhone";
char str1[100] = "aaaaaaaaaa";
strcpy(str1, str);
printf("%s", str1);
*/
/*
char s[] = {‘A‘, ‘n‘, ‘d‘, ‘r‘, ‘o‘, ‘i‘, ‘d‘};
// char s[10] = {‘A‘, ‘n‘, ‘d‘, ‘r‘,
‘o‘, ‘i‘, ‘d‘};
char a[100] = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
strcpy(a, s);
printf("%s", a);
*/
/*strcat( ):
char str[10] = "IOS";
char str1[100] = "swift";
//字符串拼接, 从str1开始逐个拷贝str中的字符, 拷到‘\0‘结束
//被拼入得字符串, 内存要足够大.
strcat(str1, str);
printf("%s", str1);
*/
/*
char str[10] = "abc";
char str1[10] = "fbc";
//strcmp逐个比较字符的ASCII码的差值, 如果不为0.说明不相等, 如果差值是正数,
前面大于后面的.
//如果都是0,则比较到\0结束.
int result = strcmp(str, str1);
printf("%d", result);
*/
/*
char str[] = "io geo";
int i = 0;
while (str[i] != ‘\0‘) {
i++;
}
printf("%d", i);
*/
/*
char str[] = "I love IOS, i want an iPhone";
int i = 0;
int count = 0;
while (str[i] != ‘\0‘) {
i++;
if (str[i] == ‘ ‘) {
count++;
}
}
printf("%d\n", i);
printf("%d", count);
*/
C语言零基础项目驱动式学习第四天,布布扣,bubuko.com