473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Comparing two like strings returns not equal

Why are output:s1 and s2 not equal?

Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3. char *s1="Hello";
  4. char *s2="Hello";
  5. if(s1==s2)
  6.   printf("s1 and s2 are equal");
  7. else
  8.   printf("s1 and s2 are not equal");
  9. }
Sep 30 '11 #1
2 1936
mac11
256 100+
You're comparing pointer values, not their contents. Try printing the hex value of s1 and s2 and you'll see they have different addresses.

To compare the value of the strings you have to use strcmp or something similar (or use C++ std::string)
Sep 30 '11 #2
you should try the strcmp() function to compare strings and by that you can exactly compare strings in string.h header file.
Oct 2 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
6
by: BrianJones | last post by:
I have a problem with the int strcmp(str1,str2) function: When I do: char *pass; char *passv; pass = getpass("Please enter....."); passv = getpass("Please verify.....");
3
by: Robert Dell | last post by:
I have a problem comparing strings in an order form i'm writing. I want to give a running total at the bottom of the page and it appears to be working except it doesn't compare correctly (it...
1
by: Reg Rat | last post by:
Hello all, I'm led to believe that the .Net framework offers intelligent string comparison such that é follows e in an alphabetic sort rather than appearing after z, for example. Does this mean...
4
by: rossum | last post by:
I was comparing strings, as one does: stringA.CompareTo(stringB) == -1 Then I thought that this was not as easy to read as it could be, wouldn't it be clearer to write: stringA < stringB ...
1
by: Jetboy555 | last post by:
Sample input: 2000 Georgia Tech 30 Virginia 20 1999 Virginia 20 Virginia tech My Problem is in taking the input in correctly. I take the year in correctly, but i'm having trouble with the...
6
by: cutlass | last post by:
Need help in thus script. Trying to create the script that uses comparison operators and functions to compare two strings entered by user. When I add in the info in the script section everything...
7
by: arnuld | last post by:
On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote: Either you are not understanding me or I am not able to understand this <p_strcmpthing. This is your code:
5
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
Not really a C#-specific comment, more general .net observations. 1) A while back I found the need to determine whether or not a particular StringComparer was case-insensitive. The best way I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.