Line number

Line number

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. [cite web|url=http://c0x.coding-guidelines.com/6.10.4.html|title=6.10.4 Line control|date=2008-01-30|accessdate=2008-07-03|author=]

Line numbers were a required element of syntax in some older programming languages such as GW-BASIC.cite web|url=http://support.microsoft.com/kb/73084|title=Differences Between GW-BASIC and QBasic|date=2003-05-12|accessdate=2008-06-28|author=] 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.

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.

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 4020 X = X + 130 GOTO 1040 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 line 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; }

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.

ee also

*Line label

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • line number — eilutės numeris statusas T sritis informatika apibrėžtis Skaitinė eilutės ↑žymė dokumente. Eilutes gali numeruoti daugelis ↑rašyklių. Eilučių numeriai nėra dokumento dalis (jų nebūna dokumento faile), jie nespausdinami, tačiau gali būti rodomi… …   Enciklopedinis kompiuterijos žodynas

  • Line number — Номер строки (на корректурном отпечатке) …   Краткий толковый словарь по полиграфии

  • television line number — televizijos eilučių skaičius statusas T sritis radioelektronika atitikmenys: angl. television line number vok. Fernsehzeilenzahl, f rus. число строк телевизионного растра, n pranc. nombre des lignes en télévision, m …   Radioelektronikos terminų žodynas

  • raster line number — kadro eilučių skaičius statusas T sritis radioelektronika atitikmenys: angl. raster line number vok. Rasterlinienzone, f rus. число строк в кадре, n pranc. nombre des lignes de trame, m …   Radioelektronikos terminų žodynas

  • Line 1 (Budapest Metro) — Line number Line 1 ( Yellow metro ) Technical Line length 4.4 km Track gauge 1435 mm …   Wikipedia

  • Trolly Line Number 9 Trail — The Historical Ellicott City/Baltimore Trolly Line #9 Trail is a convert|1.25|mi|km|1|sing=on trail in western Baltimore County, Maryland. It begins at the west end of Edmonson Avenue and extends from Catonsville through Oella to Main Street,… …   Wikipedia

  • collection coordination facility line number — An arbitrary number assigned to contingency intelligence reconnaissance objectives by the Defense Intelligence Agency collection coordination facility to facilitate all source collection …   Military dictionary

  • Number line — For other uses, see Number line (disambiguation). In basic mathematics, a number line is a picture of a straight line on which every point is assumed to correspond to a real number and every real number to a point.[1] Often the integers are shown …   Wikipedia

  • Line 7 White (Montreal Metro) — Montréal Métro Line 7, often referenced as the White Line, was a planned underground subway line for Montreal Metro.Proposed for the first time by the Bureau des Transports de Montréal (BTM) in September 1983, the original project for a new north …   Wikipedia

  • Line (text file) — In computing, a line is a unit of organization for text files. A line consists of a sequence of zero or more characters. Depending on the file system being used the number of characters on a line may be fixed (e.g., at 80 characters on some IBM… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”