11/*
22# ctype.h
33
4- Character classficiation and conversion functions.
4+ Character classification and conversion functions.
5+
6+ # unsigned char typecast
7+
8+ I think it is required to avoid undefined behaviour, because
9+ char may or not be signed
10+ http://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default
11+ and ctype functions UB if input is neither fits into `unsigned char` nor is EOF.
12+
13+ Mentioned at: http://www.greenend.org.uk/rjk/tech/cfu.html
14+
15+ Conversions to unsigned types are always defined (modulo),
16+ and char -> unsigned char is a bijection.
517*/
618
719#include "common.h"
820
9- int main () {
21+ int main (void ) {
1022 /* # isspace */
1123 {
12- assert (isspace (' ' ));
13- assert (isspace ('\n' ));
14- assert (!isspace ('a' ));
24+ assert (isspace (( unsigned char ) ' ' ));
25+ assert (isspace (( unsigned char ) '\n' ));
26+ assert (!isspace (( unsigned char ) 'a' ));
1527 }
1628
1729 /* # isdigit */
1830 {
19- assert (isdigit ('0' ));
20- assert (!isdigit ('a' ));
31+ assert (isdigit (( unsigned char ) '0' ));
32+ assert (!isdigit (( unsigned char ) 'a' ));
2133 }
2234
2335 /* # ispunct */
2436 {
25- assert (ispunct ('"' ));
26- assert (ispunct ('(' ));
27- assert (ispunct ('.' ));
28- assert (!ispunct ('a' ));
29- assert (!ispunct ('0' ));
37+ assert (ispunct (( unsigned char ) '"' ));
38+ assert (ispunct (( unsigned char ) '(' ));
39+ assert (ispunct (( unsigned char ) '.' ));
40+ assert (!ispunct (( unsigned char ) 'a' ));
41+ assert (!ispunct (( unsigned char ) '0' ));
3042 }
3143
3244 /*
@@ -40,8 +52,8 @@ int main() {
4052 http://stackoverflow.com/questions/2661766/c-convert-a-mixed-case-string-to-all-lower-case
4153 */
4254 {
43- assert (tolower ('A' ) == 'a' );
44- assert (toupper ('a' ) == 'A' );
55+ assert (tolower (( unsigned char ) 'A' ) == 'a' );
56+ assert (toupper (( unsigned char ) 'a' ) == 'A' );
4557 }
4658
4759 return EXIT_SUCCESS ;
0 commit comments