ArticleslgStudy

computer science

Bc (programming language)

Bc (programming language) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bc (programming language) rather than just read about it. In short: bc is an arbitrary-precision mathematical calculator program with an input language similar to C. It supports both interactive, command-line user-interface and script processing.

Key takeaways

  • Bc (programming language) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bc (programming language) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bc (programming language) from memory before moving on to harder problems.

Reference excerpt

bc is an arbitrary-precision mathematical calculator program with an input language similar to C. It supports both interactive, command-line user-interface and script processing.

Overview A typical interactive usage is typing the command bc on a Unix command prompt and entering a mathematical expression, such as (1 + 3) * 2, whereupon 8 will be output. While bc can work with arbitrary precision, it defaults to zero digits after the decimal point, so the expression 2/3 yields 0 (results are truncated, not rounded). This can surprise new bc users. The -l option to bc sets the default scale (digits after the decimal point) to 20 and adds several additional mathematical functions to the language.

History bc first appeared in Version 6 Unix in 1975. It was written by Lorinda Cherry of Bell Labs as a front end to dc, an arbitrary-precision calculator written by Robert Morris and Cherry. dc performed arbitrary-precision computations specified in reverse Polish notation. bc provided a conventional programming-language interface to the same capability via a simple compiler (a single yacc source file comprising a few hundred lines of code), which converted a C-like syntax into dc notation and piped the results through dc. In 1991, POSIX rigorously defined and standardized bc. Four implementations of this standard survive today: The first is the traditional Unix implementation, a front-end to dc, which survives in Unix and Plan 9 systems. The second is the free software GNU bc, first released in 1991 by Philip A. Nelson. The GNU implementation has numerous extensions beyond the POSIX standard and is no longer a front-end to dc (it is a bytecode interpreter). The third is a re-implementation by OpenBSD in 2003. The fourth is an independent implementation by Gavin Howard that is included in Android (operating system), FreeBSD as of 13.3-RELEASE, and macOS as of 13.0.

Etymology The original UNIX version 6 manual does not explain what “bc” stands for. Various sources over the years referred to it as “basic calculator”, “bench calculator” and “binary calculator”.

Implementations

POSIX bc The POSIX standardized bc language is traditionally written as a program in the dc programming language to provide a higher level of access to the features of the dc language without the complexities of dc's terse syntax. In this form, the bc language contains single-letter variable, array and function names and most standard arithmetic operators, as well as the familiar control-flow constructs (if(cond)..., while(cond)... and for(init;cond;inc)...) from C. Unlike C, an if clause may not be followed by an else. Functions are defined using a define keyword, and values are returned from them using a return followed by the return value in parentheses. The auto keyword (optional in C) is used to declare a variable as local to a function. All numbers and variable contents are arbitrary-precision numbers whose precision (in decimal places) is determined by the global scale variable. The numeric base of input (in interactive mode), output and program constants may be specified by setting the reserved ibase (input base) and obase (output base) variables. Output is generated by deliberately not assigning the result of a calculation to a variable. Comments may be added to bc code by use of the C /* and */ (start and end comment) symbols.

Mathematical operators

Exactly as C The following POSIX bc operators behave exactly like their C counterparts:

+ - * / += -= *= /= ++ -- < > == != <= >= ( ) [ ] { }

Similar to C The modulus operators, % and %= behave exactly like their C counterparts only when the global scale variable is set to 0, i.e. all calculations are integer-only. Otherwise the computation is done with the appropriate scale. a%b is defined as a-(a/b)*b. Examples:

Conflicting with C The operators

^ ^=

superficially resemble the C bitwise exclusive-or operators, but are in fact the bc integer exponentiation operators. Of particular note, the use of the ^ operator with negative numbers does not follow the C operator precedence. -2^2 gives the answer of 4 under bc rather than −4.

"Missing" operators relative to C The bitwise, Boolean and conditional operators:

& | ^ && || &= |= ^= &&= ||= << >> <<= >>= ?:

are not available in POSIX bc.

Built-in functions The sqrt() function for calculating square roots is POSIX bc's only built-in mathematical function. Other functions are available in an external standard library. The scale() function for determining the precision (as with the scale variable) of its argument and the length() function for determining the number of significant decimal digits in its argument are also built-in.

Standard library functions bc's standard math library (defined with the -l option) contains functions for calculating sine, cosine, arctangent, natural logarithm, the exponential function and the two parameter Bessel function J. Most standard mathematical functions (including the other inverse trigonometric functions) can be constructed using these. See external links for implementations of many other functions.

The -l option changes the scale to 20, so things such as modulo may work unexpectedly. For example, writing bc -l and then the command print 3%2 outputs 0. But writing scale=0 after bc -l and then the command print 3%2 will output 1.

Plan 9 bc Plan 9 bc is identical to POSIX bc but for an additional print statement.

GNU bc GNU bc derives from the POSIX standard and includes many extensions. It is entirely separate from dc-based implementations of the POSIX standard and is instead written in C. Nevertheless, it is fully backwards compatible as all POSIX bc programs will run unmodified as GNU bc programs. GNU bc variables, arrays and function names may contain more than one character, some more operators have been included from C, and notably, an if clause may be followed by an else. Output is achieved either by deliberately not assigning a result of a calculation to a variable (the POSIX way) or by using the added print statement. Furthermore, a read statement allows the interactive input of a number into a running calculation. In addition to C-style comments, a # character will cause everything after it until the next new-line to be ignored. The value of the last calculation is always stored within the additional built-in last variable.

Extra operators The following logical operators are additional to those in POSIX bc:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Bc (programming language)

Start with the simplest possible case. Write down what Bc (programming language) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bc (programming language) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bc (programming language) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bc (programming language)

In research
Bc (programming language) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bc (programming language) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bc (programming language) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Cross-platform free software, Free mathematics software, Numerical programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for Bc (programming language) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bc (programming language)” →

How to study Bc (programming language) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bc (programming language) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bc (programming language) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bc (programming language) in simple terms?

bc is an arbitrary-precision mathematical calculator program with an input language similar to C. It supports both interactive, command-line user-interface and script processing.

Why does Bc (programming language) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bc (programming language)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bc (programming language).

Tags

  • Cross-platform free software
  • Free mathematics software
  • Numerical programming languages
  • Plan 9 commands
  • Programming languages standardized by POSIX
  • Software calculators

Keep exploring