一、数据类型
整型(int) | 内存:占4个字节 |
字符类型(char) | 内存:占1个字节 = 8bits |
长整型(long) | 内存:占8个字节 |
浮点型(单精度)(float) | 内存:占4个字节 |
浮点型(单精度)(double) | 内存:占8个字节 |
布尔类型(非0即1)(bool) | 内存:占1个字节 布尔类型只要不是0,输出都是1 |
指针类型(char *) | 内存:占8个字节 |
二、循环
1.if循环
if (money < 2000) //判断条件:金额是否小于2000
{
printf("get redmi phone\n"); //如果小于2000,输出结果
}
else if (2000 < money && money < 4000) //判断条件:金额是否大于2000,小于4000
/*&&(且):两边判断语句都成立时为1,其余为0*/
{
printf("get oppo phone\n"); //如果大于2000,小于4000,输出结果
}
else //判断条件:金额是否大于4000
{
printf("get yaoyaolingxian phone\n"); //如果大于4000,输出结果
}
2、while循环
while (true) // 循环条件
{
totalSum = totalSum + idx; // 计算总和
idx++; // 索引加1
if (idx > 100) // 判断条件
{
break; /*退出循环*/
}
3、switch循环
/*如果当前语句·没有break,那就顺序执行*/
switch (choice) //判断用户输入的选项
{
case 1:
printf("注册功能\n");
break;
case 2:
printf("登录功能\n");
break;
case 3:
printf("找回密码\n");
break;
default:
/*都不满足的情况*/
printf("input error\n");
break;
}
4、do while循环
int count = 0; //定义一个变量
while (count) //判断条件
{
printf("hello world\n"); //打印结果为:hello world
}
/*总结:while 是先判断再执行*/
/*总结:do.... while 实现执行再判断*/
do
{
printf("hello 259\n"); //打印结果为:hello 259
} while (count); //判断条件
5、for循环
for ( int i = 2; i < 100; i++) //从2开始,循环到100结束,每次循环加1
6、idx++与++idx的区别
/*前置++与后置++有什么区别*/
int idx = 0;
while (idx++ < 3)
{
printf("hello nihao\n"); //打印结果为:hello nihao
}
/*后置++:先判断,在运算*/
int idx = 0;
while (++idx < 3)
{
printf("hello niuniu\n"); //打印结果为:hello niuniu
}
/*前置++:先运算在判断*/
三、数组
1、数组的本质:
1. 连续的内存空间
2. 相同的数据类型
2、整数数组
int内存为4个字节
int num[5]; // 5个元素, 每个元素都是int类型
/* 数组的内存 */
int length = sizeof(num);
printf("length is %d\n", length); // 打印长度为20字节
3、字符数组
char内存为1个字节
char array[30]; // 30个元素, 每个元素都是char类型
/* 数组的内存 */
length = sizeof(array);
printf("length is %d\n", length); // 打印长度为30字节
4、字符串
" " 字符串定义:字符串是由字符构成的
字符串的末尾默认加上'\0'
计算字符串长度与比较字符串大小
/* 字符串的末尾默认加上'\0' */
char buffer[] = "hello world";
char nums[] = "Hello worldd";
int size = sizeof(buffer);
printf("size is %d\n", size); // 打印字符串的大小为:13
/* 计算字符串的长度 */
int length = strlen(buffer); // 字符串的长度
printf("length is %d\n", length);
/*
四个常用的字符串函数: strlen, strcat, strcmp, strcpy
*/
int cmp = strcmp(buffer, nums); // 比较字符串大小
printf("cmp = %d\n", cmp);
/* ASCII码 */
/* 总结: strcmp函数是比较字符串大小的函数。比较的是ASCII码. */
5、数组排序
数组的第一个元素的索引的0, 所以最后一个元素的索引是大小减1
四、练习
1、1 + 3 + 5 + ..... + 999 =
int totalSum = 0; // 初始化总和totalSum = 0
int idx = 1; // 初始化索引idx = 1
while (true) // 无限循环
{
totalSum = totalSum + idx; // totalSum累加
idx = idx + 2; // idx增加2
if (idx > 999) // 判断idx是否大于999
{
break; /*退出循环*/
}
}
printf("totalSum:%d\n",totalSum); // 输出总和
2、随机输出一个数,如327423,将这个数反序输出324723
int num = 0; // 定义一个变量
printf("please input your num:"); // 提示用户输入一个数
scanf("%d", &num); // 输入一个数
printf("before num = %d\n", num); // 输出这个数
int resverseNum = 0; // 定义一个变量
int yushu = 0; // 定义一个变量
while (num) // 循环条件
{
yushu = num % 10; // 取余数
resverseNum = resverseNum * 10 + yushu; // 反序输出
num = num / 10; // 除以10
}
printf("reverseNum:%d\n", resverseNum); // 输出反序后的数
3、存在100个数再1-10之间,请找出出现次数最多的数字,并计算出现了几次
int nums[100] = {0}; //定义一个数组
/*赋值操作*/
for (int idx = 0;idx < 100;idx++) //从0开始到100结束
{
nums[idx] = rand() % 10 + 1; //生成1-10之间的随机数
}
for (int idx = 0; idx < 100; idx++) //从0开始到100结束
{
printf("nums[%d] = %d\t",idx, nums[idx]); //打印数组中的值
if((idx + 1) %5 == 0) //判断是否换行
{
printf("\n"); //换行
}
}
int times[10] = {0}; //定义一个数组
for (int idx = 0; idx < 100; idx++) //从0开始到100结束
{
times[nums[idx] - 1]++; //将数组中的值赋给times数组
}
for (int idx = 0; idx < 10; idx++) //从0开始到10结束
{
printf("times[%d] = %d\t", idx, times[idx]); //打印times数组中的值
if ((idx + 1) % 5 == 0) //判断是否换行
{
printf("\n"); //换行
}
}
int freMaxNum = times[0]; //定义一个变量,将times数组中的第一个值赋给freMaxNum
int maxValue = 0; //定义一个变量,用于存储出现次数最多的数字
for (int idx = 0; idx < 10; idx++) //开始for从0开始到10结束
{
if (times[idx] > freMaxNum) //判断出现次数最多的数字
{
freMaxNum = times[idx]; //将出现次数最多的数字赋给freMaxNum
maxValue = idx + 1; //将出现次数最多的数字赋给maxValue
}
}
printf("freMaxNum:%d,\t maxValue = %d\n", freMaxNum, maxValue); //打印出现次数最多的数字和出现次数
4、循环赋值:随机生成20个90-100之间的数
for (int idx = 0; idx < 20; idx++)
{
scores[idx] = rand() % 10 + 90;
}
/* 输出循环取值结果,每四个数换一行 */
for (int idx = 0; idx < 20; idx++)
{
printf("scores[%d] = %d\t", idx, scores[idx]);
if ((idx + 1) % 4 == 0)
{
printf("\n");
}
}