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

String Compare in C

Posted on 2011-03-07
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
  • 3
  • 2
  • 2

Accepted Solution

xchange earned 334 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 334 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?
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


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 166 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 213
Assigning default value to structure in C for mutithread application 17 71
Advice in Xamarin 21 97
asp Google Map 2 32
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

828 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