array of character type

• 06-10-2011
jackson6612
array of character type
Hi :)

I was under the impression that the character arrays work as regular arrays such as of type int, float etc. I was thinking that one can put more than one digit (e.g. 12, 13, etc.) or more than one letter (such as ab, bb, ccc, etc.).

Please have a look on this ASCII table.

The character values given for decimal values from "0" to "32" consist of more than one character. For example, for decimal value "0" the character value given is "NUL", and for decimal value "9" we have character value "TAB".

I was wrong. An array element of character type can contain only one digit or character. The character values which consist of more than character such as "NUL" or "TAB" stand for particular function or signs. For example, "TAB" is a set of white spaces, and "Space" (decimal value is 32) is single white space.

Please correct me if I'm wrong without confusing me more!:p

Code:

```// learning_character_strings.cpp // learning how character string work #include <iostream> #include <cstdlib> #include <cstring> using namespace std; int main() {         int i;         const int C = 5;         char string[C];         for (i=0; i<C; i++)         {                 cout << "enter string #" << (i+1) << ": ";                 cin >> string[i];         }         cout << "\n\n";         for (i=0; i<C; i++)         {                 cout << "string #" << (i+1) << " is: " << string[i] << endl;         }         system("pause");         return 0; }```
• 06-10-2011
tabstop
"TAB" is a string of three characters (plus implicit \0 at the end). '\t' is a tab character. It represents one single character, the character you get by hitting the big button that says Tab on the left side of your keyboard. It is not the three characters T, A, B. Same with all the control characters.

ETA: And going back to the beginning: you can only put one "thing" in each slot of an array. An array of ints can hold several ints, but each slot just holds one int. The number of digits is irrelevant (other than the fact that int is only "so big").
• 06-10-2011
godly 20
You didn't ask a question, but i think i understood what you need anyways. Basically you need an array that holds in each position a multi-character. If yes then include the string library and declare the array as it follows:
Code:

```#include <string.h> string array[n];```
Hope this helps :)
• 06-10-2011
jackson6612
Thank you, tabstop, godly.

On the ASCII table decimal 7 stands for ASCII "BEL" which I think produces the sound of a bell. In the following code I have tried to use "BEL" but it doesn't work. I need to hear the sound of a bell! Please help me. Thank you.

Code:

```#include <iostream> #include <cstdlib> #include <cstring> using namespace std; int main() {         int i;         const int C = 4;         char string[C];         for (i=0; i<C; i++)         {                 cout << "enter string #" << (i+1) << ": ";                 cin >> string[i];         }         cout << "\n\n";         for (i=0; i<C; i++)         {                 cout << "string #" << (i+1) << " is: " << string[i] << endl;         }         cout << static_cast<char>(int 7) << endl;         system("pause");         return 0; }```
• 06-10-2011
laserlight
It should be static_cast<char>(7), but you might as well write '\a'.
• 06-10-2011
Elysia
Jackson, your code is dangerous since you are using a standard library class name (string) and using "using namespace std". If you include <string> now, you will get a nameclash.
Therefore, avoid using "string" as a name or stop using "using namespace std" I would suggest.
• 06-10-2011
jackson6612
Thank you, laserlight, Elysia.

@ Elysia: Now I realize this; I should have used some other identifier name for that array.

These are some of the escape sequences: \n, \t, \a, \f.

I see they are called characters. Please have a look here:

But I don't get it. For example, "\n" is not a single character - these are two characters "\" and "n". And you have to use quotation marks around these so-called characters instead of single quotes which are used with normal characters such as 'a', 'c'.
• 06-10-2011
jimblumberg
Quote:

But I don't get it. For example, "\n" is not a single character - these are two characters "\" and "n". And you have to use quotation marks around these so-called characters instead of single quotes which are used with normal characters such as 'a', 'c'.
You use the '\n' with single quotes. The backslash character '\' denotes an escape sequence and means that the next character is a special non-printing character or to denote that the character is a single or double quote or backslash.

Jim
• 06-11-2011
Elysia
'\n' is a single character. There is no need to use double quotes around them.
'\n' is a special character that represents a newline. It couldn't be represented with a single character since there's no easy button on your keyboard to use to represent that. Obviously all keys on your keyboard are reserved for other characters.
So a special exception was made: If you put \ and then another character, it would be a "single character" as seen by the language.
• 06-11-2011
jackson6612
Yes, Using '\n' does work the same way as "\n". But I have almost always seen the "\n" being used with double quotes.

Code:

```// character backslash n.cpp #include <iostream> #include <cstdlib> using namespace std; int main() {         cout << "hello world" << '\n';         cout << "hello world" << "\n";         cout << "hello world\n";         system("pause");         return 0; }```
• 06-11-2011
grumpy
In your example it works the same, as all you are probably examining is the output.

The sequence of actual operator functions being called to output the '\n' character and the "\n" string are different between your first and second lines (different overloads). You will not detect that without the aid of a debugger, but it is a real difference.

The fact you don't detect a difference doesn't mean there isn't one. In your case, it reflects you using a technique that cannot directly detect the differences.