2015-11-10 41 views
0

您好我想創建一個簡單的代碼中使用,如果邏輯運算符==在if語句,但它不工作的人可以看看我的代碼在告訴我什麼是錯用它邏輯運算符在if語句不工作

這裏是我code`

#include<stdio.h> 
main(); 
{ 
char a[15], b[15]; 
clrscr(); 
printf("Enter A String\n"); 
fgets(a,15,stdin); 
printf("Enter Another String\n"); 
fgets(b,15,stdin); 
if(a==b) 
    printf("Match\n"); 
else 
    printf("Does Not Match"); 
getch(); 
} 

我使用的這個,但即使我進入2個等值它打印「不匹配」 誰能告訴我什麼是錯,我是初學者在編程和IM真的很困惑什麼是錯這裏

回答

2

你不能使用==來ch使字符串平等。它正在做的是檢查數組a和b是否指向相同的位置。改用strcmp。我假設你的編程語言是C. C++有其他類來處理這個問題。

#include <string.h> 

if (strcmp(a,b)==0) { printf("Match\n") } 
+0

非常感謝你的幫助 –