【问题标题】:Operator == doesn't work in C运算符 == 在 C 中不起作用
【发布时间】:2017-05-03 01:12:28
【问题描述】:

我在用 C 编写程序时遇到了一个奇怪的错误。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
int main (int argc, char* argv[])
{
 int boardSize = atoi(argv[2]);

 int generations = atoi(argv[4]);

int gamesort = atoi(argv[1]);
printf("2 is %d 1 is %d 4 is %d name of file is %s \n",boardSize,gamesort,generations,argv[3]);

    if (1==1)
    {
    printf("yes");
        ZeroPlayersGame(boardSize, generations,argv[3]);
    }
    else//(gamesort==2)
    {
        TwoPlayersGame(boardSize, generations,argv[3]);
    }
    return 0;
}

这是我从终端得到的错误:

ise@ubuntu:~/Desktop$ make

gcc -c main.c defs.c gameIO.c zeroPlayer.c twoPlayer.c

gcc gameIO.o defs.o zeroPlayer.o main.o twoPlayer.o -o prog

ise@ubuntu:~/Desktop$ ./prog 1 2 "l.txt" 3

2 is 2 1 is 1 4 is 3 name of file is l.txt 

Segmentation fault (core dumped)

很奇怪,你可以看到我的程序没有输入我的第一个“if”, 但你可以看到它打印了 if 语句之前的行。

感谢您的帮助!

【问题讨论】:

  • 你怎么知道的?你没有冲水。
  • 如果打印出来的话,我的里面有一个 printf。
  • 为了确保输出被刷新到终端,尝试添加一个换行符:printf("yes\n");。您在致电ZeroPlayersGame() 时遇到了崩溃
  • 如果您删除函数调用(ZeroPlayersGameTwoPlayersGame)行?你还在遇到分段错误吗?
  • @YardenRotem 太棒了!祝你好运;-)

标签: c if-statement gcc operators


【解决方案1】:

我认为您的问题与您的问题标题“运算符 == 在 C 中不起作用”无关。从您的输出中可以看出,printf 运行良好,并且函数“ZeroPlayersGame”和“TwoPlayersGame”可能存在问题。如果您向我们提供有关这两个功能的更多信息,则解决问题会更容易。 此外,在逻辑语句中使用“1==1”也很奇怪。为什么不消除它并在 printf 语句之后写下你的语句。因为“1==1”始终为真。

【讨论】:

  • 谢谢你,正如我之前写的,一切都解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 2021-05-22
  • 1970-01-01
  • 2018-02-26
相关资源
最近更新 更多