当前位置:网站首页>Introduction to C language [detailed]
Introduction to C language [detailed]
2022-07-28 21:55:00 【an520_】
C Language is a process oriented compiler language , It's very fast , Second only to assembly language .C Language is the core language of the computer industry , operating system 、 Hardware drivers 、 Key components 、 Databases and so on are inseparable from C Language ; Don't work C Language , You can't understand the underlying computer .
The development of programming languages has gone through the following stages :
assembly language --> Process oriented programming --> object-oriented programming
- Assembly language is the pioneer age of programming language , It's very low-level , Dealing directly with computer hardware , Low development efficiency , High learning cost ;
- C Language is a process oriented programming language , Out of computer hardware , You can design a medium-sized program ;
- Java、C++、Python、C#、PHP And so on are object-oriented programming languages , They add a lot of concepts to the process .
A qualified programmer must know about memory , Study C Language is the easiest way to understand memory layout 、 The most direct 、 The most effective way ,C Languages are simply made for memory , It is closer to memory than any other programming language .

C In language 32 Key words :



stay C In language , There are three representations of integers : Decimal system , octal , Hexadecimal . The number is 0 start , from 0~7 The number formed is octal . With 0X or 0x start , from 0~9,A~F or a~f The composition is hexadecimal . Except for the sign indicating positive and negative , With 1~9 start , from 0~9 The composition is decimal .
1. What is base
Hexadecimal is a way of counting , Commonly used are binary 、 octal 、 Decimal system 、 Hexadecimal . Any data in computer memory is stored in binary form .
My personal understanding of hexadecimal , Binary numbers are expressed in 2 It's the computing unit , full 2 Into the 1 The number of bits ; Octal numbers are in 8 It's the computing unit , full 8 Into the 1 The number of bits .
For any number , We can all use different base numbers to express , such as , Decimal number 12, Expressed in binary as 1100, Expressed in octal as 14, Expressed in hexadecimal as 0xC.
2. Conversion rules of base
Follow the full scale value to enter 1 position , The single digit becomes 0 Principle , Now let's count in decimal 18 For example , Yes 1-18 Each numerical value in the conversion of various hexadecimals to make a detailed description
Binary conversion :
1 Less than 2, No need to enter 1 position ,1 The binary value of is 1
2 Is a binary value 1 The next number , because 1+1 full 2, Need to enter 1 position , The single digit becomes 0, therefore 2 The binary value of is 10
3 Is a binary value 10 The next number , because 11 The number of digits 1 Less than 2, No need to enter 1 position , therefore 3 The binary value of is 11
4 Is a binary value 11 The next number , because 11 The number of digits 1+1 full 2, Need to enter 1 position , And binary values 11 Number of digits 1+1 Full again 2, So add digits 1, The final conversion result is 100
Change your mind : Binary value 11+1 ->10+(1+1)( One bit equals 2, Into the 1 position , The single digit becomes 0) ->(1+1)+0( The number of digits is full 2, Into the 1 position ) -> 100
And so on , Final decimal number 18 The binary conversion result of is 10010
To octal :
1-7 Less than 8, No need to enter 1 position ,1-7 Octal of consists of 1-7 Express
8 Octal value 7 The next number , because 7+1 full 8, Need to enter 1 position , The single digit becomes 0, therefore 8 The octal value of is 10
And so on , Final decimal number 18 The result of octal conversion of is 22.
To hexadecimal
In hexadecimal , Single digit 1-15 Respectively 1 2 3 4 5 6 7 8 9 a b c d e f (a=10....f=15).16 Is a hexadecimal value c Back 1 Number , because c+1 full 16, Need to enter 1 position , The single digit becomes 0, therefore 16 The hexadecimal of is 10. Final decimal number 18 The hexadecimal conversion result of is 12.
The detailed results are shown in the figure below (C Language prefixes numbers with 0x The number of is considered to be a hexadecimal number )

3.C In language int Declaration of type base and placeholder
Although the following 3 The assignment methods of variables are different , But the actual assignment result is 18.
Copy code The code is as follows :
// Binary type number plus 0b int number1 = 0b10010; // Octal type digits plus 0 int number2 = 022; // Hexadecimal type digit plus 0x int number3 = 0x12;
Octal placeholder :%o
Hexadecimal placeholder :%x
4. Memory storage data details
We know ,int Type data takes up 4 Bytes ,1 Bytes are 8bit. And any data in the computer memory is stored in binary form , So memory needs to be used 32 individual 0 or 1 To describe 1 individual int Type data .
because 18 The binary number of is 10010, We will int Type variable assignment 18, In essence, it corresponds to the memory address of this variable 32 individual bit A modified to :0000 0000 0000 0000 0000 0000 0001 0010( under 31 position , The following numbers are in 0 fill : Why 31 instead of 32 Well , Later on ).
Suppose we define two variables

The computer will allocate memory space according to the memory address in descending order , The details are shown in the following figure :
5. The conversion formula of base
Binary to decimal
0b1100 ->0*2 Of 0 Power + 0*2 Of 1 Power + 1*2 Of 2 Power + 1*2 Of 3 Power = 12
Decimal to binary
67 ->64+2+1 ->2 Of 6 Power + 2 Of 1 Power + 2 Of 0 Power = 0b1000011
6. Other knowledge of hexadecimal
1.n Bit binary can save integer range formula :2 Of n Power -1
for example ,3 The maximum binary number of bits is 111, The corresponding decimal number is 7;5 The maximum binary number of bits is 11111, The corresponding decimal number is (2*2*2*2*2)-1 = 31.
2. The binary saving rule for negative numbers is that the leftmost number is 1. for example ,0000 0000 0000 0000 0000 0000 0001 0010 Represents a positive integer ,1111 1111 1111 1111 1111 1111 1110 1101 A negative number
thus , We can infer ,int The maximum integer that type can hold is 2 Of (32-1) Power -1 =2147483647. Why use 32-1, It's simple ,32 individual bit in , Must smoke 1 individual bit Bits are used to describe whether the number is positive or negative .
Binary number 、 Representation of octal and hexadecimal numbers :
A number is decimal by default , It doesn't need any special format to represent a decimal number . however , Represents a binary 、 Octal or hexadecimal numbers are different , To distinguish it from decimal numbers , Must adopt some special writing method , say concretely , It's about putting specific characters in front of numbers , That's prefixing .
1) Binary system
Binary by 0 and 1 Two numbers make up , It must be used with 0b or 0B( Case insensitive ) start , for example :
// Legal binary
int a = 0b101; // Convert to decimal to 5
int b = -0b110010; // Convert to decimal to -50
int c = 0B100001; // Convert to decimal to 33
// Illegal binary
int m = 101010; // No prefix 0B, It's equivalent to the decimal system
int n = 0B410; //4 Not a valid binary number
Readers, please pay attention , The standard C The language does not support the above binary writing method , It's just that some compilers extend themselves , Binary numbers are supported . let me put it another way , Not all compilers support binary numbers , Only a few compilers support , And it has something to do with the compiler version .
Here are the results of the actual test :
Visual C++ 6.0 I won't support it .
Visual Studio 2015 Support , however Visual Studio 2010 I won't support it ; It can be said that , The high version of the Visual Studio Binary number support , Low version of the Visual Studio I won't support it .
GCC 4.8.2 Support , however GCC 3.4.5 I won't support it ; It can be said that , The high version of the GCC Binary number support , Low version of the GCC I won't support it .
LLVM/Clang Support ( Embedded in Mac OS Under the Xcode in ).
2) octal
Octal is composed of 0~7 Eight figures make up , It must be used with 0 start ( Notice the numbers 0, It's not a letter o), for example :
// Legal octal number
int a = 015; // Convert to decimal to 13
int b = -0101; // Convert to decimal to -65
int c = 0177777; // Convert to decimal to 65535
// Illegal octal
int m = 256; // No prefix 0, It's equivalent to the decimal system
int n = 03A2; //A Not a valid octal number
3) Hexadecimal
Hexadecimal is made up of numbers 0~9、 Letter A~F or a~f( Case insensitive ) form , It must be used with 0x or 0X( Case insensitive ) start , for example :
// Legal hexadecimal
int a = 0X2A; // Convert to decimal to 42
int b = -0XA0; // Convert to decimal to -160
int c = 0xffff; // Convert to decimal to 65535
// Illegal hex
int m = 5A; // Without a prefix 0X, It's an invalid number
int n = 0X3H; //H Not a valid hexadecimal number
4) Decimal system
The decimal system consists of 0~9 Ten figures make up , There is no prefix , It's the same as our usual writing style , I won't repeat .
Binary number 、 Output of octal and hexadecimal numbers
C The commonly used integers in languages are short、int and long Three types of , adopt printf function , You can put them in octal 、 Decimal and hexadecimal output . In the last section, we explained how to output... In decimal form , In this section, we focus on how to output in octal and hexadecimal , The following table lists the different types of integers 、 The corresponding format controller when outputting in different hexadecimal forms :
short int long
octal %ho %o %lo
Decimal system %hd %d %ld
Hexadecimal %hx perhaps %hX %x perhaps %X %lx perhaps %lX
The representation of hexadecimal numbers uses English letters , It's case by case , It should be reflected in the format controller :
%hx、%x and %lx Medium x A lowercase letter , Indicates that the hexadecimal number is output in the form of lowercase letters ;
%hX、%X and %lX Medium X Capitalization , Indicates that the hexadecimal number is output in the form of capital letters .
Octal and decimal numbers are case insensitive , So format controllers are in lowercase . If you are rebellious , Want to use uppercase , Then behavior is undefined , Please be careful :
Some compilers support uppercase , But the behavior is the same as that in lowercase ; Some compilers do not support uppercase , There may be a mistake , It can also lead to strange outputs .
Be careful , Although some compilers support the representation of binary numbers , But you can't use printf Function outputs binary , This is a pity . Of course , Through the conversion function, other hexadecimal digits can be converted into binary digits , And store it as a string , And then in printf Use in a function %s Just output . Considering the foundation of readers is not enough , Let's not talk about this method here .
【 Learning exchange group :607439754】
【 Network disk free information package , Get it by yourself if necessary 】:
Embedded Internet of things 22 individual STM32 project 、 Competition works ,【 Huaqing vision issued the information package 】
http://makerschool.mikecrm.com/f4wjYBB【 Share some learning tutorials below , If you are interested, you can have a look 】:
C Fundamentals of language programming
Tamp C Language , The advanced road from Xiaobai to Daniel !
C The language expression program is written like this ,99.9% To succeed !
c Code debugging is more efficient in this way
边栏推荐
- [极客大挑战 2019]Secret File&文件包含常用伪协议以及姿势
- 标准C语言学习总结10
- 基于多模态融合的非遗图片分类研究
- LeetCode链表问题——142.环形链表II(一题一文学会链表)
- 这种动态规划你见过吗——状态机动态规划之股票问题(下)
- The Swedish court lifted the 5g spectrum auction ban on Huawei and ZTE
- openEuler Embedded SIG | 分布式软总线
- 网格数据生成函数meshgrid
- 1945. sum of digits after string conversion
- 【英雄哥七月集训】第 28天:动态规划
猜你喜欢

