-3

I don't see the point of having the use of single quotes reserved for single characters. So, when is this used? i.e char a = 'a'; Coming from Javascript and PHP, I'm used to being able to use single quotes for entire strings, but after learning that single quotes are reserved for characters in C++, I'm curious to know why? Why would you need a single character?

Cerran
  • 2,087
  • 2
  • 21
  • 33
Azrael
  • 621
  • 2
  • 7
  • 16
  • 1
    Because if not, you will ask why there is no single character in C++. – Maroun Jan 28 '14 at 07:23
  • 2
    PHP is quite a bit higher level abstraction wise. When working directly with memory, the distinction between a char and a one character string is quite useful, while rightfully less so for a high level language that does not deal (as much) with direct memory access. – Joachim Isaksson Jan 28 '14 at 07:26
  • 1
    Because there is a difference between a single character `'a'` and a null-terminated array of characters `"foo"`. – Blastfurnace Jan 28 '14 at 07:27
  • @Blastfurnace Not only that, there's also a big difference between `'a'` and `"a"`. – Maroun Jan 28 '14 at 07:28
  • @ᴍarounᴍaroun It is the same difference. `"a"` is a null-terminated array of characters, just like `"foo"` is. – juanchopanza Jan 28 '14 at 07:30
  • @juanchopanza I know, but OP might think `"a"` and `'a'` are the same just because they contain the same characters. – Maroun Jan 28 '14 at 07:31
  • @ᴍarounᴍaroun: You are both correct. I was a little concerned that my dumb example might confuse the issue. – Blastfurnace Jan 28 '14 at 07:36
  • So, the differences are "a" is null-terminated, while 'a' is not. And 'a' gets replaced with the ASCII numerical values, while "a" does not? Are these really the only differences? – Azrael Jan 28 '14 at 07:43
  • @Elitis, No. [Read a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1). – Shoe Jan 28 '14 at 07:51
  • 1
    @Elitis Both C and C++ existed for more than 30 years, that is a looong time in IT, back then computers used to be millions times slower then what we have today and had laughable in comparison amounts of memory. Things like this used to make a colossal difference. Also, C/C++ are by today standards quite low level and thus closer to how underlying hardware works, high level abstractions like JS/PHP just hide that stuff from you. – user2802841 Jan 28 '14 at 08:06

4 Answers4

3

PHP and JavaScript are languages that operate at quite a high level. This means that the basic types are essentially just a few different types, whose implementation is hidden inside a set of functions in the actual script engine.

C and C++, as well as most other low level languages expose more of "how the machine works". A string, in C, is a sequence of characters. If you want to deal with strings, you need to be able to deal with their components, which is char. A single character becomes useful when you want to build strings, compare the contents of strings, etc. Naturally, for normal string operations in C++, you'd use std::string, and then, like in script languages, most aspects of how the string is actually represented is hidden inside the std::string class implementation, so you don't really need to care about it. But if you were to "look inside" a std::string, it would somewhere sooner or later, become a char *, which is a pointer to a piece of memory that contains a sequence of characters, individual char elements.

One could look at it like going from having "ready made big lumps of Lego" to having only small pieces to work with. You can still build the same things, but it requires more pieces, and requires a bit more effort to construct. What you win is flexibility and speed. A char is really easy to deal with for the processor, where a single character in PHP is still represented as a string - it just happens to be one element long. As such, there is extra overhead in keeping track of this one character string, where it's stored, how long it is, etc, because the functionality in the language doesn't make any distinction between a single character and a string of a megabyte.

The purpose of C, and to a large degree also C++, is to closely represent the hardware. So your basic types are much closer to what the actual hardware representation is, and this is something you will need to learn more about if you are going to understand C and C++ well. Unfortunately, to cover ALL of that would be far beyond a single answer in SO. You will need to get yourself a good C and/or C++ book.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

