C语言打印变量的各个字节内容

https://bbs.csdn.net/topics/390514117

#include <stdio.h>
int print_all_byte(void *addr, int size)
{
	unsigned char *ptr = addr; 
	int print_bytes = 0; 

	if(NULL == ptr) {
		return -1; 
	}

	while(print_bytes < size) {
		printf("%02x", *ptr); 
		ptr++; 
		print_bytes++; 
	}
	printf("\n"); 
	return print_bytes; 
}

int main(void)
{
	int d1 = 0x12345678; 
	long d2 = 0x87654321; 
	double d3 = 0.12345678; 
	int arr1[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 1234}; 

	if(sizeof(d1) == print_all_byte((void *)&d1, sizeof(d1))) {
		printf("print int success!\n"); 
	}

	if(sizeof(d2) == print_all_byte((void *)&d2, sizeof(d2))) {
		printf("print long success!\n"); 
	}

	if(sizeof(d3) == print_all_byte((void *)&d3, sizeof(d3))) {
		printf("printf double success!\n"); 
	}

	if(sizeof(arr1) == print_all_byte((void *)arr1, sizeof(arr1))) {
		printf("printf array success!\n"); 
	}
	return 0; 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值