C language elementary study notes

Personal homepage : Click me to enter the homepage

Column classification: C language, elementary       C language programming————KTV        C language games

Welcome everyone to like, comment, and collect.

Work hard together and go to Dachang together.

Table of contents

1. Complex concepts 

1.1 big and small endian

1.2 Plastic improvement

1.2.1 Significance of plastic improvement:

1.2.2 Shaping promotion rules

1.2.3 Examples

2. Specific functions

3. Errors caused by memory destruction in the stack area

3.1 Data exchange

 3.2 Define the array in the stack area


        After learning C language for a long time, I can’t wait to show you my C language beginner notes. This blog mainly talks about complex concepts, specific functions, and stack area memory destruction caused by C language. errors etc.

1. Complex concepts 

1.1 big and small endian

Big-endian storage means that the high-order bits of data are stored at low addresses, and the low-order bits of data are stored at high addresses.

Little-endian storage means that the low-order bits of data are stored at low addresses, and the high-order bits of data are stored at high addresses.

example code

#include<stdio.h>
int main()
{
	int a = 0x11223344;

	return 0;
}

We enter debugging to view the memory (for how to view the memory, you can click here to see how to view the memory )

Let's draw a graph

 So you can see that the low bits are stored at low addresses, so they are little-endian storage.

We can write a code on how to check whether the computer has big-endian storage or little-endian storage. The code is as follows

#include<stdio.h>
int main()
{
	int a = 1;
	char* p = (char*)&a;
	if (*p == 1)
		printf("小端存储\n");
	else
		printf("大端存储\n");
	return 0;
}

 We can see by running

         As we expected. But why can such a code be judged? We know that the int type occupies 4 bytes and char occupies 1 byte. That is to say, we use a char* pointer to point to the int data to modify one byte at a time. We can draw the picture as

 If you don’t understand this, you can check out the blog and I’ll check out the basics of pointers

1.2 Plastic improvement

C's integer arithmetic operations are always performed with at least the precision of the default integer type.
To achieve this precision, character and short operands in expressions are converted to ordinary integers before use, a conversion called integer promotion.

1.2.1 Significance of plastic improvement:

        The integer operation of the expression must be performed in the corresponding computing device of the CPU. The byte length of the operand of the integer arithmetic unit (ALU) in the CPU is generally the byte length of the int, and it is also the length of the CPU's general register.
Therefore, even if the addition of two char types is performed by the CPU, it must first be converted to the standard length of the integer operand in the CPU
.
        It is difficult for a general-purpose CPU to directly add two 8-bit bytes (although there may be such byte addition instructions in machine instructions). Therefore, various integer values ​​in the expression whose length may be smaller than the length of int must be converted to int or unsigned int before they can be sent to the CPU to perform operations.

1.2.2 Shaping promotion rules

Integer promotion is promoted according to the sign bit of the variable's data type.

1.2.3 Examples

        For example, we have a char type variable char a=-1. We know that char occupies 1 byte, which is 8 bits, so its binary sequence is 111111111. Since we want to perform integer promotion to get an int type a, we know Integer promotion is promoted according to the sign bit of the data type of the variable, and the sign bit is 1. The int type occupies 4 bytes and 32 bits, so its binary sequence is 111111111111111111111111111111111111. For the positive char type variable char b=1, its binary sequence is 00000001. We perform integer promotion on it, and its sign bit is 0, so we get the binary sequence of the int type b as 00000000000000000000000000000001. For the unsigned char type variable unsigned char c=1, we perform integer promotion on it. Like the variable, its sign bit is 0 (because it has no sign, which means they are all positive numbers), so we get the int type c The binary sequence is 00000000000000000000000000000001.

2. Specific functions

For specific functions, we can think of bubble sorting. We write a piece of code to turn an unordered array into an ordered one. The code is as follows

#include<stdio.h>
int main()
{
	int arr[5] = { 8,2,5,6,7 };
	int i, j,max;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4 - i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				max = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = max;
			}
		}
	}
	for (i = -0; i < 5; i++)
		printf("%d ", arr[i]);
	return 0;
}

We can see it by running

 We know that the function of bubble sort is to sort unordered data in non-descending or non-ascending order.

3. Errors caused by memory destruction in the stack area

        Speaking of errors caused by memory destruction in the stack area, one of the things we can easily make mistakes is that we construct a function for data exchange and define an array in the function and then return the address of the array .

3.1 Data exchange

Data exchange will basically not cause errors in the main function, but if we want to write a data exchange function, we may write it as

#include <stdio.h>
void swap(int a, int b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}
int main()
{
	int a = 10;
	int b = 20;
	swap(a, b);
	printf("a = %d\nb = %d", a, b);
	return 0;
}

 When we run we find

Its value has not changed. This is because our formal parameters are opened in the stack area. That is to say, our formal parameters and temp are both created in the stack area. What it changes is the data in the stack area, not the stack area. We can draw the desired data and understand it as 

 For modification we need to use pointers, the code is as follows

#include <stdio.h>
void swap(int* a, int* b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
int main()
{
	int a = 10;
	int b = 20;
	swap(&a, &b);
	printf("a = %d\nb = %d", a, b);
	return 0;
}

Since the address is passed, we can understand it as

 3.2 Define the array in the stack area

 For example code

int* madearr()
{
	int arr[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int i, j, min;
	for (i = 0; i < 9; i++)
	{
		for (j = 0; j < 9 - i; j++)
		{
			if (arr[j] < arr[j + 1])
			{
				min = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = min;
			}
		}
	}
	return arr;
}

        We created an array in the stack area and we want to return the array, but when the function ends, the space occupied by the function will be released, causing the array arr to be released. Although arr is returned, some undefined ones are returned due to the release of memory, resulting in An error occurred. To solve this problem, we only need to change the array arr to a global variable .

Our content is over today. I hope you can learn a lot. Finally, don’t forget to like, comment, and collect.

Guess you like

Origin blog.csdn.net/Infernal_Puppet/article/details/132466544