Thread: single vs double quotes

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    single vs double quotes

    Hi,
    could anybody tell me about the differences between using single and double quotation?

    thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Single quotations are primarily used when denoting a single character:

    Code:
    char letter = 'e' ;
    Double quotations are used with strings and output statements:

    Code:
    string MyString = "This is a string";
    cout << "This is an instance where double quotations are used.";
    If there are any other distinctions, someone else can tell them; those are the only ones I can think of off the top of my head.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    That was quick Velocity.
    thanks

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    More formally, single quotes delimit a character literal, double quotes delimit a string literal.
    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

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    to expand upon what CornedBee said, you can use single quotes for a lot of interesting things.

    for example:
    Code:
    int myint = 'abcd';
    is perfectly valid code, and should give you no warnings whereas
    Code:
    int myint = "abcd";
    is likely to give you warnings about converting const char* to int

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note however that the actual integer value of [code]int myint = 'abcd';[/cod] may be either of (or, technically something else entirely as it is up to the compiler to make it into an integer, and the compiler is allowed to do "anything it likes as long as 'abcd' and 'abce' are different), but that is unlikely):
    Code:
    int myint = 'a' + ('b' << 8) + ('c' << 16) + ('d' << 24);
    // or
    int myint = 'd' + ('c' << 8) + ('b' << 16) + ('a' << 24);
    That of course assumes that the integer is 32-bit (which is commonly the case, but older compilers may have 16-bit int, which would make the compiler grumble about "value to large for integer type" or some such).

    So whilst it is a clever way to store a small number of characters in a single integer value, it is not necessarily good if you want code that is portable to different types of processor architectures.

    --
    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.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elkvis View Post
    to expand upon what CornedBee said, you can use single quotes for a lot of interesting things.

    for example:
    Code:
    int myint = 'abcd';
    is perfectly valid code, and should give you no warnings
    Code:
    lit.cpp:2:13: warning: multi-character character constant
    The result of multi-character character constants is implementation-defined and highly unportable. GCC therefore warns about it by default.

    whereas
    Code:
    int myint = "abcd";
    is likely to give you warnings about converting const char* to int
    Errors, not warnings.
    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

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Errors, not warnings.
    Not if you compile it as C++, which this section of the cboard is relating to. Then gcc (g++) gives an error.

    --
    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.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    is likely to give you warnings about converting const char* to int
    Errors! Errors away!
    Warnings in C, errors in C++!

    Quote Originally Posted by matsp View Post
    Not if you compile it as C++, which this section of the cboard is relating to. Then gcc (g++) gives an error.
    Correction: All compilers will spit out an error or they are broken
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Errors! Errors away!
    Warnings in C, errors in C++!



    Correction: All compilers will spit out an error or they are broken
    All C++ compilers, yes.

    --
    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.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    Warnings in C, errors in C++!

    Correction: All compilers will spit out an error or they are broken
    Further correction, that is an error in both C and C++... I really wish you would learn where these languages are different. I'll show you here:

    Code:
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:non-strict warnings C90 
    
    "ComeauTest.c", line 3: error: a value of type "char *" cannot be used to initialize
              an entity of type "int"
        int foo = "bar";
                  ^
    
    1 error detected in the compilation of "ComeauTest.c".

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by citizen View Post
    Further correction, that is an error in both C and C++... I really wish you would learn where these languages are different. I'll show you here:
    When I write warning, I meant that the compiler spits out a warning instead of an error (which most compilers do), and not that it is not a programmer error in itself (that, it is).
    But how are you supposed to know this when all C compilers spits of warnings instead of errors?!?!?!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The term for any given line in a compiler's output is diagnostic. The standard defines this term explicitly and uses it where the compiler needs to say something. There are two types of diagnostics I am aware of, warnings and errors. Their difference is that only errors will stop compilation.

    Comeau's output is strong evidence that even in C90, compilation will stop if you initialize int with a char *. QED, it is an error.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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

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