Thread: Compare 2 String

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    12

    Compare 2 String

    Code:
    #include <stdio.h>
    main()
    {
    char a[10], b[10];
    clrscr();
    printf("name1 : ");fgets(a,10,stdin);
    printf("name 2 : ");fgets(b,10,stdin);
    
    if(a = b){
    	printf("same");
    }else{
    	printf("different");
    	}
    getch();
    return 0;
    }
    Even i input different string the output always say first statement / "same" ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to compile with warnings on, and it would tell you that:

    = is the assignment operator.
    == is the comparison operator.

    That wouldn't actually fix your main problem however. You cannot compare strings with ==. Instead you need something like strcmp, which is located in string.h.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    thx ill try that strcmp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM