C中的字符比较 [英] Char Comparison in C

查看:131
本文介绍了C中的字符比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较两个字符,以查看一个是否大于另一个。要查看它们是否相等,我使用了 strcmp 。我可以使用类似于 strcmp 的东西吗?

I'm trying to compare two chars to see if one is greater than the other. To see if they were equal, I used strcmp. Is there anything similar to strcmp that I can use?

推荐答案

A char 变量实际上是一个8位整数值。它将具有从 0 255 的值。这些是 ASCII代码 0 表示C空字符, 255 表示空符号。

A char variable is actually an 8-bit integral value. It will have values from 0 to 255. These are ASCII codes. 0 stands for the C-null character, and 255 stands for an empty symbol.

因此,当您编写以下赋值时:

So, when you write the following assignment:

char a = 'a'; 

与以下内容相同:

char a = 97;

因此,您可以比较两个 char 变量使用> < == < = > = 运算符:

So, you can compare two char variables using the >, <, ==, <=, >= operators:

char a = 'a';
char b = 'b';

if( a < b ) printf("%c is smaller than %c", a, b);
if( a > b ) printf("%c is smaller than %c", a, b);
if( a == b ) printf("%c is equal to %c", a, b);

这篇关于C中的字符比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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