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 7036123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:23:11+00:00 2026-05-28T01:23:11+00:00

After a long break,I am back to C but getting confused even on some

  • 0

After a long break,I am back to C but getting confused even on some simple issues.
So one is here.

Here is the simple code :

 #include<stdio.h>

 int main() {

    char str1[]="hello";
    char str2[]="hello";

    if(str1==str2)
            printf("equal");  
    else
            printf("unequal");
} 

Output:
unequal

but when I tried this one ,it worked

  char *str1="hello";
  char *str2="hello";

Output
equal

Please if anyone can provide a detailed explanation for it.
Can someone tell me what exactly does C99 standard say about the situation ???

  • 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-05-28T01:23:11+00:00Added an answer on May 28, 2026 at 1:23 am

    When you do == with pointers (which is what str1 and str2 are in both cases1) all you are doing is comparing the two addresses to see if they are the same. When you do

    char str1[]="hello";
    char str2[]="hello";
    

    You are creating two arrays on the stack that hold "hello". They are certainly at different memory locations, so str1 == str2 is false. This is like

    char str1[6];
    str1[0] = 'h';
    str1[1] = 'e';
    str1[2] = 'l';
    str1[3] = 'l';
    str1[4] = 'o';
    str1[5] = '\0'; 
    
    // and the same thing for str2
    

    When you do

    char *str1="hello";
    char *str2="hello";
    

    You are creating two pointers to the global data "hello". The compiler, seeing that these string literals are the same and cannot be modified, will make the pointers point to the same address in memory, and str1 == str2 is true.

    To compare the content of two char*s, use strcmp:

    // strcmp returns 0 if the two strings are equal
    if (strcmp(str1, str2) == 0)
        printf("Equal");
    else
        printf("Not equal");
    

    This is roughly the equivalent of

    char *a, *b;
    
    // go through both strings, stopping when we reach a NULL in either string or
    // if the corresponding characters in the strings don't match up
    for (a = str1, b = str2; *a != '\0' && *b != '\0'; ++a, ++b)
        if (*a != *b)
            break;
    
    // print Equal if both *a and *b are the NULL terminator in
    // both strings (i.e. we advanced a and b to the end of both
    // strings with the loop)
    if (*a == '\0' && *b == '\0')
         printf("Equal");
    else
         printf("Not equal");
    

    1 In the char* version, this is true. In the char[] version, str1 and str2 are really arrays, not pointers, however when used in str1 == str2, they decay to pointers to the first elements of the arrays, so they are the equivalent of pointers in that scenario.

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

Sidebar

Related Questions

So I wrote some C++ today after a long break for a simple coordinate
After a long search I'm still confused about it although I found some related
I'm getting back in to Cocoa development on the Mac after a long stint
I'm spending some time with Java again after a long break on the .NET
After doing a long search on stackoverflow i didn't find any one talked about
Found the answer!! Found the answer after a long break from looking at it!
I finally finished coding my Android calculator after so long. But the calculator won't
I have a long string, want to break that string in new line after
After taking quite a long break from active coding I am just starting to
I have text within one of my tables which is a long description, but

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.