当前位置:网站首页>C language data type and uint8 under typedef_ t / uint32_ t
C language data type and uint8 under typedef_ t / uint32_ t
2022-07-20 09:51:00 【Continuous progress】
List of articles
Preface
Based on C Language code can always be seen uint8_t / uint16_t / uint32_t /uint64_t The figure of . Such as :uint32_t a = 300;
But it doesn't seem to belong to C In language 6 Basic data types (short、int、long、char、float、double), Then it is a new data type ?
This paper takes this problem as the starting point , Reviewed the C In language 6 Basic data types ; Describes the data types in different compilers 、 Word length differences in the platform ; And that leads to uint8_t / uint16_t / uint32_t /uint64_t The source and function of ; Finally, it introduces typedef The usage of define The difference between .
One 、C Language basic data type
C Language commonality 6 Basic data types , Namely :
1) integer :short、int、long;
2) floating-point :float、double;
3) Character type :char.
Two 、 The word length difference of data types under different compilers
C Linguistic 6 Among the three basic data types ,int type ( integer -short、int、long) A special , The specific number of bytes is related to the machine word length and the compiler . The following comparison data types are 16 position 、32 position 、64 Byte length under bit compiler , use sizeof( ) Function to get .
notes :
① The compiler bits here refer to the compiled software ( Applications ) Number of digits ;
② char Type essentially , It's also an integer type , It has a length of 1 The integer of , Usually used to store characters ASCII code .
16 Bit compiler :
char // 1 Bytes
char* // 2 Bytes (* Pointer to the variable )
// 16 The addressing space of bits is 2^16, namely 16 individual bit, namely 2 byte .(32 position 、64 The same goes for bit compilers )
short int // 2 Bytes
int // 2 Bytes
unsigned int // 2 Bytes Unsigned integer
float // 4 Bytes
double // 8 Bytes
long // 4 Bytes
long long // 8 Bytes
unsigned long // 4 Bytes
32 Bit compiler :
char // 1 Bytes
char* // 4 Bytes (* Pointer to the variable )(16&32&64 Bit machines are different )
short int // 2 Bytes
int // 4 Bytes (16 position -2B,32&64 position -4B(Byte))
unsigned int // 4 Bytes (16 position -2B,32&64 position -4B)
float // 4 Bytes
double // 8 Bytes
long // 4 Bytes (16&32 position -4B,64 position -8B)
long long // 8 Bytes
unsigned long // 4 Bytes (16&32 position -4B,64 position -8B)
64 Bit compiler :
char // 1 Bytes
char* // 8 Bytes
short int // 2 Bytes
int // 4 Bytes
unsigned int // 4 Bytes
float // 4 Bytes
double // 8 Bytes
long // 8 Bytes
long long // 8 Bytes
unsigned long // 8 Bytes
As shown above ,int,long int,short int The data bit width of is related to the compiler . But there are the following principles (ANSI/ISO formulate ), namely
1) sizeof(short int) <= sizeof(int) ;
2) sizeof(int) <= sizeof(long int) ;
3) short int At least it should be 16 position (2 byte ) ;
4) long int At least it should be 32 position .
3、 ... and 、uint8_t / uint16_t / uint32_t /uint64_t The source and function of
1、 source
*_t Indicates that the identification is made by typedef Defined by , It is a kind of annotation of structure .C In language code uint8_t / uint16_t / uint32_t /uint64_t Are not new data types , But through typedef Give a new name to the data type , Such as :
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
2、 effect
1) Increase the readability of the code
uint8_t,uint32_t It can more clearly display the number of bytes .
uint8_t Indicated occupation 1 Bytes (1 byte =8 bit);
uint32_t Indicated occupation 4 Bytes (4 byte =32 bit).
2) Increase code maintainability
When it comes to cross platform , Different platforms have different word lengths , So use precompile and typedef You can easily maintain the code .
notes :uint8_t It's actually a char, So the output uint8_t Variables of type actually output the corresponding characters , Instead of numbers , Such as :
uint8_t num=67;
cout << num << endl; // The output is C
Four 、typedef The usage of define The difference between
1、typedef Usage of
typedef Do not create new types , Instead, just add an alias to the existing type . It is often used to create type names that are easy to remember , Use it to express our true intention , Such as
typedef int int32_t;
// typedef Defined a int Synonyms of int32_t, Can be in any need int In the context of int32_t.
2、typedef And define The difference between
1)typedef Act a bit like define macro , Replace synonyms with their actual types ;
2) The difference is typedef It will be interpreted at compile time , So let the compiler deal with text substitution that exceeds the power of the preprocessor , namely Not a simple text replacement .
3)#define advantage : have access to #ifdef ,#ifndef Wait to make logical judgment , Use #undef To undefine .
4)typedef advantage : It is bound by scope rules , Use typedef The scope of the defined variable type is limited to the defined function or file ( Depending on where this variable is defined ), Macro definitions have no such constraints .
Case a :
typedef char *pStr;
define pStr char *;
Generally speaking , The above two definitions pStr Data type method ,typedef than define Macro is better , Especially when there is a pointer . Please see the example :
typedef char *PSTRING1;
#define PSTRING2 char *;
PSTRING1 p1, p2;
PSTRING2 p3, p4;
In the above definition of variables ,p1、p2、sp3 Are defined as char *,p4 It's defined as char, Instead of the pointer variable we expect , The root cause is define Just a simple string replacement ; and typedef To give a new name to a type .
notes : Use #define Definition time , If the definition contains an expression , Brackets... Must be used , namely #define f(x) (xx) ;
Case 2 :
typedef char * pStr;
char string[4] = "abc";
const char *p1 = string;
const pStr p2 = string;
p1++;
p2++;
In the above code const pStr p2 Is not the same as const char * p2;
const pStr p2: Limit the data type to char * The variable of p2 As read-only , therefore p2++ error ;
const char * p2(const The decoration is the front char): It can be applied to any position ( Non system sensitive areas ) Conduct “ read-only ” operation .(“ read-only ” Is relative to char * p2 For the limited content ).
summary
1) C Language commonality 6 Basic data types :short、int、long;float、double;char;
2) data type (int type ( integer -short、int、long) A special ), The specific number of bytes is related to the number of compiler bits ;
3) uint8_t / uint16_t / uint32_t /uint64_t Not a new data type , But through typedef A new name for the data type ;
4) typedef Reasonable use of can increase the readability of the code 、 Maintainability ;
5) typedef Not a simple text replacement ,#define Definition time , If the definition contains an expression , Brackets... Must be used , namely #define f(x) (xx) .
Reference resources :
https://blog.csdn.net/mary19920410/article/details/71518130
https://blog.csdn.net/bzhxuexi/article/details/19551979
https://blog.csdn.net/qq_44770155/article/details/90602325
边栏推荐
- Construction of virtual host (multiple sites)
- Interpretation of the paper: Transformers make strong encoders for medical image segmentation
- PCB在导出gerber文件时过孔盖油设置方法
- When the vivado project version is upgraded, the relevant IP version IP status displays using cached IP results
- Interpretation of Bix NAS: searching effective bi directional architecture for medical image segmentation
- 一分钟掌握卡诺图化简法
- How does mindspore view the model parameter quantity?
- FPGA——SPI总线详解(概念)
- 线性卷积、循环卷积、周期卷积的定义、计算方法及三者之间的关系
- The end result of printf - where is the data printed
猜你喜欢
基于定时器捕获功能的红外解码程序(NEC协议)
一分钟掌握卡诺图化简法
提升开发效率的 Chrome 开发者工具快捷键参考
Interpretation of CO correcting: noise tolerant medical image classification via mutual label correction
內網滲透隧道技術的相關知識
G2l series core board -rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
Automatic nucleic acid extraction instrument based on ARM core board
vivado2018.2版本带PS侧配置(bd)调用modelsim仿真时:(vlog-13006) Could not find the package (sc_util_v1_0_3_pkg)
ZYNQ ARM核之SCU
Mysql45 talking about reading notes in simple terms index (5)
随机推荐
基于飞凌NXP i.MX6ULL的无线网络测试
基于循环卷积的一维小波变换程序验证(C语言)
全志A40i网卡软件问题怎么办
FPGA网口实现与详解(2)
Bing必应搜索用不了方法
Stm32f4 uses a timer to output multiple PWM waves with different frequency duty cycles (including codes)
Xilinx 7系列原语使用(时钟相关)——(一)
Weakly supervised Semantic Segmentation by Pixel-to-Prototype Contrast
RS232标准DB9接口定义
G2L系列 核心板 -RZ/G2L 處理器簡介|框架圖|功耗|原理圖及硬件設計指南
FPGA刷题P2:多功能数据处理器、求两个数的差值、使用generate...for语句简化代码、使用子模块实现三输入数的大小比较、使用函数实现数据大小端转化
基于瑞芯微3568核心板实现的智能网关
Jetson Xavier NX source code compilation and installation ROS melody experience (failed)
vulnhub 靶机 GOLDENEYE: 1
FPGA 多数表决器(含代码)
RTOS——桌面mini网络时钟
【复旦微】国产MCU学习(持续更新)
Intelligent gateway based on Ruixin micro 3568 core board
SCU of zynq ARM core
【Vscode配置Markdown环境】亲测好用~