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
typedefkeyword; all variables have “word” type, similar to theinttype in C. B does not have array types as in C, just pointers to vectors. - No type qualifiers or
constorvolatilekeywords. - No floating point support.
autokeyword used to declare automatic (local) variables.extrnkeyword instead ofexternkeyword.- No
statickeyword in earliest versions (though Dennis Ritchie’s C History paper seems to indicate that it may have been added later). - No
sizeofoperator. - No function prototypes; must use
extrnto 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 1declares an automatic variable x with the initial value1. - No hexadecimal constants.
- No
structs,unions, or associated operators (.and->). - No
enums. - No
doorforloop constructs. - No
continue, ordefaultstatements. - 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 (~), orbreakstatement (MH-TSS reference includes these). - Escape character is
*instead of\. For example,*nis the newline character. - Additional escape sequences:
*({*)}*eEOT
- 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
switchstatements do not require parentheses. - Parentheses are required for
returnstatements 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/switchconstructs.Example 1: Read a character from
stdinifcondis 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, andreturnstatements, 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