| Quotes ('"..."')

Previous | Punctuators | Next

Quotes are used for defining string constants. In the compile-time, the sequence of characters between qoutes are stored somewhere in the executable file. In the run time, the "result" of quotes is a pointer (of type `'char*'`) which points to the place where the sequence of characters is stored. This interpretation of quotes differs significantly from the interpretation of strings in other languages. That's why there is nothing wrong with the following C code (it stores the address where the text "Hello" is stored, interpreted as an integer, in `a`):

    int a;
    a = (int)"Hello";

Such constructs are usually impossible in other languages. Note that sequences of characters between quotes behave similarly like arrays declared with the static keyword, i.e. they can survive the end of a function or program if they are changed somewhere in the program (anyway, it is very bad idea to change the content of a string constant). For example, the following code

    for (i = 0; i < 2; i++)
      {
        char *str;
        str = "xyz";
        printf (str);
        *str = 'a';
      }

will display "xyzayz", although it seems that "xyzxyz" would be displayed. Such strange behaviour is caused by the fact that "xyz" is initialized in the compile-time. Now, if you understand the correct interpretation of what the quote punctuator does, you can explain this behaviour easily. To learn: strings in C behave quite differently than in most other languages!
Note: These two statements are very different, although most books say that they are nearly the same:

    char str[] = "Hello";
    char *str = "Hello";

Suppose that str is declared in the body of the function (i.e. it is an automatic local variable). In both cases, the text "Hello" is stored somewhere in memory, and the "result" of `"Hello"` is the address where it is stored (we will call this address addr). In the first case, str is a local array (i.e. it is created on the stack at run time, and there will be 6 bytes reserved for it), whose content is initialized at run-time (because it does not exist at compile-time) with the sequence of bytes located at addr. In other words, it is the same as you wrote:

    char str[6];
    strcpy (str, "Hello");

The consequence is that if you change the content of str (note that this is not the same as changing bytes pointed to by addr), it will be reinitialized each time when this statement is encountered again. But, in the second case, str is a local pointer, whose content is initialized (at run-time) with addr! So, if the contents of the string "Hello" are changed, this change will be permanent, because when this statement is encountered again, str will simply be reinitialized (if changed) to addr but bytes pointed to by it are not restored! Confused? See the following code:

    for (i = 0; i < 2; i++)
      {
        char str[] = "Hello";
        printf (str);
        str[0] = 'a';
      }

This program will work as expected (it will display "Hello" twice). But, if you change `'str[]'` to `'*str'`, it will not work as expected (it will display "Hello" and then "aello").
Generally, it is a very bad idea to change strings which are the result of quotes. The reason is that usually equal strings are stored only once in the program. This means that you can get unexpected results if you modify them.
If the '\' character is found inside a string literal, it is threated as the start of an escape code. Here is an incomplete list of possible escape codes:

\\ | Represents one `'\'` character.
\" | Represents one `'"'` character. Needed in strings to represent this character, because an unescaped `'"'` would end the string.
\n | Newline; for ASCII this is octal code 012.
\b | Backspace; for ASCII this is octal code 010.
\f | FormFeed; for ASCII this is octal code 014.
\r | Carriage-Return; for ASCII this is octal code 015.
\t | Horizontal Tab; for ASCII this is octal code 011.
\ddd | An octal character code. The numeric code is 3 octal digits.
\x dd... | A hex character code. All trailing hex digits are combined.