In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the data types of C language, which is very detailed and has certain reference value. Friends who are interested must finish reading it.
1. The data types of C language are basically divided into:
1. Integrality
Char / / characters are essentially integers, but char type value truncation opens up one byte unsigned char signed charshort / / 2 bytes unsigned short [int] signed short [int] int / / 4 bytes unsigned int signed intlong / / 4 bytes unsigned long [int] signed long [int]
two。 Floating point type
Floatdouble
3. Structural type
Array type struct type struct enumeration type enum union type union
4. Pointer type
Int* pi;char* pc;float* pf;void* pv;...
5. Null type
Void II. Implicit type conversion 1. What is implicit type conversion
In C language, implicit type conversion is a spontaneous behavior of the compiler, which is often converted from small to large, and represents a few-byte data type in the data type, which is converted to a multi-byte data type to ensure the integrity of the data; (the object-oriented language also has this concept, and there will also be implicit type conversion for classes.) generally speaking, implicit type conversion can be divided into two types: integer promotion and type conversion.
two。 Integer lifting
1. Definition: the integer arithmetic of C is always performed with at least the precision of the default integer type. To achieve this precision, the character (char type 1 byte) and short integer (short int type 2 byte) operands in the expression are converted to normal integers (int type 4 bytes) before use, which is called integer promotion.
Popular: no matter whether the data type is char, short int, … When reading to CPU for calculation, it will first be promoted to 32-bit calculation through integrality, and the number of read bits of the settlement result depends on the type of data read. In the case of char type, 8 bits (bit) will be truncated.
[note here: usually when CPU calculates, the data used is the translated complement of the source code]
two。 Integral promotion is promoted according to the symbol bits of the data type of the variable (refers to its own type, not the numeric type).
/ / eg1. Negative integer promotion char a =-1 char type defaults to signed type / / its binary source code is: 10000001Universe / complement: 11111111 / / when the integer promotion is made, the symbol bit in the char type data of 8bit is raised to 11111111 11111111 11111111 111111 after 32 bits; (complement) / / when the symbol bit in the char type data is raised to 32 bits. Positive integer promotion char a = 1; / its binary source code is: 00000001 / / complement = source code: 00000001 / / when the integer promotion is performed, the symbol bit in the char type data of 8bit is promoted to 00000000 00000000 0000000000000001 after 32 bits because the symbol bit in the char type data of the positive number is 0 position.
3. The concrete embodiment of truncation:
/ / eg3.char c =-129bot printf ("% d", c)
The result is: 127
The reason is:-129 source code: 1000 0000 0000 0000 1000 0001
The complement in memory is 1111 1111 1111 0111 1111
While the character variable c truncates only 8 bits, that is, what the c variable holds is: 0111 1111 (complement)
Output d% bit integer with symbol bit 0
The integer is upgraded to 0000 0000 0000 0111 1111 (complement)
If you convert it to the source code, it is 127.
[note that the truncation principle here is related to the size of the machine, and truncation operates on the complement in memory]
3. Type conversion
1. Concept: the operands on both sides of the operator are of different types, so the operation cannot be performed unless one Operand is converted to the type of another Operand; and this conversion is type conversion (compiler spontaneous)
two。 From bottom to top, automatic conversion
Long doubledoublefloatunsigned long intlong intunsigned intint
3. [note] this type conversion is only built between operation operators, otherwise there will be unreasonable problems.
Eg4.float f = 3.14 int num = f Bing Bash / implicit conversion, there will be precision loss
In the case of assignment, the high float type changes to the low int type, resulting in a loss of precision.
Third, the size of the machine. What is the size end?
The small end (storage) mode means that the base (low weight) of the data is stored in the bottom address of memory, while the high (high weight) of the data is stored in the high address of memory.
[most machines use small-end mode]
The big end (storage) mode means that the base (low weight) of the data is stored in the address of the high memory, while the high bit of the data is stored in the low address of the memory.
two。 Application of size end in truncation
Eg3. Truncation occurs in, that is, the character c truncates the integer value-129
/ / eg3.char c =-129
We show that the integer a variable in the code stores data at the address of memory. From the memory address, we can see that the sequence is incremented from high to low.
A: the complement is 1111 1111 1111 0111 1111
After converting to hexadecimal, it is ff ff ff 7f; (the weight is the highest on the left and the lowest on the right)
Then the stored data of the memory address of the character variable c is displayed, and it can be seen that since the char type has only one byte, the lowest byte with the address will be truncated first from the four bytes a.
It can be seen from the figure that it truncates the data 7f in the low address, and 7f is also a low weight.
Therefore, in vs2013, the small end principle is adopted.
3. Determine whether the byte order of the current machine is large or small # include#include#pragma warning (disable:4996) int check_sys () {int I = 1; return (* (char*) & I); / / Note that data type conversion occurs} int main () {int ret = check_sys (); if (ret) {printf ("small end\ n") } else {printf ("big end\ n");} system ("pause"); return 0;}
[note] (* (char*) & I)
When dereferencing a pointer, the size of the bytes taken from memory is determined by the type of data it points to. To put it bluntly, the address of I changes from int * to char *, and when dereferencing, the data type it points to changes from int to char, so the displayed data will be truncated.
From the truncation method above, we can know that 1 is stored in 32 bits in memory. In terms of one byte, the high weight bit is 0 and the low weight bit is 1. Therefore, the size of the end can be judged by the 1 or 0 of the parameter passed by return.
Fourth, the storage of integers in memory 1. Original code, inverse code, complement code
A binary representation of a number in a computer, called the machine number of this number. The machine number is signed, and the symbol is stored in the highest bit of a number in the computer, with a positive number of 0 and a negative number of 1.
For example, the number + 3 in the decimal system, the computer word length is 8 bits, and the conversion to binary is 00000011.
If it's-3, it's 10000011.
In C language, the storage of integers in computers is stored according to the original countercomplement rule, that is, for integers, data stored in memory is actually a complement.
When the computer adopts this rule, the +-* / operation of the data operation can be solved by addition, so the designed computer only needs to design the addition module, which can greatly save the cost.
The specific rules are as follows:
1. Positive number
The original, inverse and complement of positive numbers are the same as the original code.
two。 Negative number
Original code: the number of machines in this number, and the highest bit is the symbol bit
Inverse code: the original code remains unchanged except for the symbol bit, and the other bits are reversed by bit.
Complement: inverse + 1
two。 For example, practice the storage of integer data in memory / / example 1. Try to determine what the output result is: int main () {char a =-1; signed char b =-1; unsigned char c =-1; printf ("aversion% dinstruction% dinstruction% cession% d\ n", a, b, c); system ("pause"); return 0;}
Results:
Example 1 parsing:
-1 complement in memory: 1111 1111 1111
Truncation occurred during the storage of char a, signed char b and unsigned char c. The complement in memory is 1111 1111.
However, when three bits are output with% d (integer), integer promotion will occur, from the original 8-bit integer to 32-bit, and whether the high-order complement 0 or 1 depends on the data type (signed type complement bit, unsigned type directly complement 0)
Both char an and signed char b belong to the symbolic type, and the symbol bit is 1, with 24 bits 1.
The memory value is 1111 1111 1111; when you output% d, push back to the original code, the answer is-1
Unsigned char c belongs to unsigned type, with 24 bits 0.
The memory value is 0000 0000 0000 1111 1111; when you output% d, push back to the original code, the answer is 255
/ / example 2. Try to determine what the output is: int main () {char a = 128; char b =-128; printf ("averse% ucharge% u\ n", aMagneb); system ("pause"); return 0;}
Results:
Example 2 analysis:
The complement in memory: 0000 0000 0000 1000 0000
Memory complement: 1111 1111 1111 1000 0000
Truncation occurs when char an and char b are stored. The complement in memory is 1000 0000.
When% u (unsigned integer) output, an integer lift occurs, from the original 8-bit integer to 32-bit
Both char an and char b belong to the symbolic type, and the symbol bit is 1, with 24 bits 1.
The memory values are all 1111 1111 1111 1000 0000. the inverse code is directly equivalent to the original code when outputting% u.
The answer is:
/ / example 3. Try to determine what the output is int main () {int I =-20; unsigned int j = 10; printf ("iSj =% d\ n", iSj); system ("pause"); return 0;}
Results:
Example 3 analysis:
-20 complement in memory: 1111 1111 1111 1110 1100
10 complement in memory: 0000 0000 0000 1010
Both int I and unsigned int j are four-byte type variables, so truncation will not occur when storing them.
However, the I + j = expression will have a type conversion, and int will automatically convert to unsigned int type evaluation
Add the complement of two variables in CPU to get: 1111 1111 1111 0110
The calculation result is output as% d (integer), and pushed back to the original code: 1000 0000 0000 0000 1010
The answer is-10.
/ / example 4. Try to determine what the output is int main () {unsigned int i; for (I = 9; I > = 0; iMel -) {printf ("% u\ n", I);} system ("pause"); return 0;}
Results:
Example 4 analysis:
Because the I variable is of type unsinged int, it has no sign bit
And the expression I > = 0 will cause type conversion, and int 0 will automatically convert to unsigned int type evaluation.
Therefore, the comparison result is always true, because the 32 bit (symbol bit) of the unsigned type is always 0.
If the condition of for cycle is always Manchu, the answer is an endless cycle.
/ / example 5. Try to determine what the output is int main () {char a [1000]; int i; for (I = 0; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 205
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.