2016-12-17 53 views
0

我發現了一個奇怪的錯誤,而在C.==操作符不工作用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; 
} 

這裏是錯誤IM從得到終端:

[email protected]:~/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 

[email protected]:~/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) 

很奇怪,因爲你可以看到我的程序這麼想的進入我的第一個「如果」, 但你可以看到它,如果statment打印前行。

感謝您的任何幫助!

+3

你怎麼知道?你沒有沖洗。 –

+0

如果該劑量得到打印,我的內部就有一個printf。 –

+7

爲了確保輸出在終端刷新,請嘗試添加換行符:'printf(「yes \ n」);'。你在調用'ZeroPlayersGame()' –

回答

2

我認爲你的問題與你的問題標題「運算符==在C中不起作用」無關。從輸出中可以看出,printf運行良好,並且在函數「ZeroPlayersGame」和「TwoPlayersGame」中可能存在問題。如果您向我們提供了有關這兩種功能的更多信息,解決問題將更容易。 另外,在邏輯語句中使用「1 == 1」太奇怪了。爲什麼不消除它,並在printf語句後面寫上你的語句。因爲「1 == 1」總是如此。

+0

謝謝你,正如我之前寫的,vereything成真了。 –