Reg. strncmp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arunraj2002in
    New Member
    • Jun 2007
    • 22

    Reg. strncmp

    Hi All,

    I have doubt in strncmp.
    consider the following prog:

    int main()
    {

    int i;
    i = strncmp("Nstrin g","Astring",6) ;
    printf("%d", i);
    }

    Here o/p is 13, now if i modify the prog like:

    i = strncmp("Nstris g","Astring",6) ; O/P is 5 why??

    ok ok.. got it !! while typing the question i got the ANSWER.
    it print the difference in the character.. Right?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by arunraj2002in
    Hi All,

    I have doubt in strncmp.
    consider the following prog:

    int main()
    {

    int i;
    i = strncmp("Nstrin g","Astring",6) ;
    printf("%d", i);
    }

    Here o/p is 13, now if i modify the prog like:

    i = strncmp("Nstris g","Astring",6) ; O/P is 5 why??

    ok ok.. got it !! while typing the question i got the ANSWER.
    it print the difference in the character.. Right?
    That would clarify the first comparison: 'N'-'A' == 13 but what about the second
    comparison then? It seems as if strncmp compares starting at the last character:
    's'-'n' == 5 but that isn't a valid way to compare two strings lexicographical ly.

    kind regards,

    Jos

    Comment

    • arunraj2002in
      New Member
      • Jun 2007
      • 22

      #3
      Originally posted by JosAH
      That would clarify the first comparison: 'N'-'A' == 13 but what about the second
      comparison then? It seems as if strncmp compares starting at the last character:
      's'-'n' == 5 but that isn't a valid way to compare two strings lexicographical ly.

      kind regards,

      Jos

      OOps typing mistake.. Sorry man.. its:
      i = strncmp("Nstris g","Nstring",6) ; now for i = strncmp("Nstris g","Astring",6) ;
      Answer is 13 only..

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by arunraj2002in
        OOps typing mistake.. Sorry man.. its:
        i = strncmp("Nstris g","Nstring",6) ; now for i = strncmp("Nstris g","Astring",6) ;
        Answer is 13 only..
        Ah, ok, that explains it all: the diference of the two characters that are not equal
        is the return value of the strncmp function: 'N'-'A' == 13 and 's'-'n' == 5.

        kind regards,

        Jos

        Comment

        Working...