为什么" A" !=" A"用C?

编程入门 行业动态 更新时间:2024-10-24 13:21:28
本文介绍了为什么" 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?

本文发布于:2023-05-25 10:54:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QUOT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!