String Compare in C

Posted on 2011-03-07
Medium Priority
Last Modified: 2012-05-11
I'm doing a quick string compare in C and I keep getting the incorrect results.

I'm guessing that the way that a string is stored differently than it's actual displayed value, or there's otherwise some cranny behind the scenes of the coding that is causing this to occur.

Anyway, if I run code like the following, I get the display of


Which doesn't make any sense, any ideas?
string Letter = "02";

		cout << Letter << "\n";
		if (Letter.compare("01") == 1) {
			cout << "Hello" << "\n";

Open in new window

Question by:PGRBryant
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
  • 3
  • 2
  • 2

Accepted Solution

xchange earned 1336 total points
ID: 35062331
You assume that upon the strings being equal the returned value is 1 which is in fact not.
Compare returns zero upon string equality.

Assisted Solution

xchange earned 1336 total points
ID: 35062378
"02" (the object) is "GREATER" than "01" the parameter and according to the function's spec:
+1 : a positive is returned.

Let me know if you need any additional clarification.

Author Comment

ID: 35062468

My actual problem was a little more complicated, I had a series of If statements that followed that question which asked

Is it "01" is it "02" is it "03"...is it "15"... else... with each one redefining Letter.

And I kept getting an "else" result, but now I realized that it was because the redefinition was just causing them all to display false.. thanks.

Is 0 always true and 1 always false, can you redefine that, how does that work in C?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 35062596
>Is 0 always true and 1 always false, can you redefine that, how does that work in C?
If you mean to change the function's behaviour you can do that through overloading I suppose - the details evade my memory - but do you REALLY want to do this?
(Just because you can does not mean you should - right?)
LVL 32

Assisted Solution

phoffric earned 664 total points
ID: 35063455
Your zone and question indicate the C language, but your code post indicates the C++ language. Why are you interested in C suggestions when writing in C++?

Since you are writing in C and using the string class (which is stored differently than your c-style string, as you already guessed), you are naturally using the string compare function.

To see how to use the compare , here is code from

Notice that there are a number of different flavors of compare giving you quite a bit of flexibility.
// comparing apples with apples
#include <iostream>
#include <string>
using namespace std;

int main ()
  string str1 ("green apple");
  string str2 ("red apple");

  if (str1.compare(str2) != 0)
    cout << str1 << " is not " << str2 << "\n";

  if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    cout << "therefore, both are apples\n";

  return 0;

Open in new window


Author Closing Comment

ID: 35066473
I'm trying to learn C but the best compiler option that I could find was C++ 2010 Express, so I figured I'd just get a little acquainted with both along the way.

And xchange and you both answered my question, so points awarded and much thanks.
LVL 32

Expert Comment

ID: 35066734
Well good luck in your studies! I wish you the best. I think VS C++ 2010 Express is one of the best IDE's around for learning purposes.

It would be helpful for us and for searching the PAQ (Previously Answered Questions) by separating your C and C++ question into two separate questions, one in the C zone, and one in the C++ zone. For the C zone, be sure to post only C code so as not to confuse anyone. For the C++ zone, if you indicate that you are not looking for C-like answers, then you will get responses that hopefully will address the best C++ solutions. (I have seen pure C solutions being given in C++ zones; and I think I may have been guilty of that myself.)

By asking a C zone question and posting C++ code is very confusing to me. It also is like asking two different questions in one question. Please take a look at this:

Again, I wish you the best in your studies.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

801 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