如何编程输出所有数据类型的所占字节数和取值范围
时间: 2023-04-07 20:01:24 浏览: 165
可以使用 sizeof 运算符来输出数据类型所占字节数,使用头文件 limits.h 来输出数据类型的取值范围。以下是一个示例代码:
```c
#include <stdio.h>
#include <limits.h>
int main() {
printf("char: %lu bytes, range: %d to %d\n", sizeof(char), CHAR_MIN, CHAR_MAX);
printf("short: %lu bytes, range: %d to %d\n", sizeof(short), SHRT_MIN, SHRT_MAX);
printf("int: %lu bytes, range: %d to %d\n", sizeof(int), INT_MIN, INT_MAX);
printf("long: %lu bytes, range: %ld to %ld\n", sizeof(long), LONG_MIN, LONG_MAX);
printf("float: %lu bytes, range: %e to %e\n", sizeof(float), FLT_MIN, FLT_MAX);
printf("double: %lu bytes, range: %e to %e\n", sizeof(double), DBL_MIN, DBL_MAX);
printf("long double: %lu bytes, range: %Le to %Le\n", sizeof(long double), LDBL_MIN, LDBL_MAX);
return 0;
}
```
阅读全文