计算机考研C语言基础

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

计算机考研C语言基础

概述

​ C语言是一门面向过程的、抽象化的通用程序设计语言广泛应用于底层开发。C语言能以简易的方式编译、处理低级存储器。C语言是役产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。尽管C语言提供了许多低级处理的功能但仍然保持着跨平台的特性以一个标准规格写出的C语言程序可在包括类似嵌入式处理器以及超级计算机等作业平台的许多计算机平台上进行编译。
​ 最新的C语言标准是C18 [10]。

简单描述

一款面向过程的高级程序设计语言
面向过程:用一个个的函数将具体的业务逻辑表达出来

简单的程序

打印输出 hello world c99标准
#include <stdio.h> // standard in & out.head
int main(){
	printf{ I"hello world in” );
	return O;
}
//注意:#并不是c中的语法
格式化输出变量
# include <stdio.h>
# include <stdlib.h>

int main(){         // 定义变量格式化输出
    char c = 'a';
    int b = 4;
    float a = 3.1415;
    printf("%f\n", a);
    printf("%i\n", b);
    printf("%c\n", c);
    system("pause");
    return 0;
}
常量 不能改变的量
格式
1、#define NUM 50;  // 采用define关键字不分配内存空间的
2、const int A = 10;  // 采用const关键字在函数内部会分配内存空间
这两种常量使用方式的差异
// 定义常量
# include <stdio.h>
# include <stdlib.h>
# define NUM 10   // 第一种在全局定义常量

int main(){
    const int a = 30;   // 第二种在函数内部定义
    printf("%i\n", a);
    printf("%i\n", NUM);
    system("pause");
    return 0;
}

数据类型

常见的数据类型归纳

数据类型 { 基本类型 { 整型类型 { 基本整型 ( i n t ) 短整型 s h o r t    i n t 长整型 l o n g    i n t 双长整型 l o n g    l o n g    i n t 字符型 c h a r 布尔型 b o o l 浮点类型 { 单精度浮点类型 f l o a t 双精度浮点类型 d o u b l e 复数浮点类型 f l o a t _ c o m p l e , d o u b l e _ c o m p l e . . . 枚举类型 e n u m 空类型 v o i d 派生类型 { 指针类型 ∗ 数组类型 [    ] 结构体类型 s t r u c t 共用体类型 u n i o n 函数类型 数据类型\begin{cases} 基本类型\begin{cases} 整型类型\begin{cases}基本整型(int)\\ 短整型short\;int\\长整型long\; int\\双长整型long\; long\; int\\ 字符型char\\ 布尔型bool\end{cases}\\ 浮点类型\begin{cases}单精度浮点类型float\\双精度浮点类型double\\复数浮点类型float\_comple, double\_comple...\end{cases}\end{cases} \\ 枚举类型enum \\ 空类型void\\派生类型\begin{cases}指针类型*\\数组类型[\;]\\结构体类型struct\\共用体类型union\\函数类型\end{cases}\end{cases} 数据类型 基本类型 整型类型 基本整型(int)短整型shortint长整型longint双长整型longlongint字符型char布尔型bool浮点类型 单精度浮点类型float双精度浮点类型double复数浮点类型float_comple,double_comple...枚举类型enum空类型void派生类型 指针类型数组类型[]结构体类型struct共用体类型union函数类型

sizeof函数的作用
# include <stdio.h>
# include <stdlib.h>
// sizeof 函数输出各数据类型占用的字节

int main(){
    printf("int:%i\n", sizeof(int));
    printf("float:%i\n", sizeof(float));
    printf("double:%i\n", sizeof(double));
    printf("char:%i\n", sizeof(char));
    printf("short int:%i\n", sizeof(short int));
    system("pause");
    return 0;
}

打印输出

int:4
float:4
double:8
char:1
short int:2
请按任意键继续. . .

运算符

算术运算符:+-*/
赋值运算符:=
比较运算符:==
# include <stdio.h>
# include <stdlib.h>

int main(){
    int a = 20;
    int b = 10;
    printf("%i\n", a + b);
    printf("%i\n", b/a);
    printf("%i\n", a*b);
    printf("%i\n", a-b);
    system("pause");
    return 0;
}

流程控制

1.顺序
2.选择
1ifcondition — else — 使用 如果if后面的表达式成立则执行第一个大括号的语句否则执行第二个天括号的语句。

任意输入2个数字比较两者关系

# include <stdio.h>
# include <stdlib.h>
// if else 流程控制

int main(){
    int a, b;
    scanf("%i", &a);
    scanf("%i", &b);
    if(a >= b){
        printf("a>=b");
    }
    else{   
        printf("a < b");
    };
    system("pause");
    return 0;
}
2ifcondition — else ifcondition — else — 如果if后面的表达式成立则执行第一个大括号的语句然后判断else if的语句是否成立。若成立则执行第二个大括号的语句否则执行第三个天括号的语句。并宜可以继续嵌套。

任意输入2个数字比较两者关系

# include <stdio.h>
# include <stdlib.h>
// 流程控制

int main(){
    int a, b;
    scanf("%i", &a);
    scanf("%i", &b);
    if(a > b){
        printf("a > b");
    }
    else if( a == b){   
        printf("a = b");
    }
    else{
        printf("a < b");
    };
    system("pause");
    return 0;
}
3.循环
1for循环格式
for(表达式1;表达式2;表达式3){代码块}
表达式1给控制变量赋初值表达式2为设置跳出条件表达式3为步长。

让变量i循环10次

# include <stdio.h>
# include <stdlib.h>
// for 循环

int main(){
    int a = 10;
    int i = 0;
    for(i; i<a; i++){
        printf("%i\n",i);
    };
    system("pause");
    return 0;
}
2while循环格式
while循环格式:while(表达式){代码块}
如果while的条件为永真则会一直执行陷入死循环;如果为永假则一次也不执行。

利用while实现for循环的需求

# include <stdio.h>
# include <stdlib.h>
// while 循环

int main(){
    int a = 10;
    int i = 0;
   while (i < a)
   {
    printf("this is %i\n", i);
    i++;
   }
   system("pause");
   return 0;
}

数组

1.在数据结构中数组可以被定义为一个数据类型相同的有限序列。简单来说。数组就是能够存储一组数据的变量
2.声明格式 数据类型数组名[元素个数]={元素值-按逗号分隔}
#include <stdio.h>
#include <stdlib.h>

int main(){
    int arr[5] ={1,2,3,4,5};
    printf("下标为0的元素值为%i\n", arr[0]);
    printf("下标为1的元素值为%i\n",arr[1]);
    printf("下标为2的元素值为%i\n",arr[2]);
    printf("下标为3的元素值为%i\n",arr[3]);
    printf("下标为4的元素值为%i\n",arr[4]);
    system("pause");
    return 0;
}

函数

函数格式
返回值的数据类型   函数名(形参){
	函数体
	return 返回值
}

简单的求和函数

# include <stdio.h>
# include <stdlib.h>
// 函数

int sum(int a, int b){
    return a + b;
}

int main(){
    int a, b, c;
    scanf("%i", &a);
    scanf("%i", &b);
    c = sum(a , b);
    printf("a + b = %i", c);
    system("pause");
    return 0;
}

指针

指针就是地址地址就是指针
指针变量是专门用来存储其他变量地址的变量指针变量的类型必须和所指向变量的类型—致。
注意: 指针和指针变量的区别

1、指针就是地址地址就是指针

2、指针与指针变量的区别

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

int main(){
    int a = 20;
    int b = 10;
    printf("a的值%i\n", a);
    printf("b的值%i\n", b);
    printf("a的地址%x\n", &a);  // %x 表示使用十六进制
    printf("b的地址%x\n", &b);
    system("pause");
    return 0;
}
Note

指针运算符*

地址运算符&

声明格式
数据类型	* 	变量名
int a=10;
int * pa = &a;
float b = 3.14;
float * pb = &b;
指针运作的简单例子
# include <stdio.h>
# include <stdlib.h>
// 指针

int main(){
    int a = 10;
    int b = 30;
    int *pa, *pb;
    pa = &a;
    pb = &b;
    printf("a的地址%x\n", pa);
    printf("b的地址%x\n", pb);
    printf("a的值%d\n", *pa);
    printf("b的值%d\n", *pb);  // 通过*获取到地址的值
    system("pause");
    return 0;
}
使用指针交换两个变量值
# include <stdio.h>
# include <stdlib.h>
// 使用函数交换两个数字

// 如果使用这个函数则不能达到交换目的因为在这里会开辟新的a, b 内存地址空间 且调用该函数结束后系统会回收该函数的开辟的内存资源最终达不到交换目的
int swap(int a, int b){  
    int tamp;
    tamp = a;
    a = b;
    b = tamp;
    return 0;
}

// 使用指针的方法可以达到目的
int swap2(int *a, int *b){
    int tamp;
    tamp = *a;
    *a = *b;
    *b = tamp;
    return 0;
}

int main(){
    int a = 10;
    int b = 20;
    // swap(a, b);  // 调用该函数并不能实现变量交换
    swap2(&a, &b);  // 通过指针地址指向去实现对变量的交换
    printf("交换后的a为%d\n", a);
    printf("交换后的b为%d\n", b);
    system("pause");
    return 0;
}

结构体

自定义数据类型

typedef 关键字

typedef  关键字 别名

typedef 使用方法

# include <stdio.h>
# include <stdlib.h>
// typedef 关键字的使用

int main(){
    int a = 10;
    typedef int new_type;
    new_type b = 20;
    printf("%d\n", b);
    // system("pause");
    return 0;
}

malloc () 函数 动态分配申请内存空间

1. 需要引入 <malloc.h> 头文件
2. free()释放内存空间
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>

int main(){
    int * p;  // 定义一个指针变量
    p = malloc(sizeof(int));  // 获取int 4个字节的大小的内存空间的地址赋值给p
    * p = 30;  // 将值赋值给指针指向的地址
    printf("%d\n", * p);
    free(p);  //释放内存
    system("pause");
    return 0;
}
内存分配

1.栈区(stack):
此区域由编译器自动分配释放内存存放函数的参数值局部变量等值。

⒉.堆区(heap): malloc的使用
堆用于在程序在运行时动态地申请某个大小的内存。
一般由程序员分配释放若程序员不释放则可能会引起内存泄漏

结构体定义

struct 结构体名 {
		基础数据类型;
		结构体类型;
}

struct People{
	char name[10];
	int age;
};

结构体变量

struct 结构体名 变量名

. 和 -> 的区别

结构体变量访问成员只能用.
结构体指针变量访问成员用 ->

区分. 和-> 的作用

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

int main(){
    struct People{
        int age;
        char name[10];
    };
    struct People p;

    strcpy(p.name ,"张三");  // string copy
    p.age = 18;
    printf("%s", p.name);
    printf("%i", p.age);

    // struct People * p1= &p;
    // p1 -> age = 18;
    // strcpy(p1 ->name, "张三");
    // printf("%s", p.name);
    // printf("%i", p.age);
    
   system("pause");
   return 0;
}

使用头插法构造一个链表

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

int main(){
    typedef struct Node {
        int data;  // 节点的数据域
        struct Node * next;  // 节点的指针域
    } N;
    N p; //头节点
    N * head;  // 头指针
    int data; // 新增头节点数据
    int len = 0; // 链表有效长度,初始化为0
    
    // 链表初始化头指针指向头结点
    head = &p;
    head ->data = NULL;
    head->next =NULL;
    // 从键盘接收一个数字为链表长度
    scanf("%i", &len);

    //新增节点指针
    N *newNode;

    for (int i = 0; i<len; i++){
        newNode = (N *)malloc(sizeof(N));
        scanf("%i", &data);
        newNode->data = data;
        newNode->next = head->next;
        head->next = newNode;
    }
    newNode = head->next;
    while (newNode !=NULL)
    {
        printf("%i\n", newNode->data);
        newNode = newNode->next;
    }
    system("pause");
    return 0;
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6