iT邦幫忙

0

詢問 : C語言中 memcmp() 的回傳值

  • 分享至 

  • xImage
int memcmp(const void *str1, const void *str2, size_t n)

if Return value < 0 then it indicates str1 is less than str2.
if Return value > 0 then it indicates str2 is less than str1.
if Return value = 0 then it indicates str1 is equal to str2.

上網只查到它的回傳值是大於、等於或小於0的整數,但是都沒有一個實際的值,想請問一下這個function真的沒有一個固定的retrun value嗎?

alien663 iT邦研究生 5 級 ‧ 2022-12-16 08:47:03 檢舉
[gcc-mirror/gcc](https://github.com/gcc-mirror/gcc/blob/master/libgcc/memcpy.c)
樓上應該是要貼 https://github.com/gcc-mirror/gcc/blob/master/libgcc/memcmp.c 吧
該不會是同堂課的同學吧
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
尼克
iT邦大師 1 級 ‧ 2022-12-16 11:29:46
// g++ cpp-memcmp.cpp -o a.out
#include <stdio.h>
#include <string.h>

int main() {
    char buffer1[] = "abcde";
    char buffer2[] = "abcde";
    
    int ret = memcmp(buffer1, buffer2, sizeof(buffer1));
    if (ret > 0) {
        printf("buffer1 is greater than buffer2\n");
        printf("%d", ret);
    } else if (ret < 0) {
        printf("buffer1 is less than buffer2\n");
        printf("%d", ret);
    } else { // ret == 0
        printf("buffer1 is equal to buffer2\n");
        printf("%d", ret);
    }

    return 0;
}

自己列印出來不就知道了!

看更多先前的回應...收起先前的回應...

報告:我試了幾次
大於0的是32;小於0的是-32
是說
要不要這麼在乎這回傳值是多少
/images/emoticon/emoticon25.gif

尼克 iT邦大師 1 級 ‧ 2022-12-16 18:39:58 檢舉

我也不知道,他的問題在在哪裡?

簡單來說我需要自己寫出一個一模一樣的function

尼克 iT邦大師 1 級 ‧ 2022-12-18 10:36:47 檢舉

所以?

自己印出來我也知道,但我更想知道他到底是回傳怎樣的"數值",而不只是正負0這樣簡略的答案

自己印出來我也知道,但我更想知道他到底是回傳怎樣的"數值",而不只是正負0這樣簡略的答案

我要發表回答

立即登入回答