The switch statement when comparing a character

This is a discussion on The switch statement when comparing a character within the C Programming forums, part of the General Programming Boards category; We have code like this in an old parser we wrote: Code: char *word; ... ... switch (*word) { case ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    The switch statement when comparing a character

    We have code like this in an old parser we wrote:

    Code:
    char *word;
    
    ...
    ...
    switch (*word)
    {
       case 'A': ...; break;
       case 'B'; ...; break;
       case '-'; ...; break;
       ...
    }
    Now if we use the generic data type TCHAR, do we code it like this?

    Code:
    TCHAR *word;
    
    ...
    ...
    switch (*word)
    {
       case _T('A'): ...; break;
       case _T('B'); ...; break;
       case _T('-'); ...; break;
       ...
    }
    or does the switch statement assumes a single byte character?

    Thanks,

    MC

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It works with integeral types. So it's fine to do:
    Code:
    case 12345:
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    But I was not comparing integers, I was comparing characters. So what you are saying underneath it really compares the ASCII value of the characters?

    If so than the "wide" version will not work correct?

    MC

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Depends what a TCHAR is. If TCHAR isn't an alias for some integral type, then no. TCHAR is not a standard C type.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    TCHAR is a macro which means char when UNICODE is defined and means wchar_t when UNICODE is not defined.

    Reference:
    http://msdn.microsoft.com/library/de...n_tchar..h.asp

    So in summary, my question is really if TCHAR is wchar_t would this work?

    MC

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Again, is TCHAR integeral? Then yes. If not, then no. That's how a case works. A char is the same thing as an int, it's just most likely smaller, and its sign might possibly be different. Other than that, they're identical.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 07:32 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 02:12 PM
  3. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 09:13 AM
  4. Logical error in Switch Statement..
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 05-19-2003, 11:15 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21