C语言学习初级阶段

文章目录

一、运算符与表达式

1.1、运算符分类

  • 乘、除、取余运算符的优先级高于加、减运算符。
  • 除%运算符外,其余几种运算符既适用于浮点型数又适用于整型数。当操作符/的两个操作数都是整型数时,它执行整除运算,在其他情况下执行浮点型数除法
  • %运算只可以用于整型数

1.2、关系运算符与关系表达式

  • 关系运算符符>、<、==、>=、<=、!=
  • 关系运算符的优先级低于算数运算符
  • 从左往右计算
  • 关系表达式的值只有真和假,对应的值为1和0
  • 0代表假,非0代表真,例如-5代表真,6代表真
  • if(3<a && a<10)先算3<a,如果a为4,则返回值是1
    错误实例if(3<a<10)无论 a 是大于 3 还是小于 3,对于 3<a 这个表达式只有 1 或 0 两种结果,1和0都是小于10的。

2.1、逻辑运算符与逻辑表达式

  • 逻辑运算符!、&&、ll依次为逻辑非、逻辑与、逻辑或,
  • 逻辑非!的优先级高于算术运算符,逻辑与和逻辑或的优先级低于关系运算符.
  • 短路运算式逻辑与和逻辑或
  • 逻辑非是单目运算符,只需要跟一个操作数 例如:!操作数
#include <stdio.h>
int main()
{
  int i=1;
    i&&printf("you can not see me\n");//当i为假时,不会执行逻辑与后的表达式,称为短路运算。
    i||printf("you can not see me\n");
    //如果i为假,逻辑或会执行后面的内容。如果i为真,整体则为真,则后面的内容不会再继续执行。
    //逻辑或运算,一个为真,则整体为真,
   //一个为假,另一个为真,整体为真
  return 0;
}

2.2、赋值运算符

  • 只有变量才能称为左值,例如:等号左边只可以放变量
  • inum=inum +5 =》 inum+=5

2.3、求字节运算符

  • sizeof是求字节运算符,不是函数
  • sizeof用于求变量或常量所占用空间的大小

二、选择、循环

2.1、选择if-else讲解.

2.1.1、关系表达式与逻辑表达式

5>3&&8<4-!0
在这里插入图片描述

2.2、while、for、continue、break讲解

2.2.1、while循环

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1,total=0;
while(i<100)//在这里加分号会造成死循环
{
    total=total+i;//把i加到total上
    i++;//等价于i=i+1  在循环体内没有让while判断表达式趋近于假的操作,死循环

}
printf(total);
}

2.2.2、for循环

在这里插入图片描述

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,total;
for(i=1,total=0;i<=100;i++)//如果在这行代码加上分号结果是不对的,结果为101,原因是达到100之后,才会执行下面的语句。
{
    total=total+1;
}
printf(total);
}

2.2.3、continue语句

  • 作用:结束本次循环,即跳过本次循环体中下面未执行的语句,接着进行是否执行下一次循环的判断
#include <stdio.h>
#include <stdlib.h>
//从1到100求和
int main()
{
int i,total;
for(i=1,total=0;i<=100;i++)//如果在这行代码加上分号结果是不对的,结果为101,原因是达到100之后,才会执行下面的语句。
{
    if(i%2==0)
    {
        continue;//continue下面的语句均不会得到执行
    }
    total+=1;
}
printf(total);
}

2.2.4、break语句

  • break语句的作用是结束整个循环过程,不再判断执行循环的条件是否成立.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, total;
    for (i = 1, total = 0; i <= 100; i++) 
    {

        if (total > 2000)
        {
            break;
        }
        total += i;
    }
    printf("total=%d,i=%d\n",total, i);
    return 0;
}

注意:不可以这样写printf(total, i); 括号里面的format位置必须要有参数。

三、一维数组与字符数组

3.1、一维数组

3.1.1、数组的定义

(1)具有相同的数据类型。
(2)使用过程中需要保留原始数据。
在这里插入图片描述

  • 常量表达式中可以包含常量和符号常量,但不能包含变量

在这里插入图片描述

3.1.2、一维数组在内存中的存储

在这里插入图片描述

3.2、数组访问越界与数组的传递

3.2.1、数组的访问越界

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5]={1,2,3,4,5};
int j=20;
int i=10;
a[5]=6;//访问越界
a[6]=7;
printf("i=%d",i);//i并没有赋值,但是值发生了变化
return 0;
}

输出结果为:i=7. 但是并没有改变i的值,为什么变成了7?
在这里插入图片描述
在这里插入图片描述

上图所示,其实a[6]是没有的,那两个位置应该是i和j的值,但是a[6] 把i和j给顶替了。

3.2.2、数组的传递

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
//数组传递到子函数后,子函数的形参接收到的是数组的起始地址,因此不能把数组的长度传递给子函数
void print(int a[],int length){ //这里写a[5]、a[6]等都是没有用的 因为数组的长度是无法传递的
    int i;
    for(i=0;i<length;i++){
        printf("%3d\n",a[i]);
    }
    a[3]=20;
    printf("\n");
}
int main()
{
    int a[5]={1,2,3,4,5};
    print(a,5);//打印的是全部的数组里面的值   如果写a[2]则打印的是具体的值 
    printf("a[3]=%d",a[3]); 
    return 0;
}


3.3、字符数组与scanf读取字符串

3.3.1、字符数组的初始化与传递

在这里插入图片描述
因为字符数组一般用来存取字符串。通常采用的初始化方式是 char c[10]= “hello”。因为 C 语言规定字符串的结束标志为’\0’,而系统会对字符串常量自动加一个’\0’,为了保证处理方法一致,一般会人为地在字符数组中添加’\0’,所以字符数组存储的字符串长度必须比字符数组少 1 字节。例如,char c[10]最长存储 9 个字符,剩余的 1个字符用来存储’\0’。

  • c[10]表示的是这个数组里面有10个字符 表示的是长度
#include <stdio.h>
#include <stdlib.h>
//模拟printf(%s,c)的操作
void print(char d[])
{
    int i = 0;
    while (d[i])//当走到结束时,循环结束
    {
        printf("%c\n", d[i]);
        i++;
    }
    printf("\n");
}
//输出字符乱码时,去看字符数组是否存储了结束符'\0'
int main()
{
    char c[5] = {'h', 'e', 'l', 'l', 'o'}; // 这种方式初始化字符数组,这种方式没有结束符  char c[5]="hello" 这种方式也会出现乱码
    char d[5] = "how";
    printf("%c\n", c); // 会发现打印了乱码,因为%c读取不到结束符,会出现乱码的情况
    printf("%s\n", d);
    print(d);
    return 0;
}

3.3.2、scanf读取字符串

#include <stdio.h>
#include <stdlib.h>
//scanf读取字符串操作,会自动往字符数组中放结束符
int main(){
    char c[10];
    scanf("%s",c);//字符数组名c中存储了数组的起始地址  %s代表的是字符串 %c是字符 
    //%s遇到空格就会认为已经结束了 停止读取后面的字符  自动忽略空格
    printf("%s\n",c);
}

scanf 通过%s 读取字符串,对 c 和 d 分别输入"are"和"you"(中间加一个空格),scanf
在使用%s 读取字符串时,会忽略空格和回车(这一点与%d 和%f 类似)

3.4、gets与puts讲解,strlen-strcmp-strcpy讲解

3.4.1、gets函数与puts函数

gets函数格式:char *gets(char *str);
scanf是遇到空格就不再读取
gets一次读取一行,gets遇到\n 后,不会存储\n,而是将其翻译为空字符’\0’

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a [20];
    gets(a);//gets中放入我们字符数组的数组名即可  
    //gets一次读取一行
    printf("%s\n",a);
    return 0;
}

puts只能输出字符串,且括号里面只能放数组名。
puts等价于printf(“%s\n”,c);

3.4.2、str系列字符串操作数(需要导入string.h库)

strlen函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int len;
    char c[20];
    gets(c);
    puts(c);
    len=strlen(c);//统计字符串长度,\0是作为结束符 不统计在字符串长度之内
    printf("len=%d\n",len);

}

自定义函数mystrlen

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int mystrlen(char c[20])
{
    int i=0;
    while(c[i])//找到结束符后 循环结束
    {
        i++;
    }
    return i;
}
int main()
{
    int len;
    char c[20];
    gets(c);
    puts(c);
    len=strlen(c);//统计字符串长度,\0是作为结束符 不统计在字符串长度之内
    printf("len=%d\n",len);
    len=mystrlen(c);
    printf("my len=%d\n",len);
    return 0;

}

strcat函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int len;
    char c[20];
    char d[100];
    gets(c);
    puts(c);
    len=strlen(c);//统计字符串长度,\0是作为结束符 不统计在字符串长度之内
    printf("len=%d\n",len); 
    strcat(c,d);//把d的内容拼接到c中
    //不可以放字符串常量
    puts(c);
    return 0;

}

strcpy函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int len;
    char c[20];
    char d[100];
    gets(c);
    puts(c);
    len=strlen(c);//统计字符串长度,\0是作为结束符 不统计在字符串长度之内
    printf("len=%d\n",len); 
    strcpy(c,d);//把c中的字符串复制到e中
    //不可以放字符串常量
    puts(c);
    return 0;

}

strcmp函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int len;
    char c[20];
    char d[100];
    gets(c);
    puts(c);
    len=strlen(c);//统计字符串长度,\0是作为结束符 不统计在字符串长度之内
    printf("len=%d\n",len); 
    printf("c?d=%d\n",strcmp(c,d));//或者可以写成strcmp(c,"how");
    //c>how 返回值是正值  相等为0  小于返回-1   
    return 0;

}

四、指针

4.1、指针的本质(间接访问原理)

4.1.1、指针的定义

  • 指针变量才可以存储地址
  • 定义格式: 基类型 *指针变量名
  • 指针:一个变量的地址
  • 指针变量:一个变量,专门用来存放另一变量的地址(即指针)

4.1.2、取地址操作符与取值操作符,指针本质

  • 取地址操作符:&,也叫引用,通过这个操作符可以获得一个变量的地址
  • 取值操作符:*,也叫解引用,通过这个操作符可以获得一个地址对应的数据
    在这里插入图片描述
    在这里插入图片描述
  • 声明3个指针变量:int *a,*b,*c
  • int *a,b,c 表示的是 int *a,int b,int c

4.2、指针的传递使用场景

4.2.1、指针的传递

  • 形参是变量
  • 函数调用就是值传递
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void change(int *j)//j是形参
{
    *j=5;
}
int main(){
    int i=10;
    printf("before change i=%d\n",i);
    change(&i);//c语言的函数调用是值传递,实参赋值给形参,j=i
    printf("after change i=%d\n",i);
    return 0;

}

在这里插入图片描述

4.3、指针的偏移使用场景

4.3.1、指针的偏移

如下图所示,数组名中存储着数组的起始地址Ox61fdf0,其类型为整型指针,所以可以将其赋值给整型指针变量p,可以从监视窗口中看到p+1的值为Ox61fdf4。那么为什么加1后不是Ox61fdf1呢?因为指针变量加1后,偏移的长度是其基类型的长度,也就是偏移sizeof(int),这样通过*p+1就可以得到元素a[1].编译器在编译时,数组取下标的操作正是转换为指针偏移来完成
在这里插入图片描述

4.3.2、指针与一维数组

为什么一维数组在函数调用进行传递时,它的长度子函数无法知道呢?
这是由于一维数组名中存储的是数组的首地址.如下例所示,数组名c中存储是一个起始地址,所以子函数change 中其实传入了一个地址.
定义一个指针变量时,指针变量的类型要和数组的数据类型保持一致,通过取值操作,就可将“h”改为“H”,这种方法称为指针法.获取数组元素时,也可以通过取下标的方式来获取数组元素并进行修改,这种方法称为下标法。

#include <stdio.h>
void change(char *d)
{
	*d='H';
	d[1]='E';  //与*(d+1)='E'等价
	*(d+2)='L';
}
int main()
{
	char c[10]="hello";
	change(c);
	puts(c);
	return 0;
}
	

4.4、指针与malloc动态内存申请,栈与堆的差异

4.4.1、指针与动态内存申请

C语言的数组长度固定是因为其定义的整型、浮点型、字符型变量、数组变量都在栈空间中,而栈空间的大小在编译时是确定的、如果使用的空间大小不确定,那么就要使用堆空间。

#include <stdio.h>
#include <stdlib.h>//malloc使用的头文件
#include <string.h>

int main()
{
	int size;//size表示申请的空间的大小
	char *p;//void *类型的指针不能偏移,因此不会定义无类型指针
	scanf("%d",&size);//输入要申请的空间大小
	//malloc返回的void*代表无指针类型
	p=(char *)malloc(size);
	//p[0]='H';
	//p[1]='0';
	//p[2]='w';
	//p[3]='\0';
	strcpy(p,"malloc succceed");
	puts(p);
	free(p);//释放申请空间时,给的地址必须是最初malloc返回给我们的地址
	//	free(p+1);错误  必须要和之前的值相同,不能改变
	return 0;

4.4.2、堆与栈的差异

堆是一个动态的。

#include <stdio.h>	
#include <string.h>
#include <stdlib.h>
char *print_stack()
{
	char c[100]="I am print_stack";
	puts(c);
	return c;
}
cahr *print_malloc()
{
	cahr *p=(char*)malloc(100);
	strcpy(p,"I am print_malloc");
	puts(p);
	return p;
}
int main()
{
	char *p;
	p=print_stack();
	puts(p);
	p=print_malloc();
	puts(p);
	free(p);//只有free时,堆空间才会释放
	return 0;
}

五、结构体与C++引用

5.1、结构体-结构体对齐-结构体数组

5.1.1、结构体的定义、初始化、结构体数组

声明结构体类型的一般形式:

struct 结构体名
	{成员列表};

//下面是一个例子
struct student
{
	int num;char name[20];char sex;
	int age;float score;char addr[30];
};

先声明结构体类型,再定义变量名,例如struct student student1,student2;
结构体的scanf输入和输出

#include <stdio.h>
struct student
{
    /* data */
    int num;
    char name[20];
    char sex;
    int age;
    float score;
    char addr[30];

};//结构体类型声明时,后面一定要加分号
int main()
{
    struct student s={1001,"lele",'M',20,85.4,"Shenzhen"};//最好不能让变量名和结构体名一样
    struct student sarr[3];//定义一个结构体数组变量
    int i;
    //结构体输出,必须单独访问内部的每个成员
    printf("%d %s %c %d %f %s\n",s.num,s.name,s.sex,s.age,s.score,s.addr);//要用点.  作用是成员选择运算符
    for(i=0;i<3;i++)
    {
        scanf("%d%s %c%d%f%s",&sarr[i].num,sarr[i].name,&sarr[i].sex,&sarr[i].age,&sarr[i].score,sarr[i].addr);//s.name是一个数组不需要取地址,因为里面自带的有地址 
    }
    for(i=0;i<3;i++)
    {
        printf("%d %s %c %d %f %s\n",sarr[i].num,sarr[i].name,sarr[i].sex,sarr[i].age,sarr[i].score,sarr[i].addr);
    }
    
    //scanf("%d%s %c%d%f%s",&s.num,s.name,&s.sex,&s.age,&s.score,s.addr);//s.name是一个数组不需要取地址,因为里面自带的有地址 
    return 0;

}

5.1.2、结构体对齐

结构体的大小必须是其最大成员的整数倍。

#include <stdio.h>
#include <stdlib.h>
struct student_type1{
    double score;//double是一个浮点类型 8个字节  浮点分为float和double   float占4个字节   8
    short age; //short是整型占2个字节 跟int一样,int占4个字节               2
};
struct student_type2{
    double score;//double是一个浮点类型 8个字节  浮点分为float和double  8
    char height;     //如果2个小存储之和是小于最大长度8,那么他们就结合在一起,看成2个
    short age;                      //2
};
struct student_type3{
    int height;  //4
    char sex;       //1    
    short age;      //2
};
int main()
{
    struct student_type1 s1;
    struct student_type2 s2;
    struct student_type3 s3;
    printf("s1 size=%d\n",sizeof(s1));
    printf("s2 size=%d\n",sizeof(s2));
    printf("s3 size=%d\n",sizeof(s3));
    return 0;
}
//输出结果如下
s1 size=16
s2 size=16
s3 size=8

5.2、结构体指针与typedef的使用

5.2.1、结构体指针

一个结构体变量的指针就是该变量所占据的内存段的起始地址。可以设置一个指针变量,
用它指向一个结构体变量,此时该指针变量的值是结构体变量的起始地址。
指针变量也可以用来指向结构体数组中的元素,从而能够通过结构体指针快速访问结构体内的每个成员

#include <stdio.h>
//结构体指针的练习
struct student{
    int num;
    char name[20];
    char sex;
};
int main()
{
    struct student s={1001,"wangle",'M'};
    struct student sarr[3]={1001,"lilei",'M',1005,"zhangsan",'M',1007,"lili",'F'};
    struct student *p;//定义了一个结构体指针变量
    p=&s;
    printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex);//方式一通过结构体指针访问成员
    printf("%d %s %c\n",p->num,p->name,p->sex);//方式二,大部分用这种
    p=sarr;//等价于p=&sarr[0];
    printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex);//方式一通过结构体指针访问成员
    printf("%d %s %c\n",p->num,p->name,p->sex);//方式二,大部分用这种
    printf("---------------------------------------------\n");
    p=p+1;
    printf("%d %s %c\n",(*p).num,(*p).name,(*p).sex);//方式一通过结构体指针访问成员
    printf("%d %s %c\n",p->num,p->name,p->sex);//方式二,大部分用这种
    return 0;
}

