C language - data types

1 basic data type

In the C language, the basic data types are integer (int), character (char) and floating point (float, double). The basic types of data types under the 32-bit system are as follows

type type name byte Indicates the range
int integer 4 - 2 31 ~ 231 - 1
unsigned int unsigned integer 4 0 ~ 232 - 1
short int short integer 2 -215 ~ 215 - 1
unsigned short int unsigned short integer 2 0 ~ 216 - 1
long int long integer 4 - 2 31 ~ 231 - 1
char character type 1 - 2 7 ~ 27 - 1
unsigned char unsigned char 1 0 ~ 28 - 1
float single precision floating point 4 3.4E ± 38 (7 significant figures)
double double precision floating point 8 1.7E ± 308 (15 significant figures)
long double long double 10 1.2E ± 4932 (19 significant figures)

In addition to those listed above, there is also long long int type, which occupies 8 bytes and has a larger range than long int. Many times, we will see the following definition in the program

#define CODE   1024U

其中的“U”或者“u”,是指定为unsigned类型。Add "F" or "f" for float type, "L" or "l" for long double type. If there is no suffix after a decimal, it defaults to double type.

#define CODE   1.024   // 默认为double类型
#define CODE   1.024F   // 为float类型
#define CODE   1.024L   // 为long double类型

2 Arrays, character arrays and strings

2.1 Arrays

An array is a collection of multiple elements of the same data type. Arrays have one-dimensional arrays and two-dimensional arrays. Define the array method as follows

数据类型 数组名[数组长度]   // 一维数组
数据类型 数组名[数组行数][数组列数]   // 二维数组

数组的索引(也可以叫做下标,序号)是从0开始的。比如定义数组时,数组长度为n,那么数组的索引是从0 ~ n - 1

When assigning an initial value to an array, it can only be assigned bit by bit.

int a[4] = {
    
    1,2,3,4}

For the case where the length of the array is not given, but the initial value of the array is given. The compiler will automatically allocate storage space for the array according to the number and type of initial values.

可以用sizeof计算数组空间的大小,也就是字节数。进而可以求数组的长度。For example, to find the length of the array a

#include<stdio.h>

int main()
{
    
    
	int a[4] = {
    
    1,2,3,4};
	
	printf ("数组a的长度为%d",sizeof(a) / sizeof(a[0]));
	
	return 0;
}

The output is

数组a的长度为4

2.2 Character arrays and strings

An array whose elements are characters is called a character array. A string is a continuous sequence of characters. A string constant is enclosed in a pair of double quotes, such as "Welcome", 编译系统自动在每一字符串常量的结尾增加“\0”结尾符. 字符串可以由任意字符组成,一个长字符串可以占两行或多行,但在最后一行之前的各行需用反斜杠结尾. for example

"STM32F103ZET6"

Equivalent to

"STM\
32\
F103\
ZET6"

需要注意的是,"A"与'A'是不同的,"A"是由两个字符(字符"A"与字符"\0") 组成的字符串而后者只有一个字符。最短的字符串是空字符串"",它仅由一个结尾符"\0"组成。

3 enumerated types

An enumerated type is a user-defined data type. Enumeration is to enumerate the possible values ​​one by one. The format for defining an enumeration type is as follows

enum 枚举类型名
{
    
    
    标识符1 = 整型常数1,
    标识符2 = 整型常数2,
    ……
    标识符n = 整型常数n,
};

如果在定义时没有给枚举成员赋值,编译时,编译器会自动给每一个枚举成员赋一个不同的整型值。第一个成员为0,第二个成员为1,以此类推。枚举类型的某一个成员赋值后,它后面的成员会按照依次加1的规则,确定自己的整型值。for example

enum temp
{
    
    
    temp1 = 2,
    temp2,
    ……
    temp10 = 20,
    temp11,
};

The integer value corresponding to temp2 is 3. The integer value corresponding to temp11 is 21.

4 Structures and Unions

4.1 Structure

Struct types allow the individual 不同类型的数据items of a data element to be aggregated into a whole. The declaration format of the structure type is

struct 结构体名
{
    
    
	成员列表
}变量名列表;

定义结构体时,不能直接赋值。Here is an assignment method

#include<stdio.h>

struct student
{
    
    
    char *name;
    char *number;
    int age;
}student;

int main()
{
    
    
    // 给结构体成员赋值
    student.name = "ertu";
    student.number = "20230711";
    student.age = 23;
    
	printf("**************************\n");
	printf("姓名:%s\n",student.name);
	printf("学号:%s\n",student.number);
	printf("年龄:%d\n",student.age);
	printf("**************************\n");
	
	return 0;
}

The result of the operation is as follows

operation result

也可以定义结构体数组,存储多个相同结构的信息。

#include<stdio.h>

struct student
{
    
    
    char *name;
    char *number;
    int age;
}student[10];

