B

Appeared in:
1969
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.b
Versions and implementations (Collapse all | Expand all):
Programming language

B is an interpreted programming language for mini-computers, a direct descendant of BCPL and ancestor of C.

B was developed at AT&T Bell Telephone Laboratories and published in 1969. The development was done mainly by Ken Thompson, with some assistance from Dennis Ritchie. Later Stephen C. Johnson improved the original implementation of the language and added the run-time library.

The main purpose of B was system development, not numeric computations. It was easier to use and more convenient than Assembler while producing almost as efficient code.

B is very similar to BCPL made shorter by removing any elements Thompson felt were unnecessary. User’s manual often compares it to Fortran, probably due to popularity of the latter and the built-in capabilities of interaction with Fortran programs.

The only data type in B is a word; most operators treat it as an integer, but some use it as a memory address to be dereferenced. The variables can also be treated as characters (based on their ASCII-codes), initialized with character constants etc. The language provides no floating-point arithmetic, but octal numbers (written with preceding 0) are rather important.

The programs consist of a set of user-defined functions; one of them, main, is called at the program start, and the program execution ends as soon as main is executed. Variables can be declared in two ways — auto (local within a function) and extrn (global, listed and initialized outside of functions). Variables can be scalar or vector (a vector is a one-dimensional array of fixed length). Strings are arrays of characters.

B provides a small set of libraries which include C-like IO functions, files and strings manipulation functions and a special function callf which can call a Fortran function.

B was used in early Unix-like systems, on mini-computers DEC PDP-7 and PDP-11, as well as on Honeywell mainframes operated by OS GCOS. Later is was replaced by C in Unix systems, and nowadays the interpreter of the language is available only on GCOS8.

Elements of syntax:

Non-nestable comments /* ... */
Case-sensitivity yes (keywords must be lowercase)
Variable assignment <varname> = <value>
Variable declaration auto <varname> or extrn <varname>
Variable declaration with assignment <varname> <value>
Block { ... }
Physical (shallow) equality ==
Physical (shallow) inequality !=
Deep equality < > <= >=
Function definition <name>(<arguments>) <statement>
Function call <name>(<arguments>)
Function call with no parameters <name>()
Sequence ;
If - then if (expression) statement
If - then - else if (expression) statement else statement2
Loop forever while (1) statement
While condition do while (expression) statement

Examples:

Hello, World!:

Example for versions H6070 B

An example from “A tutorial introduction to the language B” by Kernigan. Shows usage and initialization of global variables.

main( ) {
  extrn a, b, c;
  putchar(a);
  putchar(b);
  putchar(c);
  putchar('!*n');
}

a 'Hell';
b 'o, W';
c 'orld';