Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8143053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:59:32+00:00 2026-06-06T12:59:32+00:00

When comparing a string literal with another string literal with the == operator (or

  • 0

When comparing a string literal with another string literal with the == operator (or !=), is the result well defined?

For example, are the following guaranteed to hold?

assert("a" == "a");
assert("a" != "b");

Please don’t say stuff like “use std::string” instead. I just want to know this specific case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:59:35+00:00Added an answer on June 6, 2026 at 12:59 pm
    "a" == "a"
    

    This expression may yield true or false; there are no guarantees. The two "a" string literals may occupy the same storage or they may exist at two different locations in memory.

    I think that the closest language in the C++ Standard is: “Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation defined” (C++11 §2.14.5/12). There are no other requirements or restrictions, so the result is left unspecified.

    "a" != "b"
    

    This expression must yield false because there is no way that these two string literals can occupy the same location in memory: "a"[0] != "b"[0].


    When you compare string literals in this way, you are really comparing the pointers to the initial elements in the arrays.

    Because we are comparing pointers, the relational comparisons (<, >, <=, and >=) are even more problematic than the equality comparisons (== and !=) because only a restricted set of pointer comparisons may be performed using the relational comparisons. Two pointers may only be relationally compared if they are both pointers into the same array or pointers into the same object.

    If the two "a" string literals occupy the same location in memory, then "a" < "a" would be well-defined and would yield false, because both pointers point to the initial element ('a') of the same array.

    However, if the two "a" string literals occupy different locations in memory, the result of "a" < "a" is undefined, because the two pointers being compared point into entirely unrelated objects.

    Because "a" and "b" can never occupy the same location in memory, "a" < "b" always has undefined behavior. The same is true for the other relational comparison operators.

    If you did, for some reason, want to relationally compare two string literals and have well-defined results, you can use the std::less comparer, which provides a strict-weak ordering over all pointers. There are also std::greater, std::greater_equal, and std::less_equal comparers. Given that string literals with the same contents may not compare equal, I don’t know why one would ever want to do this, but you can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following is code for comparing a String A and String B with atmost
yesterday(April 5th 2012) i'am trying comparing string which is in environment: computer 1 Java(TM)
string a=I am comparing 2 string; string b=I am comparing 2 string; if(a==b) return
Dear all expert i had a problem for comparing my string from document.write with
Environment: VS2005 C++ using STLPort 5.1.4. Compiling the following code snippet: std::string copied =
I would like to know, when comparing string in C#? which method is suitable
I am getting a difference when comparing two string which are 0 and '0'
i am facing a weird problem i am simply comparing two string values and
I am comparing two string by strcmp(1,2) and i am getting -1 insted of
How can i slice a string comparing with my list. string = how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.