C language data type deep detection

Data types in C language:

  • Basic types
    -integer types
    -floating point types
  • Structure type
    -array type
    -structure type
    -union (union) type
  • Pointer type
  • Void type (void)

Three elements of learning data types

  1. The width of the stored data
  2. Format of stored data
  3. Scope (scope)

Integer type

Integer types: char short int long
integer types are divided into signed (signed) and unsigned (unsigned) two

char 8BIT 1 byte
short 16BIT 2 bytes
int 32BIT 4 bytes
long 32BIT 4 bytes

How to prove the width (length) of the above types of data? Many positive people use sizeof (), strlen(), etc. In fact, vs. looking at disassembly is the most straightforward.

#include "stdio.h"

//探测数据宽度
void _test()
{
	char  i = 0xff;  //byte
	short j = 0xff;  //word
	int   k = 0xff;  //dword
	long  y = 0xff;  //dword
}

int main(int argc,char* argv[]) 
{
	_test();
	return 0;
}

_test() function, vs disassembly generated for it

//探测数据宽度
void _test()
{
//开栈
00CF16F0 55                   push        ebp  
00CF16F1 8B EC                mov         ebp,esp  
00CF16F3 81 EC F0 00 00 00    sub         esp,0F0h  

//保存现场
00CF16F9 53                   push        ebx  
00CF16FA 56                   push        esi  
00CF16FB 57                   push        edi  

//填充缓存区
00CF16FC 8D BD 10 FF FF FF    lea         edi,[ebp-0F0h]  
00CF1702 B9 3C 00 00 00       mov         ecx,3Ch  
00CF1707 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  
00CF170C F3 AB                rep stos    dword ptr es:[edi]  

//函数本身:定义了四个变量
	char  i = 0xff;  //byte
00CF170E C6 45 FB FF          mov         byte ptr [i],0FFh		//字节宽度 byte
	short j = 0xff;  //word
00CF1712 B8 FF 00 00 00       mov         eax,0FFh		
00CF1717 66 89 45 EC          mov         word ptr [j],ax		//字节宽度 word  
	int   k = 0xff;  //dword
00CF171B C7 45 E0 FF 00 00 00 mov         dword ptr [k],0FFh		//字节宽度dword  
	long  y = 0xff;  //dword
00CF1722 C7 45 D4 FF 00 00 00 mov         dword ptr [y],0FFh		//字节宽度dword
}

//恢复现场
00CF1729 5F                   pop         edi  
00CF172A 5E                   pop         esi  
00CF172B 5B                   pop         ebx  

//降低堆栈
00CF172C 8B E5                mov         esp,ebp  
00CF172E 5D                   pop         ebp  

//函数返回
00CF172F C3                   ret

This should be very clear, if we say, define a variable a: long long long a; its character width? For the same reason, just check the disassembly!

Floating point type

See: Data encoding on precision issues

Guess you like

Origin blog.csdn.net/qq1119278087/article/details/83834463