[問題] strcmp

看板C_and_CPP作者 (平凡)時間8年前 (2015/08/11 22:40), 8年前編輯推噓1(105)
留言6則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) DEV C 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 請問一下, 老師說strcmp是從第一個字符開始比, 完全相同輸出0, 左>右,輸出 +的差值 右<左,輸出-的差值 為什麼我編譯後出現+1和-1, 不是+2,-2? 我有試過其它字符,它好像只會出現 0/1/-1 三種 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { int delta1=strcmp("ABC","ABC"); printf("%d\n",delta1); int delta2=strcmp("ABF","ABC"); printf("%d\n",delta2); int delta3=strcmp("ABC","ABF"); printf("%d\n",delta3); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.25.181.59 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1439304012.A.5F5.html ※ 編輯: GooLoo (114.25.181.59), 08/11/2015 22:40:43

08/11 22:42, , 1F
只區分 <0, 0, >0. 正多少或負多少倒是沒規定
08/11 22:42, 1F

08/11 22:49, , 2F
原來如此,可能老師的編譯器不同?
08/11 22:49, 2F

08/11 22:50, , 3F
ex:xcode?
08/11 22:50, 3F

08/12 01:04, , 4F
是的, 編譯器實作不同可能會回傳不同值, 但一定是 < = > 0
08/12 01:04, 4F

08/12 01:13, , 5F
字元 不是字符
08/12 01:13, 5F

08/12 12:53, , 6F
字符是對岸用語
08/12 12:53, 6F
文章代碼(AID): #1LoWbCNr (C_and_CPP)