时间:2022-01-27 08:49:17 | 栏目:C代码 | 点击:次
具体代码如下所示:
#include<stdio.h> void main() { //使用sizeof验证数据类型在内存中占用的空间大小 // char // int // short // long // unsigned int // float // double // long double printf("在64位机器中:\n"); printf(" char类型占%d个字节\n",sizeof(char)); printf(" short类型占%d个字节\n",sizeof(short)); printf(" int类型占%d个字节\n",sizeof(int)); printf(" long类型占%d个字节\n",sizeof(long)); printf("unsigned int类型占%d个字节\n",sizeof(unsigned int)); printf(" float类型占%d个字节\n",sizeof(float)); printf(" double类型占%d个字节\n",sizeof(double)); printf(" long double类型占%d个字节\n",sizeof(long double)); printf("-------------------指针占4个字节---------------------\n"); char *p0; short *p1; int *p2; long *p3; unsigned int *p4; float *p5; double *p6; long double *p7; printf(" char类型指针占%d个字节\n",sizeof(p0)); printf(" short类型指针占%d个字节\n",sizeof(p1)); printf(" int类型指针占%d个字节\n",sizeof(p2)); printf(" long类型指针占%d个字节\n",sizeof(p3)); printf("unsigned int类型指针占%d个字节\n",sizeof(p4)); printf(" float类型指针占%d个字节\n",sizeof(p5)); printf(" double类型指针占%d个字节\n",sizeof(p6)); printf(" long double类型指针占%d个字节\n",sizeof(p7)); }
结果:
在64位机器中: char类型占1个字节 short类型占2个字节 int类型占4个字节 long类型占4个字节 unsigned int类型占4个字节 float类型占4个字节 double类型占8个字节 long double类型占12个字节 -------------------指针占4个字节--------------------- char类型指针占4个字节 short类型指针占4个字节 int类型指针占4个字节 long类型指针占4个字节 unsigned int类型指针占4个字节 float类型指针占4个字节 double类型指针占4个字节 long double类型指针占4个字节
知识点扩展:
c语言实现十进制与ASCII码互转
//十进制转ASCII码 //年#define KB2ASC(x) (u8)(0x30+x) //ASCII码转十进制 //年 #define ASC2KB(x) (u8)(x%(0x30))
总结