Thread: multiple characters within single quotes

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    multiple characters within single quotes

    Can I place multiple characters within single quotes? For example, is the result of the following statement defined in C:

    Code:
    'abc';
    If this is defined, how does it work and which C standard supports it?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    No. Single quotes wrap single characters. That's the syntax.

    The only time > 1 character is allowed within single quotes is when the first character is an escape character.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Todd Burch View Post
    No. Single quotes wrap single characters. That's the syntax.

    The only time > 1 character is allowed within single quotes is when the first character is an escape character.
    That's not entirely true. Multicharacter literals are part of the C standard. The following is valid C code:
    Code:
    int x = 'ABCD';
    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"

  4. #4
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Quote Originally Posted by iMalc View Post
    The following is valid C code:
    Code:
    int x = 'ABCD';
    Yes, you can do this and for some reason it produces: x = 1094861636. There is not much reason to do so, x is not an address in memory nor is it multiplying the ASCII code for each character, thats what this does:
    Code:
    int x = 'A' * 'B'* 'C' * 'D'
    which also does not produce a warning. I can see more of a reason to multiply the ASCII code for each character as this can also be modified to use the other operations aswell, treating characters as numbers. It still is not explicit what the code will actually do reading it, characters are not generally thought of as their ASCII equivalent and other languages would probably crash when given the same code.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by P4R4N01D View Post
    Yes, you can do this and for some reason it produces: x = 1094861636. There is not much reason to do so, x is not an address in memory nor is it multiplying the ASCII code for each character, thats what this does:
    Code:
    int x = 'A' * 'B'* 'C' * 'D'
    which also does not produce a warning. I can see more of a reason to multiply the ASCII code for each character as this can also be modified to use the other operations aswell, treating characters as numbers. It still is not explicit what the code will actually do reading it, characters are not generally thought of as their ASCII equivalent and other languages would probably crash when given the same code.
    This format is quite often used to make multibyte "magic" numbers, such as identifiers of file-formats (for example, BMP files contain 'BM' in the header).

    The value is actually ('A' << 24) + ('B' << 16) + ('C' << 8) + 'D', or ('D' << 24) + ('C' << 16) + ('B' << 8) + 'A' depending on the byte-order of the system.

    The real benefit of storing it as one int, rather than a character array, is that it can trivially be compared to another item of the same kind with one single instruction, rather than comparing a character array which requires (say) 4 operations, if you don't try to have fun with casting and pointers and such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. combining multiple libs into a single lib
    By eburghardt in forum Windows Programming
    Replies: 0
    Last Post: 06-16-2006, 02:51 PM
  2. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  3. How to read single characters in a string
    By MaxxMan-X in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 02:52 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Single API for multiple CGI
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 10-09-2001, 06:31 AM