网格数据生成函数meshgrid

Cross domain transfer learning of professional skill word extraction in Chinese recruitment documents

Implementation of sequence table

Adventures of little mouse: behind the scenes gags of moss 2

Pytoch learning record (III): random gradient descent, neural network and full connection

Leetcode linked list problem -- 142. circular linked list II (learn the linked list by one question and one article)

Matlab | basic knowledge summary I
![[geek challenge 2019] secret file & file contains common pseudo protocols and gestures](/img/aa/a169cdd8cc6cdfda6d2777511b4dd2.png)
[geek challenge 2019] secret file & file contains common pseudo protocols and gestures

磷脂偶联抗体/蛋白试剂盒的存储与步骤

凡尔赛天花板:“毕业两年月薪才35K,真是没出息啊~~”
随机推荐
Week 6 Linear Models for Classification (Part B)
物联网技术栈之网关技术
Divide and conquer, upload large files in pieces
Record some small requirements in the form of cases
面向千元级5G手机市场,联发科天玑700发布
基于知识元的外文专利文献知识描述框架
openEuler Embedded SIG | 分布式软总线
Hold high the two flags of 5g and AI: Ziguang zhanrui Market Summit is popular in Shencheng
kingbase中指定用户默认查找schema,或曰用户无法使用public schema下函数问题
基于Paragraph-BERT-CRF的科技论文摘要语步功能信息识别方法研究
微信小程序开发公司你懂得选择吗?
Information fusion method and application of expert opinion and trust in large group emergency decision-making based on complex network
基于属性词补全的武器装备属性抽取研究
kali里的powersploit、evasion、weevely等工具的杂项记录
B+ tree height calculation of MySQL
What is the purpose of database read-write separation [easy to understand]
How Oracle exports data (how Oracle backs up databases)
标准C语言学习总结10
Implementation of sequence table
Apple M1 processor details: performance and energy efficiency have doubled, and Intel Core i9 is no match!