difference between strcmp and "=="

This is a discussion on difference between strcmp and "==" within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm very new to C++. is strcmp considered a C++ function ? what are the differences between using ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    difference between strcmp and "=="

    Hi all,
    I'm very new to C++. is strcmp considered a C++ function ?
    what are the differences between using these 2 comparison ?
    if (strcmp (var,"string") == 0)
    if (var=="string") ?


    Thanks in advance

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    strcmp compares C style strings, that is, char * or char[].

    == compared C++ style strings, that is, std::string.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Quote Originally Posted by Cat
    strcmp compares C style strings, that is, char * or char[].

    == compared C++ style strings, that is, std::string.
    Thanks thats a quick one. I'm trying to program in C++ , i have some knowledge in C. Would it be appropriate to mix codes from 2 different languges ?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by yuliang11
    Would it be appropriate to mix codes from 2 different languges ?
    When you are firmly grounded in both -- otherwise it just confuses you and screws you up in either.
    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.*

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Quote Originally Posted by Dave_Sinkula
    When you are firmly grounded in both -- otherwise it just confuses you and screws you up in either.
    Thanks. in that case i'll just C all the way.thanks all for your help

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    Quote Originally Posted by yuliang11
    Thanks thats a quick one. I'm trying to program in C++ , i have some knowledge in C. Would it be appropriate to mix codes from 2 different languges ?
    It is possible to do so, but unadvised. Using C++ strings is generally easier, so you should learn and use them.

    But sometimes you are forces to work with the lower level c strings. For example command line arguements are always c strings. (of course you can still convert them to C++ string easily)
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by yuliang11
    Thanks. in that case i'll just C all the way.thanks all for your help
    If you're learning C++ then you're going to need to try to forget what you know about C and learn a new way of thinking.

    Don't progam in "C++--". That is, don't write code that's really C code with a few C++ things added in.

    95% of all cases in which you use a string, you can use std::string safely (and in the last 5% you should prefer std::vector<char> over char[] anyhow).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

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