为什么" A" !=" A"用C? [英] Why is "a" != "a" in C?

查看:234
本文介绍了为什么" A" !=" A"用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void main() {
    if("a" == "a")
      printf("Yes, equal");  
    else
      printf("No, not equal");
}

为什么输出不,不等于

推荐答案

什么您比较是针对不同的弦,其被存储在不同地点的两个存储器地址。这样做实际上是这样的:

What you are comparing are the two memory addresses for the different strings, which are stored in different locations. Doing so essentially looks like this:

if(0x00403064 == 0x002D316A) // Two memory locations
{
    printf("Yes, equal");
}

使用以下code到两个字符串的值进行比较:

Use the following code to compare two string values:

#include <string.h>

...

if(strcmp("a", "a") == 0)
{
    // Equal
}

此外,A==A的确可能返回true,这取决于你的编译器,它可以在编译的时候合并等于字符串连接为一个以节省空间。

Additionally, "a" == "a" may indeed return true, depending on your compiler, which may combine equal strings at compile time into one to save space.

当你比较两个字符值(不是指针),它是一个数字比较。例如:

When you're comparing two character values (which are not pointers), it is a numeric comparison. For example:

'a' == 'a' // always true

这篇关于为什么&QUOT; A&QUOT; !=&QUOT; A&QUOT;用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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