Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Storage of C language data

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to realize the storage of C language data". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to realize the storage of C language data" can help you solve the problem.

1. Introduction of data types

In the basic stage, you have learned the basic types and the size of the storage space.

Know how much memory space to open up using a certain type (size determines the scope of use).

Char / / character data type

Short / / short integer

Int / / plastic surgery

Long / / long integer

Long long / / longer plastic surgery

Float / / single precision floating point number

Double / / A double precision floating point number

Basic classification of types

Plastic family

Char

Unsigned char// unsigned

Signed char// is signed

Short

Unsigned short [int] / / unsigned

Signed short [int] / / signed

Int

Unsigned int// unsigned

Signed int// is signed

Long

Unsigned long [int] / / unsigned

Signed long [int] / / signed

Floating point family

Float

Double

Structural type

Array types

Structure type struct

Enumerated type enum

Union type union

Pointer type

Int * pi

Char * pc

Float* pf

Void* pv

Null type

Void represents an empty type (no type)

Usually applied to the return type of the function, the parameter of the function, the pointer type

2. Storage of plastic surgery in memory

The creation of a variable is to open up space in memory. The size of the space is determined by the type of variable

/ / for example, int a = 20 nint b =-10

Int takes 4 bytes for shaping. Here we will analyze how the value 20 of variable an is allocated in memory space.

2.1 original code, inverse code, complement code

There are three representations of integers in a computer, namely, the original code, the inverse code, and the complement code:

Source code: you can directly translate binary into binary in the form of positive and negative numbers.

* * inverse code: * * leave the symbol bit of the original code unchanged, and you can get it by inverting other bits in turn.

* * complement: * * Anti-code + 1 to get a complement

The three representations all have two parts: symbolic bit and numerical bit.

The symbol bits are 0 for "positive" and 1 for "negative".

The original, inverse and complement of positive integers are all the same.

The three representations of negative integers are different.

Note that the integer is stored in memory is the complement, the operator's object is the complement, and the final print is the original code.

An example is given to illustrate the original code, inverse code and complement code of numerical values.

/ / int main () {int a = 10 X int / positive number 00000000 00000000 0000 00001010 original code 00000000000000000000000001010 reverse code 00000000000000000000001010 complement an in memory storage form 0000000a int b =-10 / negative 10000000 00000000 00000000 00001010 original code 11111111111111111111111110101 inverse 1111111111111111111111111111111111110110 complement = inverse + 1b value stored in memory ff ff ff f6 return 0;}

How the value of an is stored in memory:

The value of b is stored in memory:

In computer systems, numerical values are always represented and stored by complements. The reason is that using complements, symbolic bits and numerical ranges can be handled in a unified manner.

Because there are only adders in CPU, addition and subtraction can also be handled in a unified way. in addition, the complement code and the original code are converted into each other, and the operation process is the same, and no additional hardware circuit is needed.

The following example shows that the operation of data in memory uses complement rather than original code:

Int main () calculates using complement Printed is the original code {1-1//CPU only adder 1 + (- 1) first step: 00000000 00000000 00000000 00001 complement step 2: 100000000000000000000001-1 original code 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111 11111111 11111111-1 complement result is 33 bits Out of range 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010-2} 2.2 size end introduction of 2.2.1 what is the size end

Big-end (storage) mode means that the low bit of data is stored in the high address of memory, while the high bit of data is stored in the low address of memory.

Small end (storage) mode means that the low bit of data is stored in the low address of memory, while the high bit of data is stored in the high address of memory.

2.2.2 the meaning of big end and small end

Because in a computer system, we are in bytes, and each address unit corresponds to a byte, and a byte is 8bit

But in C, in addition to the 8 bit char, there are 16 bit long and 32 bit long (depending on the compiler).

In addition, for processors with bits greater than 8 bits, such as 16-bit or 32-bit processors, because the register width is larger than one byte, there must be a problem of how to arrange multiple bytes. This leads to large-end storage mode and small-end storage mode.

Give an example of the size side, for example: a short type x of 16bit, the address in memory is 0x0010, and the value of x is 0x1122, then 0x11 is high byte and 0x22 is low byte.

For large-end mode, put 0x11 in the low address, that is, 0x0010, and 0x22 in the high address, that is, 0x0011.

For small end mode, it's just the opposite.

Our commonly used X86 architecture is small-end mode, while KEIL C51 is large-end mode. Many ARM,DSP are in small-end mode. Some ARM processors can also be chosen by the hardware to be in large-end mode or small-end mode.

Int main () {int a = 0x11223344; return 0;}

The low-byte 0x44 is blocked in the low address, so it is in small-end mode:

2.2.3 write a program to judge byte order

Design a Mini Program to determine the byte order of the current machine.

Int checksys () {int a = 1 int racket 00 00 01 char* ch = (char*) & a Truncation Chart * truncates bytes, pointer points to low address data return * ch;// dereference, returns low address data / / return * (char*) & int / the above two lines of code can also be written as one line of code} int main () {int a = checksys () If (printf 1) / / if the data saved at the low address is 1, that is, 0x01, it is low byte {printf ("small end\ n");} else {printf ("big end\ n");} return 0;}

This is the end of the content about "how to store data in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report