Thread: single vs double quotes

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by laserlight View Post
    C99 section 6.3.2.3 states that "any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type."

    Now, "bar" is of type const char[4], so we have the usual conversion to const char*, thus it seems that initialising an int with "bar" is not necessarily an error in C, though it is almost certainly a mistake.
    It's an error. The C standard is organized even worse than the C++ standard, but 6.5.4p3 says, "Conversions that involve pointers, other than where permitted by the constraints of 6.5.16.1, shall be specified by means of an explicit cast."
    And said section permits, paraphrased:
    1) Conversion between arithmetic types.
    2) Conversion between compatible structures and unions.
    3) Conversion between pointers to compatible types, where cv qualification is not lost.
    4) Conversion between void* and other pointer types, where cv qualification is not lost.
    5) Conversion from a null pointer constant to a pointer.
    6) Conversion from a pointer to _Bool.

    Quote Originally Posted by Raigne View Post
    Microsoft Visual C++ 2008 does not raise an error with
    Code:
    int t = 'abcd';
    //Actually the value is 1633837924
    and also does not raise error with
    Code:
    int t = "abcd";
    //the value is 4211020
    Hmm ... what mode (C or C++) and warning level? There should be at least a warning. (There's an awful lot of really, really bad legacy C code out there, which is why most compilers accept the code. But they should still warn.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    All warnings are default, and build is debug, and C++

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think one bottom line thing to say that neither C nor C++ nor most non-interpreted languages perceive a single quote the same as a double quote the way as an interpreted language does.

    JavaScript
    Code:
    document.alert('Holy smokes! You are being annoyed by a pop-up message!');
    That is cool. But in C++ that is not cool at all. Luckily your compiler will notify you of the difference when you make a "boo boo."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  4. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  5. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM