c - what does "representable" mean in C11? -
according c11 wg14 draft version n1570:
the header
<ctype.h>
declares several functions useful classifying , mapping characters. in cases argumentint
, value of shall representableunsigned char
or shall equal value of macroeof
. if argument has other value, behavior undefined.
is undefined behaviour?:
#include <ctype.h> #include <limits.h> #include <stdlib.h> int main(void) { char c = char_min; /* let assume char signed , char_min < 0 */ return isspace(c) ? exit_failure : exit_success; }
does standard allow pass char
isspace()
(char
int
)? in other words, char
after conversion int
representable unsigned char
?
here's how wiktionary defines "representable":
capable of being represented.
is char
capable of being represented unsigned char
? yes. §6.2.6.1/4:
values stored in non-bit-field objects of other object type consist of n
×
char_bit
bits, n size of object of type, in bytes. value may copied object of type unsigned char [n] (e.g., memcpy); resulting set of bytes called object representation of value.
sizeof(char) == 1
therefore object representation unsigned char[1]
i.e., char
capable of being represented unsigned char
. wrong?
concrete example, can represent [-2, -1, 0, 1]
[0, 1, 2, 3]
. if can't why?
related: according §6.3.1.3 isspace((unsigned char)c)
portable if int_max >= uchar_max
otherwise implementation-defined.
under assumption char signed undefined behavior, otherwise defined since char_min
have value 0
. easier see intention , meaning of:
the value of shall representable unsigned char or shall equal value of macro eof
if read section 7.4
character handling <ctype.h> rationale international standard—programming languages—c says (emphasis mine going forward):
since these functions used macros, their domain restricted small positive integers representable in unsigned char, plus value of eof. eof traditionally -1, may negative integer, , hence distinguishable valid character code. these macros may efficiently implemented using argument index small array of attributes.
so valid values are:
- positive integers can fit unsigned char
eof
implementation defined negative number
even though c99 rationale since particular wording referring not change c99 c11 , rationale still fits.
we can find why interface uses int argument opposed char, section 7.1.4
use of library functions, says:
all library prototypes specified in terms of “widened” types argument formerly declared char written int. ensures library functions can called or without prototype in scope, maintaining backwards compatibility pre-c89 code. note, however, since functions printf , scanf use variable-length argument lists, must called in scope of prototype.
Comments
Post a Comment