C 编程语言 :if statements are not working correctly with characters

标签 c if-statement chars

<分区>

我试图让这个程序说得好,但它却说还好 尽管我使变量值与 if 测试值相同

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char history[200];
    history == "NY school";

    if(history == "NY school")
    {
        printf("good");
    }
    else{printf("okay");}
    return 0;
}

最佳答案

你需要使用函数strcmp

  if (strcmp(history ,"NY school") == 0) ....

否则你就是在比较指针

加改

  history == "NY school";

使用strcpy

关于C 编程语言 :if statements are not working correctly with characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28781501/

相关文章:

c++ - 在 Windows 上从服务器端检测 RPC 连接丢失

arrays - 如何从名称列中的每个单元格中提取姓氏并将其分配给名称数组?

java - 更改字符串中的某些字符

c - 编译我的 C 程序时发出警告(字符格式,不同类型的 arg)

c - 在字符串缓冲区中查找特定单词的更优方法 C

java - "for loop"仅循环一次或两次。如果是更好的选择吗?

sql-server - SQL Server : Replace characters different than a char in a string using no temporary table

c - 在数组中存储 volatile 字符

C程序列出文件

if-statement - 在 Haxe 的 if 语句中使用对象的存在