学习与研究——Data Type

C/C++内置的基本数据类型分为两类,一是算术类型,二是空类型void。

一、C算术类型

1
5
6
7
2

基本数据类型定义的基础

9

因此,可用以下代码来研究

3
4

二、用C++运行上述C算术类型的测试程序

#include <iostream>
#include <stdint.h>
using namespace std;

int main(){
	cout <<"Size of signed char:"<< sizeof(signed char)<<endl;
	cout <<"Size of unsigned char:"<< sizeof(unsigned char)<<endl;
	cout<<"---------------------------------------------------"<<endl;
	cout <<"Size of short:" <<sizeof(short)<<endl;
	cout <<"Size of unsigned short:"<<sizeof(unsigned short)<<endl;
	cout<<"---------------------------------------------------"<<endl;
	cout <<"Size of int:"<<sizeof(int)<<endl;
	cout <<"Size of unsigned int:"<<sizeof(unsigned int)<<endl;
	cout <<"Size of signed int:"<<sizeof(signed int)<<endl;
	cout<<"---------------------------------------------------"<<endl;
	
	cout <<"Size of short int:" <<sizeof(short int)<<endl;	
	cout <<"Size of signed short int:"<<sizeof(signed short int)<<endl;
	cout <<"Size of unsigned short int:"<<sizeof(unsigned short int)<<endl;
	cout<<"---------------------------------------------------"<<endl;
	cout <<"Size of long:"<<sizeof(long)<<endl;
	cout <<"Size of unsigned long:"<<sizeof(unsigned long)<<endl;
	cout << "Size of long int:" <<sizeof(long int)<<endl;
	cout <<"Size of signed long int:"<<sizeof(signed long int)<<endl;
	cout <<"Size of unsigned long int:"<<sizeof(unsigned long int)<<endl;
	cout<<"---------------------------------------------------"<<endl;
	cout<<"Size of int32_t:"<<sizeof(int32_t)<<endl;
	cout<<"Size of uint32_t:"<<sizeof(uint32_t)<<endl;
	cout<<"---------------------------------------------------"<<endl;
	//cout<<"Size of int64_t:"<<sizeof(int64_t)<<endl;
	//cout<<"Size of uint64_t:"<<sizeof(uint64_t)<<endl;
	cout<<"---------------------------------------------------"<<endl;
	cout<<"Size of char * :"<<sizeof(char * )<<endl;
	cout<<"---------------------------------------------------"<<endl;
	cout<<"Size of float:"<<sizeof(float)<<endl;
	cout<<"Size of double:"<<sizeof(double)<<endl;
	cout<<"Size of long double:"<<sizeof(long double)<<endl;
	
	cout<<"Size of wchar_t:"<<sizeof(wchar_t)<<endl;
	return 0;

}

  uint32_t 是一个在C和C++中常用的无符号32位整数类型,它定义在标准库的头文件中,在C语言中要包含 <stdint.h> 头文件来定义,C++用 来定义。要求编译器支持 C++11 或更高版本,因为 是从 C++11 开始引入的。如果使用较旧的编译器或者编译选项不支持 C++11,需要更新编译器或修改编译选项以支持 C++11,例如,在 g++ 或 clang++ 中,你可以使用 -std=c++11 或更高版本。

# include <stdio.h>
using namespace std;
typedef unsigned char* byte_pointer;

void show_bytes(byte_pointer start, size_t len) {
	size_t i;
	for (i = 0;i < len;i++)
		printf("%.2x", start[i]);
	printf("\n");
}

void show_int(int x)
{
	show_bytes((byte_pointer)&x, sizeof(int));
}

void show_float(float x)
{
	show_bytes((byte_pointer)&x, sizeof(float));
}

void show_pointer(void* x)
{
	show_bytes((byte_pointer)&x, sizeof(void*));
}

void test_show_bytes(int val) {
	int ival = val;
	float fval = (float)ival;
	int* pval = &ival;
	show_int(ival);
	show_float(fval);
	show_pointer(pval);
}

相关知识点:

  在 C\C++ 中,size_t 是一个无符号整数类型,通常用于表示对象的大小或数组的索引。它是由标准库头文件 <stddef.h> 或 定义的。size_t 的主要目的是提供一种与平台无关的方法来表示内存区域的大小。
  size_t 的定义因系统而异。在 32 位系统上,size_t 通常定义为 unsigned int,即 32 位无符号整型;在 64 位系统上,size_t 通常定义为 unsigned long,即 64 位无符号整型。size_t 常用于以下场景:
数组索引:由于 size_t 是无符号的,因此它适合用于表示数组的索引,避免负数带来的问题。
内存管理函数:如 malloc、memcpy 和 strlen 等函数的参数或返回值类型。例如:
void *malloc(size_t size);
void *memcpy(void *dest, const void *src, size_t n);
size_t strlen(const char *str);

参考:

输出类型字节表示
深入理解计算机系统基础 show_bytes函数
https://www.cnblogs.com/lyraHeartstrings/category/1983569.html

posted @ 2025-07-05 15:30  gdyyx  阅读(11)  评论(0)    收藏  举报