c语言入门经典第几版好,C语言入门经典(第5版)

f1259863c727685d93fda99ea7a2be31.png

当然,一定要确定程序中给定的整数类型可以储存的极限值。如前所述,头文件定义的符号表示每种种类的极限值。表2-9列出了对应于每种带符号整数类型的极限值符号名。

表2-9 表示整数类型的极限值的符号

类 型

下 限

上 限

CHAR_MIN

CHAR_MAX

short

SHRT_MIN

SHRT_MAX

INT_MIN

INT_MAX

LONG_MIN

LONG_MAX

long long

LLONG_MIN

LLONG_MAX

无符号整数类型的上限都是0,所以他们没有特定的符号。无符号整数类型的上限的符号分别是UCHAR_MAX、USHRT_MAX、UINT_MAX、ULONG_MAX和ULLONG_ MAX。

0c6fb0bc6f3d9562e1227e167b03be14.png

要在程序中使用这种符号,必须在源文件中添加头文件的#include指令:

#include

可以用最大值初始化一个int数组c语言入门经典 第5版,如下所示:

int number = INT_MAX;

这个语句把number的值修改为最大值,编译器会利用该最大值编译代码。

头文件定义了表示浮点数的符号,其中一些的技术成分很大,所以此处只介绍我们感兴趣的符号。3种浮点数类别可以表示的最大正值和最小正值如表2-10所示。还可以使用FLT_DIG、DBL_DIG和LDBL_DIG符号,它们指定了对应种类的二进制尾数可以表示的整数位数。下面用一个例子来表明能否使用表示整数和浮点数的符号。

表2-10 表示浮点数类型的极限值的符号

类 型

下 限

上 限

float

FLT_MIN

FLT_MAX

double

DBL_MIN

DBL_MAX

long double

LDBL_MIN

3f33c04d38edbb491478a883fde34d7e.png

LDBL_MAX

试试看:找出极限值

这个程序输出头文件中定义的符号的对应值。

// Program 2.11 Finding the limits

#include // For command line input and output

#include // For limits on integer types

#include // For limits on floating-point types

int main(void)

{

printf("Variables of type char store values from %d to %d\n", CHAR_MIN, CHAR_MAX);

printf("Variables of type unsigned char store values from 0 to %u\n", UCHAR_MAX);

printf("Variables of type short store values from %d to %d\n", SHRT_MIN, SHRT_MAX);

printf("Variables of type unsigned short store values from 0 to %u\n", USHRT_MAX);

printf("Variables of type int store values from %d to %d\n", INT_MIN, INT_MAX);

printf("Variables of type unsigned int store values from 0 to %u\n", UINT_MAX);

printf("Variables of type long store values from %ld to %ld\n", LONG_MIN, LONG_MAX);

printf("Variables of type unsigned long store values from 0 to %lu\n", ULONG_MAX);

printf("Variables of type long long store values from %lld to %lld\n", LLONG_MIN, LLONG_MAX);

e4fdd9cc19a6f2b2ed978a404744efc6.png

printf("Variables of type unsigned long long store values from 0 to %llu\n", ULLONG_MAX);

printf("\nThe size of the smallest positive non-zero value of type float is %.3e\n", FLT_MIN);

printf("The size of the largest value of type float is %.3e\n", FLT_MAX);

printf("The size of the smallest non-zero value of type double is %.3e\n", DBL_MIN);

printf("The size of the largest value of type double is %.3e\n", DBL_MAX);

printf("The size of the smallest non-zero value of type long double is %.3Le\n", LDBL_MIN);

printf("The size of the largest value of type long double is %.3Le\n", LDBL_MAX);

printf("\n Variables of type float provide %u decimal digits precision. \n", FLT_DIG);

printf("Variables of type double provide %u decimal digits precision. \n", DBL_DIG);

printf("Variables of type long double provide %u decimal digits precision. \n",

LDBL_DIG);

return 0;

}

结果如下所示:

Variables of type char store values from -128 to 127

Variables of type unsigned char store values from 0 to 255

Variables of type short store values from -32768 to 32767

Variables of type unsigned short store values from 0 to 65535

5df9b541f34c2c81da66d8122370befb.png

Variables of type int store values from -2147483648 to 2147483647

Variables of type unsigned int store values from 0 to 4294967295

Variables of type long store values from -2147483648 to 2147483647

Variables of type unsigned long store values from 0 to 4294967295

Variables of type long long store values from -9223372036854775808 to 9223372036854775807

Variables of type unsigned long long store values from 0 to 18446744073709551615

The size of the smallest positive non-zero value of type float is 1.175e-038

The size of the largest value of type float is 3.403e+038

The size of the smallest non-zero value of type double is 2.225e-308

The size of the largest value of type double is 1.798e+308

The size of the smallest non-zero value of type long double is 3.362e-4932

The size of the largest value of type long double is 1.190e+4932

Variables of type float provide 6 decimal digits precision.

Variables of type double provide 15 decimal digits precision.

Variables of type long double provide 18 decimal digits precision.

代码的说明

在一系列的printf()函数读取中,输出和头文件定义的符号的值。计算机中的数值总是受限于该机器可以储存的函数,这些符号的值表示每种数值类型的极限值。这里用说明符%u输出无符号整数值。如果用%d输出无符号类别的最大值,则最右边的位(带符号类别的符号位)为1的数值就得不到正确的解释。

对浮点数的极限值使用说明符%e,表示这个数值是指数形式。同时选定精确到小数点后的3位数,因为此处的输出不需要非常准确。printf()函数显示的值是long double类型时,需要使用L修饰符。L必须是大写c语言入门经典 第5版,这里没有使用斜体字母l。%f说明符表示没有指数的数值,它针对比较大或十分小的数来说相当不方便。在这个事例中试一试,就会知道其意义。

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/jisuanjixue/article-126074-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值