Summary of C language core knowledge points

1. Data type

char //字符数据类型 'A'
short //短整型
int   //整型
long  //长整型
long long //更长的整型
float      //单精度浮点数
double     //双精度浮点数

double d = 3.14;
printf("%lf\n", d);

unsigned char i ;//0~255
char i//-128~0~127

1. Why are there so many types?

printf("%d\n",sizeof(char));	//1
printf("%d\n",sizeof(short));	//2
printf("%d\n",sizeof(int));		//4
printf("%d\n",sizeof(long));	//4/8
printf("%d\n",sizeof(long long));//8
printf("%d\n",sizeof(float));	//4
printf("%d\n",sizeof(double));	//8
注:同一类型的有符号数和无符号数所占字节数相同
short int age = 20;  //2^16=65535

That is, in order to express various values ​​in life more abundantly

Data storage method

insert image description here

Constants and Variables

1. Scope

When a local variable and a global variable are the same, the local variable takes precedence.

//取地址符号&
scanf("%d%d",&num1,&num2);
//避免编译器提示语法不安全
#define _CRT_SECURE_NO_WARNINGS 1

declare extren external symbol

extern int g_val;
printf("g_val = %d\n", g_val);

2. The life cycle of variables

  • The life cycle of local variables: the life cycle of entering the scope begins, and the life cycle of the out of scope ends.
  • Life cycle of global variables: the life cycle of the entire program

3. Constants

  • literal constant
  • const modified constant variable
  • Identifier constants defined by #define
  • enumeration constant
const int num = 4;
num = 8 //不能进行更改

int  n = 10;
int arr[n] = {
    
    0};//应该放入常量表达式
enum Sex
{
    
    
	MALE,
	FEMALE,
	SECRET
};
int main()
{
    
    
	printf("%d\n",MALE);	//0
	printf("%d\n",FEMALE);	//1
	printf("%d\n",SECRET);	//2
	enum Sex sex = MALE;
	sex = FEMALE;	//可更改
	FEMALE = 6;		//不能更改
}

array string

int main()
{
    
    
	char arr1[] = "abc";
	//'a','b','c','\0'
	//'\0':字符串的结束标志 
	char arr2[] = {
    
    'a','b','c'};
	char arr3[] = {
    
    'a','b','c','\0'};
	printf("%s\n",arr1); //abc
	printf("%s\n",arr2); //abc烫烫烫bc
	printf("%s\n",arr3); //abc
	printf("%d\n",strlen(arr1));//3
	printf("%d\n",strlen(arr2));//随机值
	
	printf("%d\n",strlen("c:\test\32\test.c"));//13
	//32是两个8进制数字,转化为10进制后ASCII码值所对应的字符
	char arr4[5] = {
    
    'a',98};
	printf("%d\n",sizeof(arr4));//2,所占字节大小
	printf("%d\n",sizeof(arr1));//4
}

insert image description here
insert image description here
insert image description here

1. Two-dimensional array

int arr[][4] = {
    
    {
    
    1,2,3,4},{
    
    5,6,7,8}};
//列下标不能省略

The parameter is in the form of an array
insert image description here
The parameter is in the form of a pointer

*((*p+i)+j) =  (*(p+i)[ j ])

insert image description here

pointer

Store the address of the memory unit, the memory unit is in bytes
*–> dereference operator
The pointer size is 4 bytes on a 32-bit platform

double d = 3.14;
double* pd = &d;
//sizeof(pd) = sizeof(double*)

指针类型决定了指针解引用操作的时候,能够访问空间的大小
int *p;//能够访问4个字节
char *p;//能够访问1个字节
指针类型决定了指针的步长
int *p; //p+1->4
cahr *p; //p+1->1
double *p; //p+1->8

insert image description here

1. Array of pointers

insert image description here
insert image description here

2. Array pointer

insert image description here
insert image description here

3. Summary

insert image description here

pointer advanced

1. Function pointer

insert image description here
insert image description here
insert image description here
insert image description here

