当前位置:网站首页>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
边栏推荐
- 怎样巧用断言+异常处理类,使代码更简洁!(荣耀典藏版)
- Matlab | basic knowledge summary I
- LeetCode链表问题——142.环形链表II(一题一文学会链表)
- LeetCode·581.最短无序连续子数组·双指针
- An end-to-end aspect level emotion analysis method for government app reviews based on brnn
- 技术选型Rust——事后分析
- Bully is filed for bankruptcy! The company has become a "Lao Lai", and the legal person is restricted from high consumption
- 不用Swagger,那我用啥?
- 瑞典法院取消对华为和中兴的5G频谱拍卖禁令
- Research on the recognition method of move function information of scientific paper abstract based on paragraph Bert CRF
猜你喜欢

msfvenom制作主控与被控端

Which brand is the best and most cost-effective open headset

Versailles ceiling: "the monthly salary of two years after graduation is only 35K, which is really unpromising ~ ~"

Mesh data generation function meshgrid

基于多模态融合的非遗图片分类研究

中文招聘文档中专业技能词抽取的跨域迁移学习
![[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

PyQt5快速开发与实战 5.4 网页交互

Miscellaneous records of powersploit, evaluation, weevery and other tools in Kali

Adventures of little mouse: behind the scenes gags of moss 2
随机推荐
针对下一代Chromebook,联发科推出新款芯片组MT8192和MT8195
基于Xception-TD的中华传统刺绣分类模型构建
二 RedisTemplate的序列和反序列化机制讲解
分而治之,大型文件分片上传
Assign a string pointer to an array [easy to understand]
Research on intangible cultural heritage image classification based on multimodal fusion
凡尔赛天花板:“毕业两年月薪才35K,真是没出息啊~~”
数据插值——对不同量级的数据进行归一化
节省70%的显存,训练速度提高2倍!浙大&阿里提出在线卷积重新参数化OREPA,代码已开源!(CVPR 2022 )
[极客大挑战 2019]Secret File&文件包含常用伪协议以及姿势
Huawei releases the first electric drive system driveone: charging for 10 minutes, endurance of 200km
Mysql的B+树高度计算
开放式耳机哪个音质好、公认音质好的气传导耳机推荐
First week of internship diary
The Swedish court lifted the 5g spectrum auction ban on Huawei and ZTE
磷脂偶联抗体/蛋白试剂盒的存储与步骤
Detailed explanation of JVM memory layout (glory collection version)
Leetcode 19. delete the penultimate node of the linked list [knowledge points: speed pointer, recursion, stack]
Skiasharp's WPF self drawn drag ball (case version)
Bully is filed for bankruptcy! The company has become a "Lao Lai", and the legal person is restricted from high consumption