Thread: How to compare two characters???

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Arrow How to compare two characters???

    i have a question here...
    How do I compare two characters?

    i.e.
    Code:
        char rep1, rep2;
    
        scanf("%c %c", &rep1, &rep2);
        
        
        if('rep1' == 'rep2')
                  printf("Hello\n ");


    what's wrong with the statement?

    And how do I write that code if i want to print "Hello"
    if rep1 == rep2 ??? they are Characters!!

    thanks

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Lose the single quotes there and then it will print Hello if you enter two of the same character.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Quote Originally Posted by iMalc View Post
    Lose the single quotes there and then it will print Hello
    .
    I've tried....it doesnt work

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    iMalc is right, lose the single quotes.

    Also you should be getting compile warnings with the way you had it originally.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    ...well....I don't know it still not working
    Last edited by readytogo; 11-05-2010 at 01:34 PM.

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Actually dropping the single quotes is the correct thing to do.

    Where do you get the idea that you can't compare char's? It is perfectly legal.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    If you still do not have this working? How are you compiling this? Are you actually giving arguments to your executable once compiled and hopefully without any warnings like "multi-character constant" or something like that.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Quote Originally Posted by carrotcake1029 View Post
    Actually dropping the single quotes is the correct thing to do.

    Where do you get the idea that you can't compare char's? It is perfectly legal.

    it gives me the warning sign

    "multi-character character constant"

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    are you sure you recompiled?

    try
    b='b';
    a='a';
    if (a==b)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. problem with compare two characters
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-09-2009, 12:45 PM
  3. Using if statments to compare characters
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2006, 01:11 PM
  4. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  5. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM