字符串在C程序设计语言布尔 [英] String to Boolean in C programming language

查看:119
本文介绍了字符串在C程序设计语言布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题。如何将字符串变量转换为boleean用C?

I have a simple question. How to convert a string variable into a boleean in C?

示例:

char array[] = "(1==1)";

我如何隐蔽阵列为一个布尔值,这样我可以,如果取值把它放在

How do I covert array into a boolean so that I can put it in ifs?

推荐答案

如果字符串格式是固定的,那么你可以简单地挑出单个字符和减去ASCII值 0 从他们转换为整数。就像例如:

If the string format is fixed, then you can simply pick out individual characters and subtract the ASCII value 0 from them to convert to integer. Like for example:

bool op1 = array[1] - '0';
bool op2 = array[4] - '0';

if(op1 == op2)

或者如果你是迂腐:

or if you are pedantic:

bool op1 = (bool)(array[1] - '0' > 0);
bool op2 = (bool)(array[4] - '0' > 0);

这篇关于字符串在C程序设计语言布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