Skip to content

The B Programming Language

I drafted this post back in December. I have since been distracted with “real work”, so I’ve never gotten back to it. At this point, I can’t remember what I was going to change or add to it, so I’ll just post it as-is.

I’ve been exploring the B programming language. I’ve had a casual interest in this for years and have only recently dug into it a little deeper. I created the following comparison to C for myself and anyone else who may be interested.

What’s different compared to C?

  • No preprocessor or associated directives (no symbolic constants or macros).
  • No explicit types or typedef keyword; all variables have “word” type, similar to the int type in C. B does not have array types as in C, just pointers to vectors.
  • No type qualifiers or const or volatile keywords.
  • No floating point support.
  • auto keyword used to declare automatic (local) variables.
  • extrn keyword instead of extern keyword.
  • No static keyword in earliest versions (though Dennis Ritchie’s C History paper seems to indicate that it may have been added later).
  • No sizeof operator.
  • No function prototypes; must use extrn to declare functions before use.
  • Identifiers are restricted to 7 characters and may contain the backspace character.
  • Variable initializers do not include the equals sign. For example: auto x 1 declares an automatic variable x with the initial value 1.
  • No hexadecimal constants.
  • No structs, unions, or associated operators (. and ->).
  • No enums.
  • No do or for loop constructs.
  • No continue, or default statements.
  • No comma operator.
  • No logical operators (&& and ||); must use bit-wise expressions.
  • Assignment operators have the form =op instead of op= (as in early C prior to 1976).
  • Relational assignment operators not found in C: ===, =!=, =<, =<=, =>, =>=.
  • Thompson’s original manual did not include bit-wise exclusive OR operator (^), bit-wise not operator (~), or break statement (MH-TSS reference includes these).
  • Escape character is * instead of \. For example, *n is the newline character.
  • Additional escape sequences:
    • *(  {
    • *)  }
    • *e  EOT
  • No equivalents to \a, \b, \f, \r, \v, \?, or octal/hexadecimal escape sequences.
  • B strings are terminated with *e (ASCII EOT) rather than *0 (ASCII NUL).
  • Controlling expressions for switch statements do not require parentheses.
  • Parentheses are required for return statements when a value is returned. For example: return (x)
  • Compound statements consisting of only a single statement preceded by variable declarations and/or label definitions need not be surrounded by braces in function definitions or if/while/switch constructs.

    Example 1: Read a character from stdin if cond is true.

    if (cond)
      auto unused;
      extrn read;
      read(0, &unused, 1);
    

    Example 2: Definition for a function g, which returns its parameter, f.

    g(f)
      return (f);

What’s similar?

  • Expression structure, operators, and operator precedence, except as mentioned above.
  • if/else, goto, switch, while, and return statements, aside from the differences mentioned above.
  • Everything else not mentioned above.

References:

  • Users’ Reference to B, Ken Thompson
  • The Programming Language B, S. C. Johnson & B. W. Kernighan, Technical Report CS TR 8, Bell Labs (January 1973)
  • The Development of the C Language, Dennis M. Ritchie.

No related posts.

Post a Comment

Your email is never published nor shared.

SEO Powered by Platinum SEO from Techblissonline