Странное поведение ф-ции strcmp()

Тема в разделе "WASM.HEAP", создана пользователем osrootd, 23 сен 2008.

  1. osrootd

    osrootd New Member

    Публикаций:
    0
    Регистрация:
    30 июл 2008
    Сообщения:
    1.086
    Код (Text):
    1. #test.c
    2. int main (int argc, char *argv[]){
    3.  
    4. printf ("%d\n",strcmp (argv[1], "quit"));
    5.  
    6. return 0;
    7.  
    8. }
    Результат:
    Почему где встречается 's' возвращается 1?

    Это фигня, я файл приложил echoserver.c, так там quit и s обе возвращают 0
    Почему?
     
  2. SadKo

    SadKo Владимир Садовников

    Публикаций:
    8
    Регистрация:
    4 июн 2007
    Сообщения:
    1.610
    Адрес:
    г. Санкт-Петербург
    Ну вообще сказал как отрезал...

    'h' < 'q' < 's' - чего тут странного?

    Строки сравниваются в литеральном порядке.
    При этом, strcmp не обязана возвращать "нормализованные" значения - "-1", "0", "1".

    курим ман:
    Код (Text):
    1. SYNOPSIS
    2.        #include <string.h>
    3.  
    4.        int strcmp(const char *s1, const char *s2);
    5.  
    6.        int strncmp(const char *s1, const char *s2, size_t n);
    7.  
    8. DESCRIPTION
    9.        The strcmp() function compares the two strings s1 and s2.  It returns an integer less than, equal to, or greater than zero if s1
    10.        is found, respectively, to be less than, to match, or be greater than s2.
    11.  
    12.        The strncmp() function is similar, except it only compares the first (at most) n characters of s1 and s2.
    13.  
    14. RETURN VALUE
    15.        The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or  the  first  n  bytes
    16.        thereof) is found, respectively, to be less than, to match, or be greater than s2.