single vs double quotes

This is a discussion on single vs double quotes within the C++ Programming forums, part of the General Programming Boards category; Hi, could anybody tell me about the differences between using single and double quotation? thanks...

  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,893
    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
    2,421
    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,893
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    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
    21,744
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

Page 1 of 2 12 LastLast
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

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