[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C++: strcmp - how does it work?

Posted on 2010-08-29
8
Medium Priority
?
952 Views
Last Modified: 2013-11-17
Why is it that when you put !str.compare() it actually works? Doesn't the "!" actually mean false?

From my understanding, compare() checks every letter in the string and uses a counter to track if there are any differences. If the number returned is 0 then the string matches, anything else means that the string is not the same

In C++, 0 = true, and 1 = false (unless I'm mistaken) so why does the "!" if statement and the "== 0" if statement both work?
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string str = "test";

  // if statement with !str.compare()
  if (!str.compare("test"))
     cout << "String Matched!" << endl;

  // if statement with == 0 str.compare()
  if (str.compare("test") == 0)
     cout << "String Matched!" << endl;

  return 0;
}

Open in new window

0
Comment
Question by:Zenoture
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 33554974
==0 is false, != 0 is true
0
 

Author Comment

by:Zenoture
ID: 33554981
well then that's why then lol. Thank you.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33555044
>> In C++, 0 = true, and 1 = false (unless I'm mistaken)
C++ takes its definition of integer numeric values of true/false from C:
     false is 0
     true is not 0
===========
So, consider following if else statement (either C or C++):
    if( x ) {
        // do if x != 0
    }
    else {
        // do if x == 0
    }
===========

Only if x is 0 is the else body executed; for all other integers, the x != 0 body is executed.
===========
However, some functions return 0 which may or may not mean true. You have to read the api of the function to understand what the return value means.

See string::compare:
    http://www.cplusplus.com/reference/string/string/compare/

The return value of compare is "0 if the compared characters sequences are equal". This is not saying that a 0 means true, just that the sequences are equal.

  if (!str.compare("test"))
     cout << "String Matched!" << endl;
is equivalent to (my preferred way):
  if ( 0 == str.compare("test") ) // is str == "test"
     cout << "String Matched!" << endl;

===========
compare returns an int, so the first paragraph applies here. Since a 0 return value means that str == "test", and since 0 in a if test condition means false, then the ! (NOT) unary operator on 0 means NOT false, which is true.

===========
See section "Logical operators" in
       http://www.cplusplus.com/doc/tutorial/operators/

"The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:"
!(5 == 5) // is false since the expression at its right (5==5) is true
!(6 <= 4) // evaluates to true because (6 <= 4) would be false
!true    // evaluates to false
!false   // evaluates to true

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Zenoture
ID: 33555058
Oh... so i was wrong... but now the whole 0/1 true/false thing makes sense haha. Thanks a lot phoffric, you never cease to amaze me =)
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33555063
Isn't it great when things make sense!
0
 

Author Comment

by:Zenoture
ID: 33555075
Of course! Thought why other languages tend to flip the meaning, I'll never understand...

It can't be THAT hard to stick to one standard can it? lol
0
 

Author Comment

by:Zenoture
ID: 33555078
though*
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33555995
C++ was created in order to guarantee job security, so the standard had to be different. Here is the confession from the creator of C++.
    http://www.odgaard.org/jeod/funny/interview.html
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question