[C language] How data is stored in memory

[C language] How data is stored in memory

storage area

Illustration

code

#include <stdio.h>
#define LONG 10
const int constVariable1  = 111; // 常量1 
const char constVariable2 = 'a'; // 常量2 
const char constString[]= "hello"; //  常量3-字符串 
static int globalVariable1 = 112; // 全局变量1(static 可以省略) 
int globalVariable2 = 11111; // 全局变量2 
char globalString[] = "hello"; // 全局变量3 (字符串) 

void fun2();
void fun1()
{
    
    
	int localVariable = 5; // 局部变量 
	printf("栈区(局部变量存放区)地址:%x\n",&localVariable);
}
int main()
{
    
    
	printf("代码区(fun1 方法)地址:%x\n",fun1);
	printf("代码区(main 方法):%x\n",main);
	printf("代码区(fun2 方法):%x\n\n",fun2);
	 
	printf("全局静态区1(全局变量)地址:%x\n",&globalVariable1);
	printf("全局静态区2(全局变量)地址:%x\n",&globalVariable2);
	printf("全局静态区3 (全局变量 字符串)地址:%x\n\n",&globalString);
	
	printf("常量区地址1:%x\n",&constVariable1);
	printf("常量区地址2:%x\n",&constVariable2);
	printf("常量区地址3(常量字符串):%x\n\n",&constString);
	
	fun1();

	return 0;
 }
 void fun2() {
    
    }

result

analyze

The address of the method is in the code area. I personally think it should be in the code area. After compilation, the method name is an address, and the method name does not exist in the memory. When the method is called, this line of address will be transferred to execute.

Personally, I think that the storage area in C language is part of the computer memory. Part of the memory is divided into code area, global static area, constant area, and stack. In this way, the partitions will be clearer and will not appear confusing.

When studying the storage area, I found that the constant area is in front of the global static area. However, when I verified it, I found that the address of the global static area is in front of the constant area. I guess it may be different at different compile times.

Store data into memory

char understand

char, int, short, long, etc. identify the size allocated in memory.

Here I will focus on char , because I always thought that only char can represent characters, or can output characters to the console. In fact, it is not the case. char is just an identifier that opens up a byte in the memory, and is often used to store characters, so It’s called char, which is the same as int and long.

code

char char1 = 65;
int char2 = 66;
long char3 = 67;
printf("char 类型: %d\n",char1);
printf("int 类型:%d\n",char2);
printf("long 类型: %d\n",char3);

printf("\n----------- 将输出类型 d 改为 c ------------\n");
printf("char 类型: %c\n",char1);
printf("int 类型:%c\n",char2);
printf("long 类型: %c\n",char3);

printf("\n----------- ''也可以表示为数字 ------------\n");
char char4 = 'e';
int char5 = 'f';
printf("int 类型:%d\n",char4);
printf("long 类型: %d\n",char5);

result

analyze

Like other integer data types, char represents numbers. You can define %c when outputting to output characters. Int and long can also be output as characters. In the same way, assigning 'a' to a variable during assignment essentially means assigning a value of 65 to the variable. When printing, you can choose to use %d to print 65, or use %c to print a.

Guess you like

Origin blog.csdn.net/weixin_62726289/article/details/129466084