C language data type (1) - basic data type and conversion

Introduction
When using a computer to solve practical problems, the main object of program processing is data. For different problems, the types of data involved are also diverse, which may include numerical data represented by numbers, and may also include data represented by characters. of non-numeric data. Different data types are stored in different forms in the computer, and the operations performed by the system on them are also different. In order to meet the needs of the system for data operations of various data types, C language provides definitions of many data types, requiring that each data used in C language must define a type, and the system allocates storage space for the data accordingly, and determines the data type. operations that can be performed.
Data types in C language can be divided into three categories: basic data types, structured data types, and pointers.
insert image description here
This article only introduces the basic data types, and the remaining two types will be explained later.

basic data type

The basic data types of C language include integer type, character type, real type (floating point type), and empty type. Each data type specifier is as shown.
insert image description here
In addition to type specifiers, there are also some data type modifiers, which are used to expand the meaning of basic types so as to more accurately adapt to the needs of various situations. Modifiers are long (long), short (short), signed (signed) and unsigned (unsigned). These modifiers are combined with the type specifiers of the basic data types to express different value ranges and the size of the memory space occupied by the data.
insert image description here

printf() function output format

insert image description here

Additional format specifiers can be used between % and the format specifier, and the additional format specifiers are mainly used to specify the width and output form of the output data.
insert image description here

例如:%ld —— 输出十进制长整型数 
			%m.nf —— 右对齐,m位域宽,n位小数或n个字符
			%-m.nf —— 左对齐  

Data type conversion:

Quantities of different types in C language can perform mixed operations, and when the various quantities participating in the same expression have different types, type conversion must be performed during the calculation process. There are two conversion methods, one is automatic class conversion, and the other is mandatory type conversion.
Automatic type conversion means that when the quantities operated in the same expression have different types, the compiler will automatically convert them into quantities of the same type and then perform the operation.
Automatic conversion principle : Automatically convert the operation object with low precision and small range to the type of operation object with high precision and large range, so as to complete the operation result with higher precision, and then calculate according to the same type of quantity.
insert image description here

Type conversion in assignment operation:
<Real variable>=<Integer expression>
The decimal part is automatically filled with zeros
<Integer variable>=<Real expression>
The decimal part of the real expression is automatically rounded off (without rounding )
<character variable>=<integer expression>
<integer variable>=<long integer expression>
automatically intercepts the low byte assignment of the expression, discards the high byte
<integer variable>=<character data >
<Long integer variable>=<Integer expression>
Automatically add 0 or 1 to the high byte

Mandatory conversion can cast the operation object through the cast operator in the expression, and its function is to cast the operation result of the expression into the type expressed by the similar specifier.

 一般形式:(数据类型说明符)(表达式)

Example: (int)('b'+3 x)%3
1/(float)a+6
Note: (float) 22/5 is not the same as (float) (22/5) Wrong

type conversion:
(int) b =a+5
b= int (3 a)

Summarize

C language provides a variety of data types for users to use. Different types are used for different calculation purposes. Various data types not only provide convenience, but also bring some troubles. Therefore, we must choose the correct data type when programming. If the selection is incorrect, it will cause program compilation errors, incorrect running results, or some other problems. Therefore, an important step in programming is to choose the correct data type, which is very important.

Guess you like

Origin blog.csdn.net/Tao_9/article/details/129668332