从字符串转换

基本类型 / 布尔类型

#include <strings.h>

// C has no parser for "true"/"false": compare the text.
const char *str1 = "true";
bool boolValue1 = strcmp(str1, "true") == 0;
//boolValue1 is true

// strcasecmp (POSIX) ignores letter case
const char *str2 = "TRUE";
bool boolValue2 = strcasecmp(str2, "true") == 0;
//boolValue2 is true

// "0" and "1" go through a number
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");