本文介绍了glibc中的strtoul的实现是否与C11标准冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是由glibc实现的stdlib.h中的功能strtoul的描述:

The follows is the description of function strtoul in stdlib.h implemented by glibc:

strtoul(字符串到无符号长整数")函数类似于strtol,不同之处在于它转换为无符号long int值.语法与上述strtol相同.溢出时返回的值为ULONG_MAX(请参见类型范围).

The strtoul ("string-to-unsigned-long") function is like strtol except it converts to an unsigned long int value. The syntax is the same as described above for strtol. The value returned on overflow is ULONG_MAX (see Range of Type).

如果字符串表示负数,则strtoul的作用与strtol相同,但是将结果强制转换为无符号整数.例如,这意味着strtoul在"-1"上返回ULONG_MAX,而比LONG_MIN更负的输入返回(ULONG_MAX + 1)/2.

If string depicts a negative number, strtoul acts the same as strtol but casts the result to an unsigned integer. That means for example that strtoul on "-1" returns ULONG_MAX and an input more negative than LONG_MIN returns (ULONG_MAX + 1) / 2.

如果基数超出范围,则strtoul将errno设置为EINVAL,或者在溢出时将ERGE设置为ERANGE.

strtoul sets errno to EINVAL if base is out of range, or ERANGE on overflow.

这意味着,例如,"-2"将被转换为ULONG_MAX - 1.但是C11标准[7.22.1.4-8]说:

It means that, for example, "-2" will be converted to ULONG_MAX - 1. But the C11 standard [7.22.1.4-8] says:

因此,按标准,例如,"-2"应转换为ULONG_MAX.有冲突吗?

So by the standard, for example, "-2" shall be converted to ULONG_MAX. Is it a conflict?

推荐答案

这可能是glibc在标准化发生之前实现功能的另一种情况.

This is probably another case of glibc implementing a feature before standardization occurred.

是的.

但是,我认为glibc的结果更有用.如果需要完全合规,则可以包装该函数以执行转换.

However, I consider glibc's result more useful. If you need perfect compliance, you can wrap the function to perform the conversion.

这篇关于glibc中的strtoul的实现是否与C11标准冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 07:29