[問題] strcmp

作者: GooLoo (平凡)   2015-08-11 22:40:09
開發平台(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):
作者: bibo9901 (function(){})()   2015-08-11 22:42:00
只區分 <0, 0, >0. 正多少或負多少倒是沒規定
作者: GooLoo (平凡)   2015-08-11 22:49:00
原來如此,可能老師的編譯器不同?ex:xcode?
作者: LPH66 (-6.2598534e+18f)   2015-08-12 01:04:00
是的, 編譯器實作不同可能會回傳不同值, 但一定是 < = > 0
作者: dritchie (卍~邁斯納效應~卍)   2015-08-12 01:13:00
字元 不是字符
作者: ctrlbreak   2015-08-12 12:53:00
字符是對岸用語

Links booklink

Contact Us: admin [ a t ] ucptt.com