2. Array of function pointers

Can replace switch statement
insert image description here

3. A pointer to an array of function pointers

insert image description here

sort function

insert image description here

insert image description here

string functions

insert image description here

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

keyword (static define)

Static modification of local variables: makes the life cycle of local variables longer
Static modification of global variables: narrows the scope of global variables, and an error occurs when another function declares extern
Static modification of functions: changes the link attribute of the function, making the external link Attributes Converted to Internal Link Attributes

macro definition

#define MAX(X,Y) (X>Y?X:Y)
int main()
{
    
    
	max = MAX(a,b);//max = (a>b?a,b)
}

structure

//结构体变量.成员
//结构体指针->成员
struct Book
{
    
    
	char name[20];
};
int main()
{
    
    
	struct Book b1 = {
    
    "CCC"};
	struct Book* pb = &b1;
	printf("%s\n",b1.name);
	printf("%s\n",(*pb).name);
	printf("%s\n",pb->name);
//更改成员数据
	b1.name = "uuuu"//err
	strcpy(b1.name,"uuuu");//string.h
}

insert image description here

insert image description here

cycle

	int i = 1;
	while(i<10)
	{
    
    
		if(i == 5)
			continue;
		printf("%d",i);
		i++;
	}
	//陷入死循环,当i=5时,跳到while,然后到continue,还会回到while
	//ctrl+z 可结束
	while(ch = getchar()! = EOF)
	{
    
    
		putchar(ch);
	}
	char password[20] = {
    
    0};
	scanf("%s",password);
	ret = getchar();
	//当输入完后,按下回车键,‘\n’就会被ret接收
	while(ch=getchar()!='\n')
	{
    
    
		;
	}
	//将缓冲区读取完

The loop control variable of the for statement:
1. Do not modify the loop variable in the body of the for loop to prevent the for loop from getting out of control
.

	int i=0,k=0;
	for(i=0,k=0;k=0;i++)
		k++;
	//不进入循环,k=0为假
#include<windows.h>
Sleep(1000);
#include<stdlib.h>
system("cls");//清空屏幕
//时间戳
srand((unsigned int) time()); 
ret = rand()

function

1. Function call

After the formal parameter is instantiated, it is actually equivalent to a temporary copy of the actual parameter.

The call by address can establish a real connection between the function and the variables outside the function, that is, the inside of the function can directly manipulate the variables outside the function

The printf function returns the number of characters
insert image description here

2. Recursion

1. There are restrictions. When this restriction is met, the recursion will not continue

//计算字符串长度
//char arr[] = "bit";
//int len = my_strlen(arr);
int my_strlen(char *str)
{
    
    
	if(*str!='\0')
		return 1+my_strlen(str+1);
	else
		return 0;
}

3. Arrays as function parameters

//arr是数组,对数组arr进行传参,实际上传递过去的是数组arr的首元素的地址 &arr[0]
buble_sort(arr,sr);

insert image description here
insert image description here

4. Pointers as parameters

insert image description here
insert image description here

operator

1. Arithmetic operators

In addition to the % operator, several other operators work on integers and floating-point numbers.

2. Shift operator

1. Arithmetic right shift: discard the right side, fill the original sign bit on the left
2. Logical right shift: fill zero on the left

3. Logical operators

^异或运算,相同为0,不同为1
!逻辑反操作
EOF:文件结束标志

取反
	int a = 0//4个字节,32bit位
	int b = ~a;//b是有符号数的整形
	//存储的是补码,打印出的是原码,b=-1
	

dynamic memory

#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main()
{
    
    
	//像内存申请10个整形的空间
	int* p = (int *)malloc(10*sizeof(int));
	if(p==NULL)
	{
    
    
		//打印错误原因的一个方式
		printf("%s",strerror(errno));
	}
	free(p)
	p=NULL;
	return 0;
}

insert image description here

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

flexible array

insert image description here

insert image description here

file operation

insert image description here
# file operations

insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/Strive_LiJiaLe/article/details/127239767