Does isspace() accept getchar() values?

后端 未结 2 1415
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 17:59

isspace() works if the input is representable as unsigned char or equal to EOF.

getchar() reads the next character from stdin.

When getch

2条回答
  •  自闭症患者
    2021-01-19 18:44

    According to C11 WG14 draft version N1570:

    §7.21.7.6/2 The getchar function is equivalent to getc with the argument stdin.

    §7.21.7.5/2 The getc function is equivalent to fgetc...

    §7.21.7.1/2 [!=EOF case] ...the fgetc function obtains that character as an unsigned char converted to an int...text in [...] is mine.

    i.e.,

    • isspace() accepts getchar() values
    • all getchar()!=EOF values are representable as unsigned char
    • there is no undefined behavior here.

    If you think it is too obvious ("what else can it be"), think again. For example, in the related case: isspace(CHAR_MIN) may be undefined i.e., it may be undefined behavior to pass a character to a character classification function!

    If UCHAR_MAX > INT_MAX the result may be implementation-defined:

    §6.3.1.3/3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

提交回复
热议问题