Examveda
Examveda

What will be the output of the following program?
void main()
{
      char str1[] = "abcd";
      char str2[] = "abcd";
      if(str1==str2)
            printf("Equal");
      else
            printf("Unequal");
}

A. Equal

B. Unequal

C. Error

D. None of these.

Answer: Option B

Solution(By Examveda Team)

Strings are compared using strcmp() function defined under string.h header file.


This Question Belongs to C Program >> Arrays And Strings

Join The Discussion

Comments ( 12 )

  1. Tt Y
    Tt Y :
    1 year ago

    How this program runs explain

  2. Moza Alshkhari
    Moza Alshkhari :
    4 years ago

    String str1 = “ABCD“ ;
    String str2 = str1.toLowerCase();
    str2.replace(‘a’ , ‘b’ );
    str2.replace(‘b’ , ‘c’ );
    System.out.printIn(str2);

  3. Vrunda Savaliya
    Vrunda Savaliya :
    5 years ago

    We can not compare string with operators so i think ans c is right .

  4. Anti Hero
    Anti Hero :
    5 years ago

    str1 and str2 are both separate memory allocations for the array elements, when the condition says str1==str2 , we are comparing its memory allocation and not the string i.e. "abcd", and since both the strings cannot be in the same memory location , therefore the answer will always be UNEQUAL no matter what the string may be, well atleast that is what I understood.

  5. Saket Srivastava
    Saket Srivastava :
    5 years ago

    String is not compared the way it is being compared like integers. That's why the answer is "Unequal".

  6. Dhiraj Chandanshive
    Dhiraj Chandanshive :
    7 years ago

    i thing c answer is correct

  7. Don Satish
    Don Satish :
    7 years ago

    hi, friends !!!

    if(str1==str2);// we will get if(0) i.e if(false)

    so the else part is executed.

  8. Md Iqbal
    Md Iqbal :
    7 years ago

    ans is error , braces missing sir

  9. Mayank Thaldi
    Mayank Thaldi :
    7 years ago

    ANKITA U R RIGHT !!

  10. Ankita Awasthi
    Ankita Awasthi :
    7 years ago

    It's a run time error...answer is c)error

  11. Arif Saifi
    Arif Saifi :
    8 years ago

    what do compare == operator. Is this match address

  12. Pramod Tonape
    Pramod Tonape :
    8 years ago

    i am not getting.
    can u tell in simple...................

Related Questions on Arrays and Strings