In the previous section we have used the library function printf() with a single string as parameter. This appeared as
printf("hello world\n");
The double quote character used to enclose strings should not be confused with a pair of single quote characters.
If you have followed what has been said so far you may well be wondering why the character \n was included in the string and why it didn't appear in the output. The answer is that this pair of characters is converted by the compiler into a single new-line character that is stored as part of the string. Actual new-line characters in the source of the program are treated as if they were space characters by the compiler, so the escape convention is necessary to get a new-line character into the string.
The program example used earlier to illustrate multiple statements also illustrates the significance of the \n character in the strings. If the \n were left off the end of the last line of program output then the effect would be that the next operating system prompt would appear on the same line as the output of the program.
You can include as many \n's as you like within a string enabling multi-line output to be produced with a single use of the printf() function. Here's an example.
main() { printf("This is an\nexample of\nmulti-line output\n"); }When the program was compiled and run it produced the following output.
This is an example of multi-line output
It would not be possible to include an actual new-line character in the string in the program source because this would "mess-up" the source and the compiler would not be able to translate the program properly.
However if the string is too long to fit comfortably on a single line of the source listing of program then it is possible to spread a string over several lines by escaping the actual new-line character at the end of a line by preceding it with a backslash. The string may then be continued on the next line as the following example shows.
main() { printf("hello, \ world\n"); }Note that there are no characters after the backslash on the third line and also note that the continuation string must start at the very start of the next line. The following nicely laid out version of the above program.
main() { printf("hello, \ world\n"); }produced the output
hello, worldthe indenting spaces at the start of the string continuation being taken as part of the string.
An alternative, and probably nicer, approach is to make use of what is called string concatenation. This simply means that two strings which are only separated by spaces are regarded by the compiler as a single string. Actually the two strings can be separated by any combination of spaces, newlines, tab characters and comments. In the jargon of C programming all these things are collectively known as white space although perhaps we should now say "content challenged space". The use of string concatenation is shown by the following example.
main() { printf("hello," /* space only */ " world\n"); }
Programmers using MSDOS should note that versions of printf() supplied with MSDOS compilers normally convert \n to a carriage return and line feed (new-line) character pair, there is no need to try and include a carriage return character in a string. On Unix systems this is the responsibility of the display driving software.
There are several other characters that cannot conveniently be included in a string but which can be included using the \ notation. The complete list is
\a "BELL" - i.e. a beep \b Backspace \f Form Feed (new page) \n New Line (line feed) \r Real carriage return \t Tab \v Vertical Tab \\ Backslash \' Single Quote \" Double Quote \? Question MarkIt is not necessary to use the \ convention for all the above characters in strings but they will always be understood.
It is also possible to include arbitrary characters in strings by including the three digit octal representation of the character preceded by a \ symbol. For example the "hello world" program could be written in this form.
main() { printf("\150ello, world\n"); }In case you didn't know, 150 is the octal code for "h". It is not, generally, possible to use hexadecimal or decimal constants in this fashion. The use of \ in this way is known as "escaping" and the \ is known as an "escape" character, it turns off the normal meaning of the immediately following character.
The effect is actually to send the string of binary 1s and 0s equivalent to octal 150 to the output device which should respond in the manner its designers intended it to. You can use this convention to send all sorts of unusual special characters to all sorts of unusual output devices.