c语言简单复习

    科技2022-07-21  127

    进制 几进制 逢 几进一 二进制 (0,1) 逢二进一 八进制 (01234567) 逢八进一 ( 7+1=10 ) 十进制 (0123456789) 逢十进一 ( 9+1=10) 十六进制(0123456789abcdef)(15+1=10)

    二进制加法 1+0=1 1+1=10 11+10=101 111+111=1110 八进制加法 3+4=7 3+5=10 3+6=11 5+6=13 十六进制加法 6+7=d 6+8=e 7+9=10 7+b=12

    输入输出(重点)

    int b = 015;//八进制表示15 printf("%d\n", b); //十进制输出 printf("%o\n", b);//八进制输出 printf("%#o\n", b);//加上八进制前缀并八进制输出 int c = 0x2a;//十六进制表示16 printf("%d\n", c); // 十进制输出 printf("%x\n", c); //十六进制输出 printf("%#x\n", c);//加上十六进制前缀,十六进制输出 int a = 0b101;//用二进制表示101 printf("%d\n", a);//十进制输出 printf("%o\n", a);//二进制输出 printf("%#o\n", a);//加上二进制前缀,二进制输出

    转义字符 \n 换行 \t 4个空格 ’ '原封不动输出 ‘’ ''原封不动输出 自增和自减 前自增或则前自减 先对自己进行加或者减,再进行其他操作

    后自增或则自减 先进行其他操作,在对自己进行加或减

    不管是前自增还是后自增,自己始终都要加一 自减类似 类型转换 1.自动类型转换(重点)

    float f=100.2; int n=f; printf("%d\n",n);// 100 float f=100.2; float f1=100.2; int n=f+f1; printf("%d\n",n);//201 尽量保证数据不失真

    2.强制类型转换

    int a=100; float f=(float)a; printf("%f\n",f);//100.000000 printf("%f\n",(float)100);//100.000000 return 0; int sum=100; int people=7; float chu=sum/people; printf("%f\n",chu);//14.000000 return 0; int sum=100; int people=7; float chu=(float) sum/people; printf("%f\n",chu);//14.285714 return 0;

    循环 while() for() switch() do……while()

    跳出循环 break 用于跳出循环,结束循环 continue 用于结束当前循环 进行下一次循环 return

    指针 32位系统中为什么最大支持的内存为4G 内存的最小单位(byte),每个字节为8位(bit) 计算机是由很多个电位组成的,每个电路的电压可以是0v或则0, 5v时电路为1 32位cpu,32根线,每根线取值是0或则1,连续32个0(最小值),连续32个1(最大值) 2的32次方,4g

    int a=10; int *p=&a;

    ** 的作用* 1.乘法 2.定义一个指针变量 int *p char *p double *p 3.便是获取指向指针的数据

    输出地址用 %p

    #include<stdio.h> void main() { int a=15,b=99,c=222; int *p=&a; printf("%p,%p\n",p,&a);//打印地址 printf("%d,%d\n",*p,a);//打印地址里的值 printf("%d,%d\n",p,&a); return 0; }

    定义一个指针变量 int p=&a; ** 的作用:表示p是一个指针变量,p里面存储的一个地址

    p是一个指针变量,占用8个字节的内存(不一定是8个,可能是4个),那么这8个字节的内存里面保存的是编号

    int a=15,b=99,c=222; int *p=&a; *p=b;//*p==a a=b c=*p; //c=a // 99 99 99

    值的交换

    int a=100,b=999,temp; int *pa=&a;//*pa==100 int *pb=&b;//*pb==999 temp=*pa;//temp=100 *pa=*pb;//*pa=999 *pb==temp//*pb=100

    函数

    函数的参数 返回类型 函数名(参数列表) { body }

    形参

    实参把值拷贝给形参

    #include<stdio.h> int awap(int a, int b) { int temp; temp=a; a=b; b = temp; printf("a=%d,b=%d\n", a, b); } int main() { int i = 10, j = 20; awap(i, j); printf("i=%d,j=%d", i, j); return 0; } //a=20,b=10 //i=10,j=20 不能返回两个值,所以采用指针 #include<stdio.h> int awap(int *a, int *b) { int temp; temp=*a; *a=*b; *b = temp; } int main() { int i = 10, j = 20; awap(&i, &j); printf("i=%d,j=%d", i, j); return 0; }

    字符串 char类型保存单个字符,char*保存字符串 char[50]=“hello world” char *pa=“hello world”

    #include<stdio.h> #include<stdlib.h> char *strlonger(char *str1, char *str2) { if (strlen(str1) >= strlen(str2)) return str1; else return str2; } int main() { char str1[30] = { "helloworld" }; char str2[30] = { "xihuashifan" }; char *str; str = strlonger(str1, str2); printf("longer string is %s\n", str); return 0;

    二级指针 int a=10; int *p=&a;

    int **p=&p;

    (不够完善) int a = 10; int *p = &a;// p里面保存是a得地址 int **pp = &p;// pp里面保存的是p的地址,*pp取到p ,**pp取到a printf("%p,%p\n", p, *pp); printf("a=%d,*p=%d,**pp=%d\n", a,*p, *pp); return 0;

    指针的初始化 int *p =NULL;//定义一个空指针

    int * char* double* void* void*malloc(参数列表)

    void 不是一个空指针 ,暂时不知道指针保存什么数据类型 可以强制转化为任意类型的指针* (int*)pv int *p=NULL;,这个时候p为空指针

    结构体

    自己定义的类型 struct stu{ char *name; int num; int age; }; int main(){ struct stu stu1; stu1.name="tom"; stu1.num=12; stu1.age=18; printf("%s的学号是%d的,年龄是%d\n",stu1.name,stu1.num,stu1.age); }

    结构体指针 int a=10; int *p=&q; struct stu stu1; struct stu *pstu = &stu1;

    #include<stdio.h> struct stu{ char *name; int num; int age; }; int main(){ struct stu stu1; stu1.name="tom"; stu1.num=12; stu1.age=18; printf("%s的学号是%d,年龄是%d\n",stu1.name,stu1.num,stu1.age); struct stu *pstu=&stu1; pstu->name="lisa"; pstu->num=13; pstu->age=10; printf("%s的学号是%d,年龄是%d\n",pstu->name,pstu->num,pstu->age); }

    typedef 起别名

    typedef struct stu{ char *name; int num; int age; }stu;

    #include<stdio.h> typedef struct stu{ char *name; int num; int age; }stu; typedef struct { char c; int a; ; }s; int main(){ s s1; s1.a=10; printf("%d\n",s1.a); stu stu1; stu1.name="tom"; stu1.num=12; stu1.age=18; printf("%s的学号是%d,年龄是%d\n",stu1.name,stu1.num,stu1.age); }

    结构体内存对齐 struct stu{ char *name; int num; int age; };

    int a; struct stu stu1;

    结构体大小满足: cpy 寻址 1.cpu 主要是为了高效的寻址 char 1个字节 从1的倍数找地址 int 4个字节 从4的倍数寻找地址

    #include<stdio.h> typedef struct { int a; char c; int d; }s; int main(){ s s1; printf("%d\n",sizeof(s1));//12 }

    2.结构体大小是成员中数据类型占用字节数最大的的倍数

    #include<stdio.h> typedef struct { int a; short b; char c; }ss; int main(){ ss s2; printf("%d\n",sizeof(s2));//8 } ----------------------- typedef struct { int a; short b; short c; }sss; sss s3; printf("%d\n",sizeof(s3));//8

    文件操作

    打开文件——>对文件进行读写操作 ——>关闭文件

    file *fopen (文件名 打开方式)

    打开方式

    1.r(read)以只读的方式打开,文件必须存在,否则打开失败2.w(write)以写入的方式打 开 如果文件不存在 则自动创建文件3.a(append)以追加的方式打开 如果文件不存在 则追加的方式创建文件

    打开的文件类型 t:(text)文本文件 b:(binary)图片,视频

    对单个字符进行操作 fgetc(fp):从文件中读取一个字符 (file get char) fputc(’a‘,fp):输入一个字符到文件中 (file output char)

    例如:

    FILE *fp=fopen(地址,"w"); if(fp==null){ printf("open failed\n"); exit(1); } char ch='0'; fputc(ch,fp); return 0;

    对字符串进行操作 fgets(str,100,fp) (string) 例如:

    FILE *fp=fopen(地址,"r"); if(fp==null){ printf("open failed\n"); exit(1); } char str[30]={0}; fgets(str,30,fp); printf("%s\n",str); return 0; fputs: FILE *fp=fopen(地址,"r"); if(fp==null){ printf("open failed\n"); exit(1); } char str[30]={"xihuashifan"}; fputs(str,fp);// 字符串写入文件 return 0;

    函数 char ptr[100]; 1.fread(数组,每个数据块的大小,数据块的个数,文件指针)//一共多少字符=数据块大小*数据块个数

    #include <stdio.h> int main(){ FILE *fp=fopen("E:\\1.txt","r"); char str1[1000]; fread(str1,2,100,fp); //总共200个字符 printf("%s\n",str1); }

    2.fwrite(数组,每个数据块的大小,数据块的个数,文件指针)//一共多少字符=数据块大小*数据块个数

    #include <stdio.h>

    int main(){

    FILE *fp=fopen(“E:\1.txt”,“w”); char str1[1000]={“helloworld\nxihuashida\n嘿嘿”}; fwrite(str1,10,100,fp); //总共1000个字符

    }

    fprintf(文件指针,格式,内容) (file print format) 例子:

    int main(){ FILE *fp=fopen("E:\\1.txt","w+"); char str[20]={"xihuashida"}; int year=2019; fprintf(fp,"%s,%d",str,year); } const(consistant)恒定不变的 const int a=10

    用const 定义的变量认为常量 必须在定义的时候就给它赋值

    用const修饰指针

    const int *p; 值是不能被改变的

    int *const p; 值可以改变 地址不能改变

    #include <stdio.h> int main(){ int a=10; int b=20; int *const p=&a;//指向a后不能再指向b *p=20; printf("%d",*p);//20 }
    Processed: 0.009, SQL: 8