好久之前的了,学习c语言的笔记。
依旧是老套路,从基础的变量类型,到函数定义一步步学起
#include <stdio.h>
#include <string.h>
//c语言的变量类型与基本操作
void test2()
{
//数字型
int a = 45, b = 34;
float fa = 123;
double da = 32.5;
printf("元素 = %d\n",a);
//字符型
char str = "d";
char *str1 = "Hello";
char str2[] = "Another Hello";
printf("元素 = %s\n", str2);
//指针类型
char *p = str2;
printf("元素 = %s\n", p);
//数组一维
int n[10] = {1,2,3,4,5,6,7,8,9,10}; /* n 是一个包含 10 个整数的数组 */
for (int j = 0; j < 10; j++)
{
printf("Element[%d] = %d\n", j, n[j]);
}
//数字二位数组
int atwo[3][4] = { //完全初始化
{ 0, 1, 2, 3 } , /* 初始化索引号为 0 的行 */
{ 4, 5, 6, 7 } , /* 初始化索引号为 1 的行 */
{ 8, 9, 10, 11 } /* 初始化索引号为 2 的行 */
};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("元素 = %d \n", atwo[i][j]);
}
}
//其他二维数组的初始化方式
int btwo[3][4] = { {0},{1,2},{1,2,3,4} }; //部分初始化
int ctwo[][4] = { { 0 },{ 1,2 },{ 1,2,3,4 } }; //分行部分初始化
//字符串二维数组的使用
char str3[][5] ={"1234","5678","9012" }; //字符串数组初始化
for (int j = 0; j < 3; j++)
{
printf("元素 = %s \n", &str3[j]);
}
//结构体
struct student
{
int age;
char name[20];
}st; //直接定义结构体变量 可以匿名(无结构体名字)
//先定义结构体 然后定义变量
struct teammember
{
int age;
char name[20];
};
struct teammember tm, tmarray[2], *tmp;
tm.age = 10;
//tm.name = "songyu";
//字符串数组赋值不能用“=” 除非直接初始化 name是数组名,它是常量指针,其数值是不能修改的。
struct teammember ttemp = { 10,"1234" };
strcpy(tm.name, "songyu");
printf("struct name is: %s \n", tm.name);
//枚举类型
//枚举类型的实质是整数集合,枚举变量的引用同于整数变量的引用规则,并可以与整数类型
//的数据之间进行类型转换而不发生数据丢失。
//如果自己不手动赋值,那么系统自动从0开始
enum DAY
{
MON = 1, TUE=2, WED=3, THU=4, FRI=5, SAT=6, SUN=7
};
enum DAY day,tomorrow;
day = MON;
printf("enum tomorrow is: %d \n", day); //枚举的实质是数字
int tomorrows = (int)day + 1;
printf("enum tomorrow is: %d \n", tomorrows); //输出的依旧是数字
//强制转换为枚举
tomorrow = (enum DAY)tomorrows;
//printf("enum tomorrow is: %s \n", tomorrow); //输出的依旧是数字
//枚举对应的字符串根本在编译后的程序中就没有。只能再写switch casep判断输出
//意思就是说 枚举就是方便我们进行判断的 毕竟字符串意义明显 编译之后字符串就没了
switch(tomorrow)
{
case MON:break;
case TUE:break;
case WED:break;
default:break;
}
gets(); //防止关闭
}