int main()
{
    
    
    // 给结构体成员赋值
    student[0].name = "ertu";
    student[0].number = "20230711";
    student[0].age = 23;
    
	printf("**************************\n");
	printf("姓名:%s\n",student[0].name);
	printf("学号:%s\n",student[0].number);
	printf("年龄:%d\n",student[0].age);
	
	// 给结构体成员赋值
    student[1].name = "yingting";
    student[1].number = "20230711";
    student[1].age = 20;
    
	printf("**************************\n");
	printf("姓名:%s\n",student[1].name);
	printf("学号:%s\n",student[1].number);
	printf("年龄:%d\n",student[1].age);
	printf("**************************\n");
	
	return 0;
}

The output is as follows

output result

4.2 Union

The union is defined as

union 共用体名
{
    
    
	成员列表
}变量名列表;

The union body is similar to the structure body and will not be repeated here.

5. Expand

5.1 Structure memory allocation

When the structure allocates memory, it has the following rules (it may be different in different environments, here it is only for VC6.0)

  • Open up memory in units of the number of bytes of the data type that occupies the largest number of bytes in the structure
  • byte alignment

5.1.1 Allocate memory in units of the number of bytes of the data type that occupies the largest number of bytes in the structure

In other words, the size of the structure is an integer multiple of the data type with the largest number of bytes in it. Here is an example

#include<stdio.h>

struct temp
{
    
    
	int i;
    char j;
 
}temp;

int main()
{
    
      
    printf("结构体所占字节数:%d\n",sizeof(temp));
    printf("i的地址:%p   j的地址:%p\n",&temp.i,&temp.j);
 
    return 0;   
}

The output is as follows

output result

The data type with the largest number of bytes in the structure is int, which occupies 4 bytes. Therefore, when allocating memory, use 4 bytes as the unit. Although the char type only occupies 1 byte, 4 bytes of memory are still allocated for it.

如果结构体成员中出现了数组,并不把数组看作整体来计算字节数,而是看数组的数据类型。In other words, even if a char j[5] is defined, it will not be regarded as a data type with a length of 5 bytes when allocating memory. You can see an example

#include<stdio.h>

struct temp
{
    
    
	short int i;
    char j[5];
}temp;

int main()
{
    
      
 
    printf("结构体所占字节数:%d\n",sizeof(temp));
    printf("i的地址:%p   j的地址:%p\n",&temp.i,&temp.j[0]);
 
    return 0;   
}

The output is

output result

如果结构体成员中出现指针变量,那么会以8字节为单位,开辟内存。You can see an example

#include<stdio.h>

struct temp
{
    
    
	short int i;
    char *j;
}temp;

int main()
{
    
    
	printf("结构体所占字节数:%d\n",sizeof(temp));
    printf("i的地址:%p   j的地址:%p\n",&temp.i,&temp.j);
 
    return 0;   
}

The output is as follows

output result

5.1.2 Byte alignment

To put it simply, the address for storing variables must be a multiple of the number of bytes occupied by the variable. Byte alignment is a strategy of exchanging space for time, which can improve the efficiency of CPU reading data.

5.1.3 Nested structures in structures

Let's look at an example

#include<stdio.h>

struct temp1
{
    
    
    char m;
    char *n;
}temp1;

struct temp
{
    
    
	int i;
    char j;
    struct temp1 k;
}temp;

int main()
{
    
    
    printf("结构体temp1所占字节数:%d\n",sizeof(temp1));
    printf("结构体temp所占字节数:%d\n",sizeof(temp));
    
    printf("i的地址:%p   j的地址:%p\n",&temp.i,&temp.j);
    printf("m的地址:%p   n的地址:%p\n",&temp1.m,&temp1.n);
 
    return 0;   
}

The output is as follows

output result

It can be seen from this that when there are nested structures, the two structures allocate space separately. The unit for allocating space is the number of bytes of the data type that occupies the largest number of bytes among the structure members contained in each. The memory size occupied by the final structure is the sum of the memory sizes occupied by the two structures.

5.2 Union Memory Allocation

Unions are different from structures when allocating memory 共用体所占内存大小等于其成员中所占内存最大的成员的字节数. You can see an example

#include<stdio.h>

union temp
{
    
    
	int i;
    char j;
}temp;

int main()
{
    
    
    printf("结构体temp所占字节数:%d\n",sizeof(temp));
    
    printf("i的地址:%p   j的地址:%p\n",&temp.i,&temp.j);
 
    return 0;   
}

The output is

output result

可以看到,两个成员的起始地址相同. i and j are stored as follows

Union Memory Allocation

5.3 Data type conversion

The format of the cast is

(目标数据类型)变量/表达式

In addition to unifying data types during calculation, data type conversion also plays an important role when LCD displays variables. For example, if you want to display the real-time temperature on the LCD.

The temperature module has detected the temperature value, and the data type is float type. Need to display "Temper:26" to the LCD. This is to directly package "Temper:" and the ambient temperature detected by the temperature module, use the sprintf function to convert it into a string, and display it on the LCD.

unsignde char string[20];

sprintf((char*)string,"Temper:%.1f\r\n",temper);

5.4 typedef

Use typedef to give a new name to the data type. For example, some u8, u16, etc. we used when developing STM32 are all defined by typedef

typedef uint32_t  u32;
typedef uint16_t u16;
typedef uint8_t  u8;

Guess you like

Origin blog.csdn.net/qq_45217381/article/details/131658854