Technology Sharing

C language programming core detailed explanation Chapter 1: Number system and conversion and ASCII code

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Written at the beginning

This column is for the basics and advanced learning of C language programming, and strives to solve the problems of self-designed C language examinations and postgraduate entrance examinations, so as to lay a solid C language foundation for learning data structure and algorithm design.
Of course, this column will not be as detailed as explaining the knowledge step by step to the beginner level, but it will try its best to cover all the key points.

1. Number system and conversion

1.1 Definition of four number systems

Binary, Decimal, Octal, Decimal
First, let's talk about the decimal system, which is the most commonly used system in life. Every 10 is incremented by 1.

Why learn binary?

The underlying language of computers is binary, which is the easiest language for computers to understand.

Binary Definition: 0 1 Two encoding symbols
Every two steps forward
For example: 10001 1001111 1000201 (illegal definition) 11111117 (illegal definition)

Why was octal introduced?

Binary is too long, and decimal compression is too troublesome to handle.

Octal definition: 0 1…7 Eight coding symbols are incremented by one every eight
For example: 123451 10678 20740 09341 (illegal definition)

Why was hexadecimal introduced?

Upgraded version of octal

Hexadecimal Definition:0 1…9 ABCDEF Sixteen symbols are incremented by one every sixteen
For example: 1abc1 0945dej5

Thinking: Sometimes it is actually difficult to distinguish between decimal, octal, and hexadecimal. How do you distinguish them?

Decimal: 123
Add 0 in front of octal: 0123
Add 0x in front of hexadecimal: 0x123

1.2 Conversion between four number systems

First of all, it is clear that the bridge for number system conversion is binary. If you want to convert from other systems to other systems, you can complete the conversion by first converting to binary and then converting to other systems.

1. Convert decimal to binary
Decimal to binary conversion, core formula: divide by 2 and take the remainder

2. Convert binary to decimal
Convert binary to decimal, core memory: sum by weight, 2nPower, n starts from 0

3. Convert binary to octal
Core formula: treat three digits as a group, if they are not enough, add 0 in front

4. Convert binary to hexadecimal
Core formula: treat four digits as a group, if they are not enough, add 0 in front

Omitted part: eight to two, sixteen to two, which is essentially the reverse process of 3 and 4.

2. Number system application

2.1 ASCII code

For example: A a B b ¥ $ and so on. These symbols are stored by ASCII values ​​in the computer.
This means that there is a unique binary code.

ASCII code is a standard code established by the United States.
For example (to remember):
The ASCII code value of the capital letter A is: 65 (in decimal, of course, the computer is stored in binary, so it is convenient to remember it in decimal)
ASCII code value of uppercase letter B: 66
ASCII code value of lowercase letter a: 97
ASCII code value of lowercase letter b: 98

It is not difficult to find that the difference between uppercase and lowercase is 32. Remember this difference and use it to convert between uppercase and lowercase.

2.2 Memory

In computer memory management, the base is still very important.

8 bits (binary bits) = 1 byte (B)
1024 bytes = 1KB
1K=1024=210
1M=1024*1024=220

2.3 Machine Code

Machine code is actually the code value understood by the actual computer. We can say that the value stored in a certain space is 78 (decimal), but the computer certainly does not understand it. The machine code is the binary representation of 78.
There are three types of machine codes: complement, original code and inverse code.

3. The structure of C language

The composition of C language: several files
File: Several functions
Function: Function header and function tail
Function header and function body
Statements: Three types of statements

Statements are worth mentioning. Statements include comments, definition statements, and execution statements. The most important point to pay attention to is the execution statement.

3.1 Data structure and data type:

Four types of data: constant variables expressions functions
Constant: A quantity whose value does not change
Variable: A quantity whose value can change at any time.
Expression: A legal formula that connects data using operators
Function: Call a function with certain functions as the operation amount

3.2 Data and data types

Data types: basic types, constructed types, pointer types, void types
Basic types: integer int and the like
Construction type: structure
Empty type: void

Identifier (canonical):

  1. Reserved words (keywords): There are 32 in total. For example: int float char if else for
  2. Predefined identifiers: scanf printf include
  3. User-defined identifiers: variable names, array names, etc.

User-defined identifier: The first character must be a letter or an underscore, followed by letters, numbers, and underscores
The C language is case-sensitive. Int cannot be user-defined, but Int can be.