The <i>newline</i> Character - and other vagaries!




Document: Software Engineering 1: Course Notes

next VariablesData Types and Arrays
up Chapter 2 pp. 11-19
previous A Shady Character?

The newline Character - and other vagaries!

One special problem in dealing with characters is how to represent the idea of a "line break" or a "new line". In the ASCII character set there are actually two separate numeric codes associated with the idea of line breaking - one called "carriage return" the other called "line feed". The terminology goes back to mechanical typewriters and teletypes, where moving on to a new line required two separate actions: the "carriage" carrying the roller and paper had to be moved back to the extreme right (so that the left hand edge of the paper was relocated under the printing position) and the paper had to be "fed" through the roller by the correct amount for a new line. Of course, these quaint historical origins have rather little to do with the operation of modern computer displays; but the distinction between moving the print position (the cursor) down a line, and moving it back to the left hand margin still exists, and so the distinct functions of the two ASCII control codes still apply. Nonetheless, it is conventional in C programming to have a single control code which is denoted by the sequence \n and which has the effect of both moving down a line, and moving back to the left hand margin. This special character is called the newline character.

Why cannot a newline simply be represented in the C file by, literally, inserting a new line? Why do we need this special sequence \n? The basic answer is that line breaks are used in a C program to try to lay it out in a legible manner; and that if we put in line breaks in the C file where what we really want is to have the program emit a line break at run time, then this will disrupt and interfere with the formatting of the C program. In fact, within C string constants, which is where line breaks are usually desired, it is illegal to insert a literal line break, and a compiler error would be generated. So it is necessary to invent some kind of more or less artificial way of indicating the idea of a line break, or the "newline" character within a C string, and \n is the way it is done. Note that when you write a statement like:

    printf("\n");

then the string which is actually stored internally in the computer at run time (and passed as an argument to printf()) contains just one character - the ASCII newline character, and not the two separate ASCII characters backslash and the letter n. In turn, what the printf() function actually "sends" to the screen is not the two separate characters \ and n, but rather the single ASCII newline character.gif

The sequence \n is called an "escape" sequence: the idea is that the character n is "escaping" from its normal interpretation, and is, instead, being given a special interpretation. Its a "sequence" because the n must be preceded by the special "escape" character, which, in this case, is the backslash character, \ . The use of an escape sequence to represent a special ASCII character (such as newline) introduces a difficulty of its own: what if we want to actually put the backslash character itself into a string? How can we prevent it being interpreted as an "escape" character? Well, the answer is obvious enough: if we want to incorporate, literally, the backslash character \ into a C string, we simply escape this escape character itself: i.e., we write \\ . Thus to print the backslash character using printf() we would write:

    printf("\\");

As well as the newline character, denoted \n there are a variety of other special ASCII characters which can be represented in C by escape sequences. Try out these sequences for yourself, and try to establish their effect: \r, \b, \t.




Document: Software Engineering 1: Course Notes

next VariablesData Types and Arrays
up Chapter 2 pp. 11-19
previous A Shady Character?



McMullin@ugmail.eeng.dcu.ie
Wed Apr 12 19:40:14 BST 1995