The B Programming Language 

 

Data Objects

Types and Declaration
In a language like FORTRAN, variables are always of one data type or another and you can only do certain operations with certain types of variables. For example, FORTRAN will not let you add a floating-point number to a character, or perform logical operations on integers. 

B, on the other hand, is a typeless language. The compiler does not keep track of whether variables refer to integers, characters, octal numbers, and so on; that is left up to you. You can subtract the letter 'a' from 1.5 without getting an error in B...of course, the answer will not mean much, but you can do the subtraction. 

Declarations in B specify storage class of variables. Such declarations also, in some circumstances, specify initialization.

There are three storage classes in B. Automatic storage is allocated for each function invocation. External storage is allocated before execution and is available to any and all functions. Internal storage is local to a function and is available only to that function, but is available to all invocations of that function. 

Arithmetic, Octal Numbers
All arithmetic in B is integer, unless special functions are written. There is no equivalent of the Fortran IJKLMN convention, no floating point, no data types, no type conversions, and no type checking. Users of double-precision complex will have to fend for themselves. 

The arithmetic operators are the usual '+', '-', '*', and '/' (integer division). In addition, we have the remainder operator '%':

                                     x = a%b

sets x to the remainder after a is divided by b (both of which should be positive).

Since B is often used for system programming and bit-manipulation, octal numbers are an important part of the language. The syntax of B says that any number that begins with 0 is an octal number (and hence can't have any 8's or 9's in it). Thus 0777 is an octal constant, with decimal value 511. 

Characters; putchar; Newline
    main( ) {

                  auto a;

                  a= 'hi!';

                  putchar(a);

                 putchar('*n' );

                }

 

This program prints "hi!" on the terminal it illustrates the use of character constants and variables. A "character" is one to four ASCII characters, enclosed in single quotes. The characters are stored in a single machine word, right justified and zero-filled. We used a variable to hold "hi!” but could have used a constant directly as well.

The sequence "*n" is B Jargon for "newline character", which, when printed, skips the terminal to the beginning of the next line. No output occurs until putchar encounters a "*n" or the program terminates.

Since B is a typeless language, arithmetic on characters is quite legal, and even makes sense sometimes:

c = c+'A'-'a';

converts a single character stored in c to upper case (making use of the fact that corresponding ASCII letters are a fixed distance apart).                                  

Assignment and Type Conversion
There are 16 assignment operators in B. All have the form
                                   

lvalue op rvalue

The assignment operator = merely evaluates the rvalue and stores the result in the lvalue. The assignment operators =|, =&, ===, =|=, =<, =<=, =>, =>=, =<<, =>>, =+, =-, =%, =*, and =/ perform a binary operation between the rvalue stored in the assignment's lvalue and the assignment's rvalue. The result is then stored in the lvalue. The expression x=*10 is identical to x=x*10.

Vectors
A vector is a set of consecutive words, somewhat like a one-dimensional array in Fortran. The declaration auto v[10]; allocates 11 consecutive words: v[0], v[1], ..., v[10] for v. Reference to the i-th element is made by v[i] ; the [ ] brackets indicate subscripting. 

External Vectors
Vectors can of course be external variables rather than auto. We declare v external within the function (don't mention a size) by extrn v; and then outside all functions write v[10];.

External vectors may be initialized just as external simple variables: 

v[10]  'hi!', 1, 2, 3, 0777;

sets v[0] to the character constant 'hi!', and v[1] through v[4] to various numbers. v[5] through v[10] are not initialized. 

Addressing
To understand all the ramifications of vectors, a digression into addressing is necessary. The actual implementation of a vector consists of a word v which contains in its right half the address of the 0-th word of the vector; i.e., v is really a pointer to the actual vector.

This implementation can lead to a fine trap for the unwary. If we say

 auto u[10], v[10];

  ---

  u = v;

  v[0] = ...

  v[1] = ...

  ---

The unsuspecting might believe that u[0] and u[1] contain the old values of v[0] and v[1]; in fact, since u is just a pointer to v[0] , u actually refers to the new content. The statement "u=v" does not cause any copy of information into the elements of u; they may indeed be lost because u no longer points to them.

Strings
A string in B is in essence a vector of characters. It is written: 

 "this is a string"