从字符串转换

基本类型 / 布尔类型

#include <strings.h>

// C 中没有解析 "true"/"false" 的函数:需自行比较文本。
const char *str1 = "true";
bool boolValue1 = strcmp(str1, "true") == 0;
//boolValue1 is true

// strcasecmp(POSIX)忽略字母大小写
const char *str2 = "TRUE";
bool boolValue2 = strcasecmp(str2, "true") == 0;
//boolValue2 is true

// "0" 和 "1" 要经由数字转换
const char *str3 = "0";
bool boolValue3 = strtol(str3, nullptr, 10) != 0;
//boolValue3 is false

printf("boolValue1 is %s\n", boolValue1 ? "true" : "false");
printf("boolValue2 is %s\n", boolValue2 ? "true" : "false");
printf("boolValue3 is %s\n", boolValue3 ? "true" : "false");