- 1、ctype.h
- 2、stdlib.h
- 2.1、atoX系列函数
- 2.2、strtoX系列函数
ctype.h头文件中定义了一系列对字符的数据类型的判断
更多函数请参考:http://c.biancheng.net/ref/isalnum.html
#include#include #define PRINTF(format, ...) printf("("__FILE__":%d) %s: "format,__LINE__,__FUNCTION__, ##__VA_ARGS__) #define PRINT_INT(value) PRINTF(#value":%d n", value) #define PRINT_CHAR(value) PRINTF(#value":%c n", value) int main(){ PRINT_INT(isalnum('A'));//判断字符是否为字母或数字a-zA-Z,0-9均返回非0 PRINT_INT(isalpha('A'));//判断是否是字母a-zA-Z均为字母,返回非0 PRINT_INT(isblank(' '));//判断是否为空字符 PRINT_INT(iscntrl('t'));//判断是否为控制字符 PRINT_INT(isdigit('a'));//判断是否为一个十进制数 PRINT_CHAR(tolower('A'));//字符转小写 PRINT_CHAR(toupper('a'));//字符转大写 return 0; }
#ifndef stdio_h #include2、stdlib.h#endif #ifndef ctype_h #include #endif #define PRINTLN(format) printf("%s %d:"format"n",__FUNCTION__,__LINE__) #define PRINTFLN(format, ...) printf(#format":"format"n", ##__VA_ARGS__) int main(){ PRINTLN("检测参数字符是否为数字或字母"); printf("%dn", isalnum('a'));//2 printf("%dn", isalnum('B'));//1 printf("%dn", isalnum('2'));//4 printf("%dn", isalnum('-'));//0 PRINTLN("检测参数字符是否为字母"); printf("%dn", isalpha('2'));//0 printf("%dn", isalpha('a'));//2 printf("%dn", isalpha('A'));//1 PRINTLN("检测参数字符是否为十进制数字"); printf("%dn",isdigit('a'));//0 printf("%dn",isdigit('1'));//4 PRINTLN("检测参数字符是否为十六进制数字"); printf("%dn",isxdigit('1'));//128 printf("%dn",isxdigit('A'));//128 printf("%dn",isxdigit('K'));//0 PRINTLN("检测所传字符是否为小写字母"); printf("%dn",islower('a'));//2 printf("%dn",islower('B'));//0 printf("%dn",islower('2'));//0 PRINTLN("检测所传字符是否为大写字母"); printf("%dn",isupper('A'));//1 printf("%dn",isupper('a'));//0 printf("%dn",isupper('2'));//0 PRINTLN("检测参数是否为标点符号"); printf("%dn",ispunct('c'));//0 printf("%dn",ispunct('2'));//0 printf("%dn",ispunct('A'));//0 printf("%dn",ispunct('.'));//16 PRINTLN("检测参数是否为空白字符"); printf("%dn",isspace(' '));//8 printf("%dn",isspace(' '));//8 printf("%dn",isspace('a'));//0 return 0; }
stdlib.h头文件中定义了一些变量和宏以及函数。
详细可参考:这里
其中atoX(const char *str)系列函数拥有将字符串转换类型的功能。
#ifndef stdio_h #include#endif #ifndef stdlib_h #include #endif #define PRINTF(format, ...) printf(format, ##__VA_ARGS__) #define PRINTLN(value) PRINTF(value"n") #define PRINTF_F(value) PRINTF(#value": %fn", value) #define PRINTF_I(value) PRINTF(#value": %in", value) int main(){ PRINTLN("把参数字符串转为一个浮点数(类型为double)"); PRINTF_F(atof("12.34"));//结果为12.34 PRINTF_F(atof("-12e34"));//结果为:-12e34(支持科学计数法) PRINTF_F(atof(" 1.234abcd"));//结果为:1.234 PRINTF_F(atof("0x10"));//结果为0(不同编辑器结果不同,部分编辑器支持16进制) PRINTF_F(atof("0x10p3"));//结果为0(不同编辑器结果不同,部分编辑器支持16进制) PRINTLN("nn把参数字符串转为一个整数"); PRINTF_I(atoi("1234"));//结果为:1234 PRINTF_I(atoi("-1234"));//结果为:-1234 PRINTF_I(atoi(" 1234abc"));//结果为1234 PRINTF_I(atoi("0x10"));//结果为0(不支持十六进制数) PRINTF_I(atoi("a"));//结果为0 PRINTLN("把参数转为long int"); PRINTF_I(atol("3")); return 0; }
上方代码调用结果如下
strtoX(const char *str, char **endptr)相关的函数功能和atoX系列函数功能差不多,但是可重复解析,更安全,更强大