//结果如下
1001 wangle M
1001 wangle M
1001 lilei M
1001 lilei M
---------------------------------------------
1005 zhangsan M
1005 zhangsan M

5.2.2、typedef的使用

前面定义结构体变量时使用的语句是 struct student s,以这种方式来定义结构体变量有些
麻烦,即每次都需要写 struct student。那么有没有简单一些的定义方式呢?答案是肯定的,可以选择使用 typedef 声明新的类型名来代替已有的类型名

#include <stdio.h>
//结构体指针
typedef struct student
{
    int num;
    char name[20];
    char sex;
}stu,*pstu;//stu是struct student的别名
//stu等价于 struct student , pstu等价于struct student*

typedef int INTEGER;
int main(){
    stu s={1001,"wangle",'M'};
    pstu p;//定义了一个结构体指针变量
    INTEGER i=10;
    //stu *p=&s;//定义了一个结构体指针变量
    p=&s;
    printf("i=%d,p->num=%d\n",i,p->num);
    return 0;
}
//结果如下
i=10,p->num=1001

使用 stu 定义结构体变量和使用 struct student 定义结构体变量是等价的;使用 INTEGER
定义变量 i 和使用 int 定义变量 i 是等价的;pstu 等价于 struct student*,所以 p 是结构体指针变量

5.3、C++引用

5.3.1、C++引用的讲解

#include <stdio.h>
//当你在子函数中要求改主函数中变量的值就要引用,不修改就不需要写引用&
void modify_num(int &b)//形参中写&要称为引用
{
    b=b+1;
}
//在子函数内修改主函数的普通变量的值
int main()
{
    int a=10;
    modify_num(a);
    printf("after modifyt_num=%d\n",a);
    return 0;
}

#include <stdio.h>
void modify_num(int *&p,int *q)  //引用必须和变量名紧邻 
{
    p=q;
}
//子函数内修改主函数的一级指针变量
int main()
{
    int *p=NULL;
    int i=0;
    int *q=&i;
    modify_num(p,q);
    printf("after_num:%d\n",*p);
    return 0;
}

5.3.2、C++的布尔类型

布尔类型在 C 语言没有,是C++的,只有true和false

#include <stdio.h>
int main()
{
    bool a=true;
    bool b=false;
    printf("a=%d,b=%d\n",a,b);
    return 0;
}

运算符优先级表

在这里插入图片描述

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值