gpt4 book ai didi

c - 如何修复 if 语句错误和 "Result of comparison against a string literal is unspecified"?

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:31 25 4
gpt4 key购买 nike

所以,我才刚刚起步(请原谅我的新手),我正在使用 Xcode 在 Mac 上开发 C 程序。它基本上只是一个 scanf() ,然后是一大堆产生预定输出的 if 语句。我已经编写了它,以便 Xcode 可以毫不费力地编译它,但是当我尝试运行它时,我遇到了一个奇怪的“Fix-it”错误并且没有输出。

char planet[9];

printf("Input the name of a planet\n");

scanf("%c", &planet);

if (planet == "Earth")
{printf("Earth is 150 million kilometers away from the sun");}

if (planet == "Mars")
{printf("Mars is 220 million kilometers away from the sun");}

if (planet == ("Mercury"))
{printf("Mercury is 57 million kilometers from the sun");}

if (planet == ("Venus"))
{printf("Venus is 108 million kilometers from the sun");}

if (planet == ("Jupiter"))
{printf("Jupiter is 779 million kilometers from the sun");}

if (planet == ("Saturn"))
{printf("Saturn is 1.73 billion kilometers from the sun");}

if (planet == ("Uranus"))
{printf ("Uranus (haha) is 2.88 billion kilometers from the sun");}

if (planet == ("Neptune"))
{printf("Neptune is 4.5 billion kilometers from the sun");}

return 0;

是代码本身,但我无法让它工作。

这里还有 Xcode 项目的链接。

https://www.facebook.com/photo.php?fbid=252616984794310&set=a.106237419432268.11635.100001380326478&type=1&theater

最佳答案

planet 的地址永远不会等于任何这些字符串文字的地址。您需要使用 strcmp 来比较字符串的内容,而不是比较它们的地址。

关于c - 如何修复 if 语句错误和 "Result of comparison against a string literal is unspecified"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8276568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com