用字节展示数值存储
- 代码部分
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //包含strlen函数,strcpy函数
typedef unsigned char *byte_pointer; //用typedef将byte_pointer定义为指向unsigned char的对象的指针
//typedef char *byte_pointer;
//typedef int *byte_pointer;
void show_bytes(byte_pointer start, size_t len) {
size_t i;
for (i = 0; i < len; i++)
printf("%p\t0x%.2x\n", &start[i], start[i]); //依次打印每个字节的存放位置和存储内容
printf("\n"); //c语言中指光标移至行首再换行\r\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;
double fval = (double) ival;
int *pval = &ival;
printf("Stack variable ival = %d\n", ival);
printf("(int)ival:\n");
show_int(ival);
printf("(float)ival:\n");
show_float(fval);
printf("&ival:\n");
show_pointer(pval);
}
void simple_show_a() { //展示一个整型变量第1,2,3个字节的存放
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
}
void simple_show_b() {
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
}
void float_eg() {
int x = 3490593; //将一个整型变量转换为浮点型
float f = (float) x;
printf("For x = %d\n", x);
show_int(x);
show_float(f);
x = 3510593; //将一个浮点型变量转换为整型
f = (float) x;
printf("For x = %d\n", x);
show_int(x);
show_float(f);
}
void string_ueg() { //展示一个字符串的存放
/* $begin show-ustring */
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s));
/* $end show-ustring */
}
void string_leg() {
/* $begin show-lstring */
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s));
/* $end show-lstring */
}
void show_twocomp() //展示x和-x的存放(探究负数的补码存储形式)
{
short x = 12345;
short mx = -x;
show_bytes((byte_pointer) &x, sizeof(short));
show_bytes((byte_pointer) &mx, sizeof(short));
}
int main(int argc, char *argv[])
{
int val = 12345;
if (argc > 1) {
val = strtol(argv[1], NULL, 0);
printf("calling test_show_bytes\n");
test_show_bytes(val);
} else {
printf("calling show_twocomp\n");
show_twocomp();
printf("Calling simple_show_a\n");
simple_show_a();
printf("Calling simple_show_b\n");
simple_show_b();
printf("Calling float_eg\n");
float_eg();
printf("Calling string_ueg\n");
string_ueg();
printf("Calling string_leg\n");
string_leg();
}
return 0;
}
注意:
size_f是一种“整型”类型,里面保存的是一个整数,就像int, long那样。这种整数用来记录一个大小(size)。size_t的全称应该是size type,就是说“一种用来记录大小的数据类型”。
通常我们用sizeof(XXX)操作,这个操作所得到的结果就是size_t类型。
因为size_t类型的数据其实是保存了一个整数,所以它也可以做加减乘除,也可以转化为int并赋值给int类型的变量。
strtol函数:long int strtol(const char *nptr, char **endptr, int base)
strtol()会将nptr指向的字符串,根据参数base,按权转化为long int, 然后返回这个值。
- 实验结果
下面是a.out后面带参数的运行结果
下面将原代码略做更改
//typedef unsigned char *byte_pointer;
//typedef char *byte_pointer;
typedef int *byte_pointer;
不能将unsigned char改为int,这里原来一个地址单元用1个字节表示,现在变成了4个字节,start[i]被当作int型
//typedef unsigned char *byte_pointer;
typedef char *byte_pointer;
//typedef int *byte_pointer;
不能将unsigned char改为char,若某字节数值超过127(char的正范围),但打印格式是16进制,这时程序按照补码填充,就会在前面出现很多f