Knowledge

C character classification

Source 📝

1344: 22: 386:
For example, an array of 256 eight-bit integers, arranged as bitfields, is created, where each bit corresponds to a particular property of the character, e.g., isdigit, isalpha. If the lowest-order bit of the integers corresponds to the isdigit property, the code could be written as
283:. These functions are used to test characters for membership in a particular class of characters, such as alphabetic characters, control characters, etc. Both single-byte, and wide characters are supported. 383:
Unlike the above example, the character classification routines are not written as comparison tests. In most C libraries, they are written as static table lookups instead of macros or functions.
371:
As this may be expressed in multiple formulations, it became desirable to introduce short, standardized forms of such tests that were placed in the system-wide header file
871: 259: 86: 39: 58: 65: 72: 864: 1184: 54: 1348: 105: 252: 1165: 1074: 857: 1283: 416: 43: 1303: 1255: 1372: 1313: 1298: 1004: 245: 171: 79: 1260: 1308: 1014: 911: 906: 901: 161: 1288: 999: 947: 880: 280: 166: 151: 1151: 1126: 32: 1169: 1111: 829: 1131: 646: 186: 8: 1146: 1141: 1103: 994: 156: 1212: 977: 942: 767: 582: 276: 121: 752: 737: 718: 699: 680: 661: 639: 620: 601: 575: 563: 544: 525: 506: 487: 1045: 1040: 1009: 952: 800: 730: 711: 692: 673: 654: 632: 613: 594: 556: 537: 518: 499: 480: 296: 181: 176: 146: 782: 411:
This can cause problems if when the macro expands, the expression substituted for
1156: 1116: 1024: 431:
is evaluated twice. For this reason, the table-based approach is generally used.
1329: 1174: 1136: 1062: 962: 303:
character set, the following expression identifies a letter, when its value is
1366: 1232: 1222: 1161: 1202: 967: 406:#define isdigit(x) ((x) >= '0' && (x) <= '9') 136: 1293: 849: 937: 916: 447:
in C++). The functions that operate on wide characters are defined in
299:
for classifying characters into different types. For example, for the
439:
The functions that operate on single-byte characters are defined in
21: 1237: 1227: 1207: 1052: 1019: 957: 458:
The classification is evaluated according to the effective locale.
401:
used a potentially faulty method similar to the first code sample:
231: 222: 204: 921: 213: 1192: 1089: 1084: 891: 1278: 1217: 1197: 1121: 1069: 1057: 398: 300: 1079: 292: 896: 275:
is an operation provided by a group of functions in the
667:
checks whether the operand is a blank space character
758:
checks whether the operand falls into specific class
427:. It is not immediately evident that the argument to 686:
checks whether the operand is a printable character
626:
checks whether the operand is a graphical character
46:. Unsourced material may be challenged and removed. 607:checks whether the operand is a control character 806:returns a transformation mapping to be used with 1364: 788:returns a wide character class to be used with 773:converts the operand using a specific mapping 865: 253: 291:Early C-language programmers working on the 550:checks whether the operand is an uppercase 493:checks whether the operand is alphanumeric 879: 872: 858: 705:checks whether the operand is punctuation 588:checks whether the operand is hexadecimal 260: 246: 512:checks whether the operand is alphabetic 106:Learn how and when to remove this message 531:checks whether the operand is lowercase 434: 1365: 569:checks whether the operand is a digit 853: 44:adding citations to reliable sources 15: 13: 743:converts the operand to uppercase 724:converts the operand to lowercase 392:#define isdigit(x) (TABLE & 1) 14: 1384: 845: 378: 1343: 1342: 795: 777: 762: 747: 20: 831:ISO/IEC 9899:1999 specification 31:needs additional citations for 822: 645:checks whether the operand is 1: 815: 419:. For example, if one calls 55:"C character classification" 7: 425:isdigit(run_some_program()) 295:operating system developed 10: 1389: 1256:Compatibility of C and C++ 286: 273:C character classification 1338: 1322: 1269: 1246: 1183: 1102: 1033: 985: 976: 930: 887: 403: 389: 309: 142:Character classification 277:ANSI C Standard Library 1261:Comparison with Pascal 881:C programming language 281:C programming language 435:Overview of functions 195:Miscellaneous headers 40:improve this article 1373:C standard library 397:Early versions of 297:programming idioms 187:Alternative tokens 122:C standard library 1358: 1357: 1098: 1097: 813: 812: 270: 269: 172:Memory allocation 157:File input/output 116: 115: 108: 90: 1380: 1346: 1345: 983: 982: 978:Standard library 874: 867: 860: 851: 850: 839: 838: 837:. p. 193, § 7.4. 836: 826: 809: 803: 791: 785: 770: 755: 740: 733: 721: 714: 702: 695: 683: 676: 664: 657: 642: 635: 623: 616: 604: 597: 585: 578: 566: 559: 547: 540: 528: 521: 509: 502: 490: 483: 461: 460: 407: 393: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 337: 334: 331: 328: 325: 322: 319: 316: 313: 262: 255: 248: 235: 226: 217: 208: 118: 117: 111: 104: 100: 97: 91: 89: 48: 24: 16: 1388: 1387: 1383: 1382: 1381: 1379: 1378: 1377: 1363: 1362: 1359: 1354: 1334: 1318: 1271: 1265: 1249:other languages 1248: 1247:Comparison with 1242: 1179: 1117:Borland Turbo C 1094: 1034:Implementations 1029: 972: 926: 883: 878: 848: 843: 842: 834: 828: 827: 823: 818: 807: 799: 789: 781: 766: 751: 736: 729: 717: 710: 698: 691: 679: 672: 660: 653: 638: 631: 619: 612: 600: 593: 581: 574: 562: 555: 543: 536: 524: 517: 505: 498: 486: 479: 470: 465: 437: 409: 408: 405: 395: 394: 391: 381: 369: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 289: 266: 229: 220: 211: 202: 177:Process control 112: 101: 95: 92: 49: 47: 37: 25: 12: 11: 5: 1386: 1376: 1375: 1356: 1355: 1353: 1352: 1339: 1336: 1335: 1333: 1332: 1330:Dennis Ritchie 1326: 1324: 1320: 1319: 1317: 1316: 1311: 1306: 1301: 1296: 1291: 1286: 1281: 1275: 1273: 1267: 1266: 1264: 1263: 1258: 1252: 1250: 1244: 1243: 1241: 1240: 1235: 1230: 1225: 1220: 1215: 1210: 1205: 1200: 1195: 1189: 1187: 1181: 1180: 1178: 1177: 1172: 1159: 1154: 1149: 1144: 1139: 1134: 1129: 1124: 1119: 1114: 1108: 1106: 1100: 1099: 1096: 1095: 1093: 1092: 1087: 1082: 1077: 1072: 1067: 1066: 1065: 1055: 1050: 1049: 1048: 1037: 1035: 1031: 1030: 1028: 1027: 1022: 1017: 1012: 1007: 1005:Dynamic memory 1002: 997: 992: 986: 980: 974: 973: 971: 970: 965: 960: 955: 950: 945: 940: 934: 932: 928: 927: 925: 924: 919: 914: 909: 904: 899: 894: 888: 885: 884: 877: 876: 869: 862: 854: 847: 846:External links 844: 841: 840: 820: 819: 817: 814: 811: 810: 804: 797: 793: 792: 786: 779: 775: 774: 771: 764: 760: 759: 756: 749: 745: 744: 741: 734: 726: 725: 722: 715: 707: 706: 703: 696: 688: 687: 684: 677: 669: 668: 665: 658: 650: 649: 643: 636: 628: 627: 624: 617: 609: 608: 605: 598: 590: 589: 586: 579: 571: 570: 567: 560: 552: 551: 548: 541: 533: 532: 529: 522: 514: 513: 510: 503: 495: 494: 491: 484: 476: 475: 472: 467: 436: 433: 404: 390: 380: 379:Implementation 377: 310: 288: 285: 268: 267: 265: 264: 257: 250: 242: 239: 238: 237: 236: 227: 218: 209: 197: 196: 192: 191: 190: 189: 184: 179: 174: 169: 164: 159: 154: 149: 144: 139: 131: 130: 129:General topics 126: 125: 114: 113: 28: 26: 19: 9: 6: 4: 3: 2: 1385: 1374: 1371: 1370: 1368: 1361: 1351: 1350: 1341: 1340: 1337: 1331: 1328: 1327: 1325: 1321: 1315: 1312: 1310: 1307: 1305: 1302: 1300: 1297: 1295: 1292: 1290: 1287: 1285: 1282: 1280: 1277: 1276: 1274: 1268: 1262: 1259: 1257: 1254: 1253: 1251: 1245: 1239: 1236: 1234: 1233:Visual Studio 1231: 1229: 1226: 1224: 1223:GNOME Builder 1221: 1219: 1216: 1214: 1211: 1209: 1206: 1204: 1201: 1199: 1196: 1194: 1191: 1190: 1188: 1186: 1182: 1176: 1173: 1171: 1167: 1163: 1162:Visual Studio 1160: 1158: 1155: 1153: 1150: 1148: 1145: 1143: 1140: 1138: 1135: 1133: 1130: 1128: 1125: 1123: 1120: 1118: 1115: 1113: 1110: 1109: 1107: 1105: 1101: 1091: 1088: 1086: 1083: 1081: 1078: 1076: 1073: 1071: 1068: 1064: 1061: 1060: 1059: 1056: 1054: 1051: 1047: 1044: 1043: 1042: 1039: 1038: 1036: 1032: 1026: 1023: 1021: 1018: 1016: 1013: 1011: 1008: 1006: 1003: 1001: 998: 996: 993: 991: 988: 987: 984: 981: 979: 975: 969: 966: 964: 961: 959: 956: 954: 951: 949: 946: 944: 941: 939: 936: 935: 933: 929: 923: 920: 918: 915: 913: 910: 908: 905: 903: 900: 898: 895: 893: 890: 889: 886: 882: 875: 870: 868: 863: 861: 856: 855: 852: 833: 832: 825: 821: 805: 802: 798: 794: 787: 784: 780: 776: 772: 769: 765: 761: 757: 754: 750: 746: 742: 739: 735: 732: 728: 727: 723: 720: 716: 713: 709: 708: 704: 701: 697: 694: 690: 689: 685: 682: 678: 675: 671: 670: 666: 663: 659: 656: 652: 651: 648: 644: 641: 637: 634: 630: 629: 625: 622: 618: 615: 611: 610: 606: 603: 599: 596: 592: 591: 587: 584: 580: 577: 573: 572: 568: 565: 561: 558: 554: 553: 549: 546: 542: 539: 535: 534: 530: 527: 523: 520: 516: 515: 511: 508: 504: 501: 497: 496: 492: 489: 485: 482: 478: 477: 473: 468: 463: 462: 459: 456: 454: 451:header file ( 450: 446: 443:header file ( 442: 432: 430: 426: 422: 418: 414: 402: 400: 388: 384: 376: 374: 308: 306: 302: 298: 294: 284: 282: 278: 274: 263: 258: 256: 251: 249: 244: 243: 241: 240: 233: 228: 224: 219: 215: 210: 206: 201: 200: 199: 198: 194: 193: 188: 185: 183: 180: 178: 175: 173: 170: 168: 165: 163: 160: 158: 155: 153: 150: 148: 145: 143: 140: 138: 135: 134: 133: 132: 128: 127: 123: 120: 119: 110: 107: 99: 88: 85: 81: 78: 74: 71: 67: 64: 60: 57: –  56: 52: 51:Find sources: 45: 41: 35: 34: 29:This article 27: 23: 18: 17: 1360: 1347: 1203:Code::Blocks 1175:Watcom C/C++ 989: 963:Preprocessor 943:Header files 830: 824: 474:Description 457: 452: 448: 444: 440: 438: 428: 424: 421:isdigit(x++) 420: 412: 410: 396: 385: 382: 372: 370: 304: 290: 272: 271: 167:Localization 141: 102: 96:October 2011 93: 83: 76: 69: 62: 50: 38:Please help 33:verification 30: 1294:Objective-C 1075:Windows CRT 417:side effect 363:'z' 345:'a' 333:'Z' 315:'A' 152:Mathematics 1270:Descendant 1142:Norcroft C 968:Data types 917:Embedded C 816:References 471:character 466:character 354:&& 324:&& 137:Data types 66:newspapers 1272:languages 1104:Compilers 1046:libhybris 948:Operators 938:Functions 808:towctrans 768:towctrans 583:iswxdigit 455:in C++). 162:Date/time 1367:Category 1349:Category 1323:Designer 1238:NetBeans 1228:KDevelop 1208:CodeLite 1053:dietlibc 1020:Variadic 995:File I/O 931:Features 790:iswctype 753:iswctype 738:towupper 719:towlower 700:iswpunct 681:iswprint 662:iswblank 640:iswspace 621:iswgraph 602:iswcntrl 576:isxdigit 564:iswdigit 545:iswupper 526:iswlower 507:iswalpha 488:iswalnum 449:wctype.h 279:for the 232:stdarg.h 223:setjmp.h 205:assert.h 1213:Eclipse 1166:Express 922:MISRA C 801:wctrans 731:toupper 712:tolower 693:ispunct 674:isprint 655:isblank 633:isspace 614:isgraph 595:iscntrl 557:isdigit 538:isupper 519:islower 500:isalpha 481:isalnum 453:cwctype 441:ctype.h 429:isdigit 373:ctype.h 287:History 214:errno.h 182:Signals 147:Strings 80:scholar 1193:Anjuta 1090:uClibc 1085:Newlib 1063:EGLIBC 1041:Bionic 1010:String 958:Syntax 953:String 892:ANSI C 783:wctype 445:cctype 415:has a 124:(libc) 82:  75:  68:  61:  53:  1304:Limbo 1218:Geany 1198:CLion 1122:Clang 1070:klibc 1058:glibc 1025:POSIX 835:(PDF) 647:space 399:Linux 360:<= 348:<= 330:<= 318:<= 301:ASCII 87:JSTOR 73:books 1314:Vala 1299:Alef 1185:IDEs 1152:SDCC 1080:musl 1015:Time 1000:Math 990:Char 469:Wide 464:Byte 305:true 293:Unix 234:> 230:< 225:> 221:< 216:> 212:< 207:> 203:< 59:news 1279:C++ 1170:C++ 1157:TCC 1147:PCC 1137:LCC 1132:ICC 1127:GCC 1112:ACK 912:C23 907:C17 902:C11 897:C99 423:or 42:by 1369:: 1309:Go 1284:C# 1168:, 1164:, 796:— 778:— 763:— 748:— 375:. 339:|| 307:: 1289:D 873:e 866:t 859:v 413:x 366:) 357:c 351:c 342:( 336:) 327:c 321:c 312:( 261:e 254:t 247:v 109:) 103:( 98:) 94:( 84:· 77:· 70:· 63:· 36:.

Index


verification
improve this article
adding citations to reliable sources
"C character classification"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
C standard library
Data types
Character classification
Strings
Mathematics
File input/output
Date/time
Localization
Memory allocation
Process control
Signals
Alternative tokens
assert.h
errno.h
setjmp.h
stdarg.h
v
t
e

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.