To run things faster and efficient you must learn how to use less space needed. When you have an 'a', this is actually a number (see ASCII table), but when you got "a" it is an array of 2 characters {'a','\0'}. The zero is to know when your string ends because the computer is not sure when the string ends. Do you want to add a length property like in javascript, to know directly the string's length? You use more space that may not be needed. Somehow you have to distinguish these two values to run efficient code. Learning C/C++ first you actually learn how things work on the low level of your computer and understand more than by learning php/javascript/ruby/python first. C++ is more customizable than higher level programming languages.

Theofilos Mouratidis
  • 1,146
  • 12
  • 32
1

Javascript and PHP are scripting languages while C++ (and especially its predecessor C) is quite low-level native programming language where you have to consider how your variables are stored in memory.

char a = 'a'; creates a 8-bit-long numeric variable that can hold character value (ASCII code) and put the value of the character a into it. So char a = 97; does the same work.

const char* s = "a"; creates a null terminated string which is an array with two elements: the value of character a and the terminating 0 character (just number 0 or the character '\0'). The * means we create a pointer to the array. Its type is const char because it contains string literal which is constant. We could create an identical array using const char s[2] = { 97, 0 }; or const char s[2] = { 'a', '\0' };.


By the way, single quotes are not reserved exclusively for single characters. You can put a few characters into single quotes. See What do single quotes do in C++ when used on multiple characters?.
Community
  • 1
  • 1
Melebius
  • 6,183
  • 4
  • 39
  • 52
  • 8-bit long? Says who? – Shoe Jan 28 '14 at 07:37
  • Adding to what @Jefffrey said, the size of a `char` is not set to 8 bits in C++. This is platform dependent. There is a minumum size, and the *byte* is defined to be the same size of the `char` (a `char` being one byte long). – juanchopanza Jan 28 '14 at 07:43
  • 1
    @Jefffrey "At least 8 bit long" if you want to be completely correct. I really don't think this is the most important thing a novice must know. – Melebius Jan 28 '14 at 07:44
  • @Melebius But a novice should know not to assign a string literal to a pointer to non-const. So `const char* s = "a";` would be better :) – juanchopanza Jan 28 '14 at 07:48
  • @Melebius I really wouldn't call myself a novice. But obviously I'm not advanced either. Just digging for details. – Azrael Jan 28 '14 at 07:48
  • "I really don't think this is the most important thing a novice must know" -- Then why did you mention it at all? – Shoe Jan 28 '14 at 07:49
  • I guess because this is the most common implementation of C++. – Spook Jan 28 '14 at 07:52
  • @Spook, being right some cases doesn't make it correct. – Shoe Jan 28 '14 at 08:11
  • The 8-bit detail is something you could have easily substituted with the word *byte* and no one would have complained. Instead you went there incorrectly and later said "I really don't think this is the most important thing a novice must know". This doesn't make any sense. Please remove the "8-bit" part from the answer. – Shoe Jan 28 '14 at 08:13
0

The language C++ inherited scalar types from the language C. So you should get a good book about C++ or C to get the details.

The type char is a numeric type. It's an integer that can old a character. It's usually signed but can be overwritten with the signed or unsigned prefix. Additionally the signedness can be configured at the compiler. The 'A' literal defines a number that is identical to the code of the character A.

The "a" string is an array of char's and has a zero termination. In the example you have two bytes 'a','\0'. When you use the literal the compiler passes the address as a pointer to the array. This pointer can be assigned to pointer variable

char *s = "A";

or passed to a function

foo("A");

Additionally there are all the pointer arithmentics possible that you can grasp when you got the meaning of a char array.

Edit Both the number literal 'a' and the char array "A" are const objects. It's obvious that you can't assign anything to a number like

'a' = 23; // wrong!

When you assigned the literal to variable you can change the variable later tough.

But when you stored the pointer in a pointer variable it's illegal and you get undefined behavior when you try to modify the char array:

char *s = "A";
*s = 'B';  // try to change the first byte of the char array, causes undefined behavior.

To express this it's good style to use a pointer to const char variable:

const char *s = "A";
*s = 'B'; // compiler diagnostic says: not allowed
harper
  • 13,345
  • 8
  • 56
  • 105