Talking about the Basic Data Types of C Language

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


Preface

Sort out learning content.

Basic data types in C language

1. Unit

Before talking about the basic data types, we first need to understand the storage unit of the computer.
First of all, a computer is composed of logic circuits. The logic circuit usually has only two states, the on and off of the switch. These two states can be represented by "1" and "0". Therefore, binary storage information is used inside the computer. The amount of information contained in a binary bit is called a bit, and a bit is the smallest unit of information.
Next, let’s talk about other storage units from small to large.

 1Byte=8Bit  表示范围 :2^n(n代表比特位数),也就是0~255个无符号整数 or -128~127个有符号整数
 1KB=1024Byte   
 1MB=1024KB
 1GB=1024MB
 1TB=1024GB
 1PB=1024TB

Explain the representation range of 1 byte (Byte) in an easy-to-understand way. Observe the eight binary numbers below (from low to high, 1 bit represents 1 binary number), the top is 00000000, and the bottom is 00000001 , Which means two states. Since the state of each bit is only 0 or 1, there are two states according to the permutation and combination -> 1 binary number, 4 states for 2 binary numbers, and 8 states for 3 bits. For this type of question, 8-bit binary number There are 2^8 states for a number. Since it starts from 0, the unsigned number it can represent is 0~255.

00000000
00000001

2. Basic data types

Next, let’s talk about data types. The basic data types in C language are as follows:

基本数据类型						     大小        表示范围
char          //字符数据类型     1Byte		 0~2^8-1个无符号数
short         //短整型 			2Byte		 0~2^16-1个无符号数
int           //整形			4Byte		 0~2^32-1个无符号数   	C中整数默认为int型
long          //长整型			4Byte		 0~2^32-1个无符号数	(不同的系统和编译器的大小可能会是8Byte)
long long     //更长的整形		8Byte		 0~2^64-1个无符号数
float         //单精度浮点型     4Byte		 0~2^32-1个无符号数
double        //双精度浮点型     8Byte 		 0~2^32-1个无符号数      C中浮点数默认为double型

For integer variables (signed by default), you can use unsigned (unsigned number), signed (signed number) to modify. E.g:

 unsigned char a = 128;

We can also sizeof(类型名)get the size of the data type by printing . E.g:

#include<stdio.h>

int main()
{
    
    
	printf("char的大小是%d个字节\n", sizeof(char));
	printf("int的大小是%d个字节\n", sizeof(int));
	printf("long的大小是%d个字节\n", sizeof(long));
	printf("long long的大小是%d个字节\n", sizeof(long long));
	printf("float的大小是%d个字节\n", sizeof(float));
	printf("double的大小是%d个字节\n", sizeof(double));
	return 0;
}

The results of the above code running in 32-bit VS2017 are as follows:
Insert picture description here

Friends who have studied other programming languages ​​may find that the above data types do not mention string type (string), so can C language represent strings? The answer is yes. In C language, char 字符串名[]= "字符串内容"a string is defined by -> .

3. Why are there so many types?

  1. Because for different objects, different data types are more representative.
  2. Because the size and representation range of each data type are different, and different spaces are assigned to each type, the purpose of saving space can be achieved.
  3. It is convenient for computer storage and processing.

4.printf() transfer parameters

When printf passes in parameters, if it is plastic, it will pass in four bytes by default. Since printf is a variable parameter function, the type of the subsequent parameters is unknown, so no matter what type you pass in, printf will only Depending on the type, it will be stored in two different lengths. Among them, 8 bytes are only long long, float and double (note that float will be processed as double and then passed in), and the other types are all 4 bytes. So although the type of a + b is char, it is actually received with a four-byte integer. In addition, when reading, integer methods such as %lld and %llx and floating-point methods such as %f and %lf read 8 bytes, and others read 4 bytes.

to sum up

The above are some of the notes I summarized about the basic data types of the C language. We welcome supplementary corrections for deficiencies!

Guess you like

Origin blog.csdn.net/weixin_47460769/article/details/111354068