strcmp???

This is a discussion on strcmp??? within the C Programming forums, part of the General Programming Boards category; i have a question guys.. strcmp is short for string compare right? therefore it compare 2 string.. but can strcmp ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    strcmp???

    i have a question guys.. strcmp is short for string compare right? therefore it compare 2 string.. but can strcmp can be also used for character?
    like if you are to compare to variable, say A and B, which are declared as char.. would strcmp work?
    ex. if (strcmp( A, B) == 0)
    Last edited by SvNo7; 12-29-2006 at 05:11 AM.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    no it would not

    simply because the arguments to strcmp needs to be char *

    if you want to compare variables that are characters simply use the relational operators ==, !=, >, <, >= or <= as you would with integers
    Last edited by peterchen; 12-29-2006 at 05:23 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Quote Originally Posted by peterchen
    simply because the arguments to strcmp needs to be char *
    Almost that simply, but not quite. strcmp() assumes that both the (const) char * arguments it receives are the address of an array of char that are terminated, eventually, with a zero character.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by SvNo7
    i have a question guys.. strcmp is short for string compare right? therefore it compare 2 string.. but can strcmp can be also used for character?
    like if you are to compare to variable, say A and B, which are declared as char.. would strcmp work?
    ex. if (strcmp( A, B) == 0)
    well if u still wanted to use strcmp to compare two single char. It should be char *

    What u could do is
    Code:
    char str1[2] = {"a"};
    char str2[2] = {"a"};
     
    if(strcmp(str1,str2_==0)
    {}
    This is sort of tech which most of the newbie use. But which is not recommended. This is just a way to show you to use it. But still if you wanted to compare two char this would the recommended

    Code:
    char a ='1';
    char b = '1';
     
    if(a== b)
    {}
    your could actually see the difference between those two. In the first method u will have to allocated some same for the null char which basically means u are using one more byte for just one byte. But when u declare it as a char u are using just one byte. NOTE char data type takes one byte most of the time.

    ssharish2005
    Last edited by ssharish2005; 12-29-2006 at 07:37 AM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    hmm.. i know that if you are to compare to char it should be in == form.. but how come we didn't have an error when we compiled our program using the strcmp in comparing to variable

    ex.
    Input is b
    Code:
    main()
    {
        char x1;
        char a='b';
    
        printf("Input letter: ");
        scanf("%c",&x1);
    
        if ( strcmp ( x1, a ) == 0)
            printf("same letter!!");
    
    return 0;
    }
    output:

    same letter!!

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A string has (generally) more than one character. Unless it is a null string, it is not a single character. So comparing single characters using strcmp is not correct.

    SvNo7: your example displays nothing relevant, and is potentially disinformation.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by SvNo7
    hmm.. i know that if you are to compare to char it should be in == form.. but how come we didn't have an error when we compiled our program using the strcmp in comparing to variable

    ex.
    Input is b
    Code:
    main()
    {
        char x1;
        char a='b';
     
        printf("Input letter: ");
        scanf("%c",&x1);
     
        if ( strcmp ( x1, a ) == 0)
            printf("same letter!!");
     
    return 0;
    }
    output:

    same letter!!
    You should have turned on your warning while compiling this code. The code produces 2 - 3 warning messages. What compiler are u using first of all. For your information her are the warning information

    Code:
    teststrcmp.c: In function ‚main‚:
    teststrcmp.c:12: warning: passing argument 1 of ‚strcmp‚ makes pointer from integer without a cast
    teststrcmp.c:12: warning: passing argument 2 of ‚strcmp‚ makes pointer from integer without a cast
    And explicitly specifying the int beside the main is a should practice

    ssharish2005

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you turn warnings up higher you'll see that strcmp() is undefined because you didn't include <stdlib.h>, and likewise for printf() because <stdio.h> is missing. (Just postulating here.)

    If you just want to compare the first character of a string with another character, simply use ==:
    Code:
    if(astring[0] == achar) {}
    If you want to search for a single character inside a string, you can use strchr().

    In case you're interested, there are many functions that deal with strings and characters. Here's a list. http://cppreference.com/stdstring/index.html

    [edit] Compiling the above code with dinkumware.com/exam:

    MINGW/C++:
    Code:
    sourceFile.cpp: In 
       function `int main()':
    sourceFile.cpp:6: `printf' 
       undeclared (first use this function)
    sourceFile.cpp:6: (Each 
       undeclared identifier is reported only once for each function it appears 
       in.)
    sourceFile.cpp:7: `scanf' 
       undeclared (first use this function)
    sourceFile.cpp:9: `strcmp' 
       undeclared (first use this function)
    sourceFile.cpp:13:2: warning: no newline at end of file
    VC++ v8/C++:
    Code:
    sourceFile.cpp
    sourceFile.cpp(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    sourceFile.cpp(6) : error C3861: 'printf': identifier not found
    sourceFile.cpp(7) : error C3861: 'scanf': identifier not found
    sourceFile.cpp(9) : error C3861: 'strcmp': identifier not found
    sourceFile.cpp(10) : error C3861: 'printf': identifier not found
    [/edit]
    Last edited by dwks; 12-30-2006 at 03:36 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21