C语言数据类型——整理
程序员文章站
2024-03-18 19:59:22
...
C定义的5种整型数据类型
测试代码
程序说明:sizeof是字节操作符,使用方式,sizeof(数据类型)。sizeof的作用是得到数据类型所占的字节数。
#include<stdio.h>
int main(void) {
printf("sizeof(signed char) = %d/n", sizeof(signed char));
printf("sizeof(short int) = %d/n", sizeof(short int)); /* sizeof的结果都是int型 */
printf("sizeof(int) = %d/n", sizeof(int));
printf("sizeof(long int) = %d/n", sizeof(long int));
printf("sizeof(long long int) = %d/n", sizeof(long long int));
return 0;
}
64位编译环境运行结果
sizeof(signed char) = 1/nsizeof(short int) = 2/nsizeof(int) = 4/nsizeof(long int) = 8/nsizeof(long long int) = 8/n
无符号整型
#include<stdio.h>
int main(void) {
printf("sizeof(unsigned char) = %d/n", sizeof(unsigned char));
printf("sizeof(unsigned short int) = %d/n", sizeof(unsigned short int)); /* sizeof的结果都是int型 */
printf("sizeof(unsigned int) = %d/n", sizeof(unsigned int));
printf("sizeof(unsigned long int) = %d/n", sizeof(unsigned long int));
printf("sizeof(unsigned long long int) = %d/n", sizeof(unsigned long long int));
return 0;
}
浮点型数据类型
C标准中对不同类型的浮点数有不同的规定,编译器不同或硬件条件不同,字节长度也不相同。
测试代码
#include<stdio.h>
int main(void) {
printf("sizeof(float) = %d/n", sizeof(float));
printf("sizeof(double) = %d/n", sizeof(double));
printf("sizeof(long double) = %d/n", sizeof(long double));
return 0;
}
引用
http://www.360doc.com/content/13/0925/09/9133899_316913649.shtml
上一篇: 100行代码教你实现贪吃蛇小游戏