Line number

From Wikipedia, the free encyclopedia

In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and incrementing by 1 for each successive line.

In the C programming language the line number of a source code line is one greater than the number of new-line characters read or introduced up to that point[1].

Line numbers are a required element of syntax in some older programming languages such as BASIC. The primary reason for this is that most operating systems at the time lacked interactive text editors; since the programmer's interface was usually limited to a line editor, line numbers provided a mechanism by which specific lines in the source code could be referenced for editing, and by which the programmer could insert a new line at a specific point. Line numbers also provided a convenient means of distinguishing between code to be entered into the program and commands to be executed immediately when entered by the user (which do not have line numbers).

Largely due to the prevalence of interactive text editing in modern operating systems, line numbers are not a feature of most programming languages.

[edit] Line numbers and style

It was a matter of programming style, if not outright necessity, in these languages to leave gaps between successive line numbers—i.e., a programmer would use the sequence (10, 20, 30, ...) rather than (1, 2, 3, ...). This permitted the programmer to insert a line of code at a later time. For example, if a line of code between lines 20 and 30 was left out, the programmer might insert the forgotten line at line number 25. If no gaps were left in the numbering, the programmer would be required to renumber line 3 and all subsequent lines in order to insert the new line after line 2. Of course, if the programmer needed to insert more than nine additional lines, renumbering would be required even with the sparser numbering. Some BASICs had a RENUM command, which typically would go through the program (or a specified portion of it) reassigning line numbers in equal increments.

In a large program containing subroutines, each subroutine would usually start at a line number sufficiently large to leave room for expansion of the main program (and previous subroutines). For example, subroutines might begin at lines 10000, 20000, 30000, etc.

[edit] Line numbers and GOTOs

In "unstructured" programming languages such as BASIC and Fortran, line numbers were used to specify the targets of branching statements. For example:

10 IF X = 42 GOTO 40
20 X = X + 1
30 GOTO 10
40 PRINT "X is finally 42!"

GOTO-style branching is now widely considered by programmers to be poor programming style, as it tends to lead to the development of spaghetti code. (See Considered harmful, Structured programming.) Even in some later versions of BASIC that still mandated line numbers, the use of line number-controlled GOTOs was phased out whenever possible in favor of cleaner constructs such as the for loop and while loop.

Many modern languages (including C and C++) include a version of the GOTO statement; however, in these languages the target of a GOTO is specified by a textual label instead of a line number, and the use of GOTO is strongly discouraged by most programming style guides. The typically accepted use being when it is used to escape out of deep looping. For example:

while(1) {
  while (1) {
    if (done) {
      goto freedom;
    }
  }
}
freedom:

This is much cleaner than the alternative that follows, which needs to have a much more widely scoped `done` variable.

while(1) {
  if (done) {
    break;
  }
  while (1) {
    if (done) {
      break;
    }
  }
}

[edit] Line numbers and syntax errors

If a programmer introduces a syntax error into a program, the compiler (or interpreter) will inform the programmer that the attempt to compile (or execute) failed at the given line number. This simplifies the job of finding the error immensely for the programmer.

The use of line numbers to describe the location of errors remains standard in modern programming tools, even though line numbers are never required to be manually specified. It is a simple matter for a program to count the newlines in a source file and display an automatically generated line number as the location of the error. In IDEs such as Microsoft Visual Studio, in which the compiler is usually integrated with the text editor, the programmer can even double-click on an error and be taken directly to the line containing that error.

Languages