What does “representable” mean in C11?

前端 未结 3 1026
自闭症患者
自闭症患者 2020-12-06 18:48

According to C11 WG14 draft version N1570:

The header declares several functions useful for classifying and mapping cha

3条回答
  •  遥遥无期
    2020-12-06 19:02

    The revealing quote (for me) is §6.3.1.3/1:

    if the value can be represented by the new type, it is unchanged.

    i.e., if the value has to be changed then the value can't be represented by the new type.

    Therefore an unsigned type can't represent a negative value.

    To answer the question in the title: "representable" refers to "can be represented" from §6.3.1.3 and unrelated to "object representation" from §6.2.6.1.

    It seems trivial in retrospect. I might have been confused by the habit of treating b'\xFF', 0xff, 255, -1 as the same byte in Python:

    >>> (255).to_bytes(1, 'big')
    b'\xff'
    >>> int.from_bytes(b'\xFF', 'big')
    255
    >>> 255 == 0xff
    True
    >>> (-1).to_bytes(1, 'big', signed=True)
    b'\xff'
    

    and the disbelief that it is an undefined behavior to pass a character to a character classification function e.g., isspace(CHAR_MIN).

提交回复
热议问题