top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Behavior of string comparison in C?

+3 votes

Is the following comparison guaranteed to be true?

"hello world"=="hello world";

Also, is the following always guaranteed to be false?

char a[] = "hello world";
a == "hello world";
posted Nov 23, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
"hello world" == "hello world";

will be always true,

becaue "hello world" creates a read-only array of 12 chars containing the values 'h', 'e', 'l', 'l', 'o', ' ','w','o','r','l','d','\0' which has no name and has static storage duration (meaning that it lives for the entire life of the program);

char a[] = "hello world";
a == "hello world";

will always be flse, because:

It creates one object - a char array of size 12, called a, initialised with the values 'h', 'e', 'l', 'l', 'o', ' ','w','o','r','l','d','\0'. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears.

From the above, you can understand that,

In first case:
"hello world" got created as a read only array .. and it should having a memory location where it got stored
asume the memory address is 0xabcd0123

In second case:
char a[] = "hello world" is nothing but "hello world" string is got copied to a[], and a[] will have its own memory address which definitely not the same the 0xabcd0123 (address of "hello world")
both share the different address.

So hence while compairing it  compares the memroy address.

Here I have a sample progam which will make you understand more have a look:

#include <stdio.h>

int main()
    char a[] = "hello world";

    char *p = "hello world";

    if ("hello world" == "hello world")
        printf("True 1\n");
    if (a  == "hello world")
        printf("True 2\n");

    printf("%p , %p, %p\n", "hello world", a, p); //here address of "hello world" and p will be same but address of a will be different.

   return 0;
answer Nov 24, 2014 by Arshad Khan
Similar Questions
0 votes
0 votes

float i = 1.1;
float j = 1.1;

Many a time if (i == j) results in false. Why??

+2 votes

The input strings will contain only numeric digits
The input strings can be of any large lengths
The lengths of the two input string need not be the same
The input strings will represent only positive numbers

If input strings are “1234” and “56”, the output string should be “1290”
If input strings are “56” and “1234”, the output string should be “1290”
If input strings are “123456732128989543219” and “987612673489652”, the output string should be “123457719741663032871”

+1 vote

Consider an implementation of Strings consisting of an array where the first element of the array indicates the length of the String and the next elements represent the value of the ASCII characters in the String.
Implement String concatenation of such scenario using C?

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.