定义类型uint8_t,uint32_t

定义的类型uint8_t,uint32_t能更明显的显示所占字节数。uint8_t表示占1个字节(1 字节=8 bit), uint32_t表示占4个字节((4 字节=32 bit)。

 1 #include<stdio.h>
 2  void main(void)
 3 {
 4    typedef unsigned char uint8_t;
 5    typedef unsigned int uint32_t;
 6    int size_char   = sizeof(char);
 7    int size_int    = sizeof(int);
 8    int size_uint8  = sizeof(uint8_t);
 9    int size_uint32 = sizeof(uint32_t);
10    printf("the sizeof:
char:%d;
int:%d;
uint8_t:%d;
uint32_t:%d;
",
11           size_char,size_int,size_uint8,size_uint32);
12 }

运行结果:

$ gcc -o an_example an_example.c
$ ./an_example
the sizeof:
char:1;
int:4;
uint8_t:1;
uint32_t:4;

 类型转换

 1 #include<stdio.h>
 2  void main(void)
 3 {
 4    typedef  int uint8_t;
 5    typedef  int uint32_t;
 6    uint32_t a=300;
 7    uint8_t b=2;
 8    b=(uint8_t)a;
 9    printf("a=%d,  b=%d
",a,b);
10 }

运行结果

a=300,  b=300

 参考:

[1] 类型转换 http://www.cnblogs.com/gaoshanxiaolu/p/3601038.html

[2] 类型转换 http://blog.csdn.net/shenwansangz/article/details/50186495

原文地址:https://www.cnblogs.com/abc36725612/p/5945804.html