-6

I know the difference between 9 and '9'...that one is used as the integer 9 and the other is stored as a character 9 with a specific ASCII value but what is the significance of " " in C++?
I am a beginner so I just know that the " " are used while writing cin>> and cout<< but what else is its significance in the programming language and what meaning does "9" carry?

oshhh
  • 141
  • 2
  • 10
  • You need to learn about strings. – SLaks Aug 15 '16 at 14:43
  • 3
    Single quotes for character literals, double quotes for strings. You probably need [a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Aug 15 '16 at 14:43
  • 2
    Double quotes are *string literals*. Please, buy an introductory book on C++, or work through tutorials before posting questions about language fundamentals. – Jonathon Reinhart Aug 15 '16 at 14:44
  • @JoachimPileborg That was useful...btw nothing to do with the book, I just needed to learn about strings, not done that yet, just started. – oshhh Aug 15 '16 at 14:47
  • `'9'` is not required to be ASCII. It's encoded in whatever encoding the compiler uses, and there are systems that don't use ASCII. – Pete Becker Aug 15 '16 at 15:07

1 Answers1

2

9 is a literal of type int.

'9' is a literal of type char.

"9" is a literal of type const char[2]: the first element is the character 9 (the actual numeric value depends on the encoding the compiler is using; if ascii is used - which is common - then the value will be 57), the second is the null-terminator.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    s/`char[2]`/`const char[2]` – NathanOliver Aug 15 '16 at 14:47
  • @NathanOliver Interesting. I don't know about C++, but I'm sure that is not true in C. Otherwise, `char *p = "whatever";` would fail to compile with "...discards const qualifier". – Jonathon Reinhart Aug 15 '16 at 14:51
  • 2
    @JonathonReinhart In C++ `char *p = "whatever";` should fail to compile with a standard conforming compiler. All string literals are `const`. – NathanOliver Aug 15 '16 at 14:52
  • 3
    @JonathonReinhart: my understanding in C++ is that compilers are lapse when it comes to this. But it ought to fail at compile time. – Bathsheba Aug 15 '16 at 14:52
  • 2
    @JonathonReinhart The standard says *Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration* – NathanOliver Aug 15 '16 at 14:54
  • 1
    The first element does not have to have ASCII value 57. Its value depends on the encoding that the compiler uses, and some systems do not use ASCII. – Pete Becker Aug 15 '16 at 15:10
  • @PeteBecker: that's very true. I'll amend. – Bathsheba Aug 15 '16 at 15:13
  • 1
    @JonathonReinhart - back in C++03, there was an implicit conversion from "array of const char" to "pointer to char", for backward compatibility; that conversion was deprecated, and in C++11 it was removed. – Pete Becker Aug 15 '16 at 15:28
  • 1
    @NathanOliver - re; "`char *p = "whatever":` should fail to compile..." - no, that's not required. The **only** requirement is that the compiler issue a diagnostic. Having done that, it's free to continue to compile the code. That's a hook for implementation-specific extensions; in this case, it allows backward compatibility for code that relied on the old C and C++ rule that allowed that conversion. – Pete Becker Aug 15 '16 at 15:31
  • @PeteBecker Good point. I though g++ and clang did not at least warn for this but now I can't seem to find that behavior. Maybe I am just confusing actually looking at the warnings by the OP's with not issuing them. – NathanOliver Aug 15 '16 at 15:38