B, short for Programming Language B, is a procedural programming language created by Ken Thompson at Bell Labs in 1969. B was developed as a simplified version of BCPL, intended for system and operating system programming on early Unix systems. Historical implementations and documentation can be accessed at the B Language Historical Resources.
B exists to provide a compact, typeless language suitable for low-level programming tasks. Its design philosophy emphasizes simplicity, portability, and efficiency, allowing programmers to write concise system-level code while serving as a stepping stone toward the development of C. It abstracts some of the complexities of BCPL while maintaining procedural programming features.
B: Variables and Functions
Variables in B are typeless and declared implicitly, while reusable code blocks are defined as functions using funct (or implicitly by assignment in early implementations).
' declare variables
count = 10
name = "Alice"
' define a function
funct greet()
writef("Hello, %s\n", name)
end
greet()Typeless variables allow quick coding and flexibility, while functions enable modular, reusable code for procedural tasks.
B: Control Flow
Conditional and loop statements include if, else, for, while, and goto for explicit jumps.
if count > 0
writef("Count is positive\n")
else
writef("Count is zero or negative\n")
for i = 1; i <= 5; i = i + 1
writef("Iteration: %d\n", i)These constructs provide structured program flow while maintaining the minimal syntax characteristic of B, suitable for early Unix programming and system routines.
B: Arrays and Strings
B supports simple arrays as contiguous memory sequences and strings as character arrays, enabling basic data storage and manipulation.
' define an array
numbers[5] = {1,2,3,4,5}
' iterate over array
for i = 0; i < 5; i = i + 1
writef("Number %d: %d\n", i, numbers[i])Arrays and strings allow programmers to manage collections of data efficiently, despite the typeless nature of the language.
B: I/O
Input and output are handled with read and writef, supporting console interactions.
' simple I/O
writef("Enter your name: ")
read name
writef("Hello, %s!\n", name)This enables B programs to interact with users and process data in a straightforward, console-based environment.
B is primarily of historical interest, used to study early system programming and the evolution of procedural languages. It directly influenced C and indirectly shaped modern system-level languages, forming a foundation for Unix software development and compiler design.