C language enhancement (1)

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right



提示:以下是本篇文章正文内容,下面案例可供参考

1. Keywords

1. volatile
Add this in front of the variable, even if the variable is not used at compile time, it will not be optimized. Embedded is often used, because don't let the compiler omit steps, such as directly caching the value inside the cpu, and then read the value directly inside the cpu, and then do not repeatedly read the hardware IO port. But we do need to repeatedly read the IO port.

The C language book defines the volatile keyword as follows:

volatile reminds the compiler that the variables defined behind it may change at any time, so every time the compiled program needs to store or read this variable, tell the compiler not to optimize the variable, and it will directly read from the variable memory address Read data, which can provide stable access to special addresses.

If there is no volatile keyword, the compiler may optimize reading and storage, and may temporarily use the value in the register. If this variable is updated by other programs, inconsistencies will occur. (To put it simply: the volatile keyword affects the result of compiler compilation. A variable declared with volatile means that the variable may change at any time. Do not perform compilation optimization for calculations related to the variable to avoid errors.)

Cache (cache): The original meaning refers to a high-speed memory whose access speed is faster than general random access memory (RAM). Usually, it does not use DRAM technology like system main memory, but uses expensive but faster SRAM technology. The setting of the cache is one of the important factors for the high performance of all modern computer systems.
Registers: Registers are some small storage areas used to store data inside the CPU, and are used to temporarily store data and calculation results involved in calculations.
2. After the const
is added, the variable becomes a constant, and it cannot be changed. The original intention is to put variables on the flash to save memory.

Two, the structure

The structure declaration does not take up space, and the space is occupied only after the instantiation definition.

struct person wei = {"weidongshan", 40}; Only this sentence will allocate space

1. Ordinary structure code

The code is as follows (example):

/* 打印同学的姓名、年龄 */
struct person {
    
    
	char *name;
	int age;
};

int main( void )
{
    
    
/*	
	char *name = "weidongshan";
	int age = 40;

	char *name2 = "abc";
	int age2 = 10;

	char *name3 = "abc";
	int age3 = 10;
*/	
	struct person wei = {
    
    "weidongshan", 40};
		
	int i;
	
	dong = 123;
	i = 1; 
	i = 2;
	
	a = 1;
	c = 'A';
	buf[99] = 'B';
	
	prvSetupHardware();
	
	printf("sizeof(int) = %d, sizeof(char) = %d\r\n", sizeof(int), sizeof(char));
	printf("sizeof(int *) = %d, sizeof(char *) = %d\r\n", sizeof(int *), sizeof(char *));
	
	//printf("name = %s, age = %d\r\n", name, age);
	printf("name = %s, age = %d\r\n", wei.name, wei.age);
	
	/* 如果程序运行到了这里就表示出错了, 一般是内存不足 */
	return 0;
}

结果:
sizeof(int) = 4, sizeof(char) = 1
sizeof(int *) = 4, sizeof(char *) = 4
name = weidongshan, age = 40

Summarize

Learn from Wei Dongshan's C language live broadcast class and Baidu Encyclopedia

Guess you like

Origin blog.csdn.net/lianghuajunone/article/details/123413028