

bc understands the following command line options:
Common uses for bc are:
The actual limit on the number of digits that can be handled depends
on the amount of storage available on the machine, so manipulation
of numbers with many hundreds of digits is possible.
when evaluated, responds immediately with the line:
428571
Other operators can also be used. The complete list includes:
+  * / % ^
They indicate addition, subtraction, multiplication, division, modulo (remaindering), and exponentiation, respectively. Division of integers produces an integer result truncated toward zero. Division by zero produces an error message.
Any term in an expression can be prefixed with a minus sign to
indicate that it is to be negated (this is the ``unary'' minus
sign). For example, the expression:
7 + 3
is interpreted to mean that 3 is to be added to 7.
More complex expressions with several operators and with parentheses are interpreted just as in FORTRAN, with exponentiation (^) performed first, then multiplication (*), division (/), modulo (%), and finally, addition (+), and subtraction (). The contents of parentheses are evaluated before expressions outside the parentheses. All of the above operations are performed from left to right, except exponentiation, which is performed from right to left.
Thus the following two expressions:
a^b^c and a^(b^c)
are equivalent, as are the two expressions:
a*b*c and (a*b)*c
bc shares with FORTRAN and C the convention that a/b*c is equivalent to (a/b)*c.
Internal storage registers to hold numbers have single lowercase
letter names. The value of an expression can be assigned to a
register in the usual way, thus the statement:
x = x + 3
has the effect of increasing by 3 the value of the contents of the register named x. When, as in this case, the outermost operator is the assignment operator (=), then the assignment is performed but the result is not printed. There are 26 available named storage registers, one for each letter of the alphabet.
There is also a builtin square root function whose result is
truncated to an integer (see also ``Scaling quantities'', below).
For example, the lines:
x = sqrt(191)
x
produce the printed result:
13
produce the output line:
9and set up bc to do octal to decimal conversions. Beware of trying to change the input base back to decimal by entering:
Because the number 10 is interpreted as octal, this statement has no effect. For those who deal in hexadecimal notation, the uppercase characters AF are permitted in numbers (no matter what base is in effect) and are interpreted as digits having values 1015, respectively. These characters must be uppercase and not lowercase.
The statement:
ibase = A
changes back to decimal input base no matter what the current input base is. Negative and large positive input bases are permitted; however no mechanism has been provided for the input of arbitrary numbers in bases less than 1 and greater than 16.
obase is used as the base for output numbers. The value of
obase is initially set to a decimal 10.
The lines:
obase = 16
1000
produce the output line:
3E8This is interpreted as a threedigit hexadecimal number. Very large output bases are permitted. For example, large numbers can be output in groups of five digits by setting obase to 100000. Even strange output bases, such as negative bases, and 1 and 0, are handled correctly.
Very large numbers are split across lines with seventy characters per line. A split line that continues on the next line ends with a backslash (\). Decimal output conversion is fast, but output of very large numbers (that is, more than 100 digits) with other bases is rather slow.
The values of ibase and obase do not affect the course of internal computation or the evaluation of expressions; they only affect input and output conversion.
When two scaled numbers are combined by means of one of the arithmetic operations, the result has a scale determined by the following rules:
The contents of scale must be no greater than 99 and no less than 0. It is initially set to 0.
The internal quantities scale, ibase, and
base can be used in expressions just like other
variables. The line:
scale = scale + 1
increases the value of scale by 1, and the line:
scale
causes the current value of scale to be printed.
The value of scale retains its meaning as a number of decimal digits to be retained in internal computation even when ibase or obase are not equal to 10. The internal computations (which are still conducted in decimal, regardless of the bases) are performed to the specified number of decimal digits, never hexadecimal, octal or any other kind of digits.
The line:
define a(x){
begins the definition of a function with one argument. This line must be followed by one or more statements, which make up the body of the function, ending with a right brace (}). Return of control from a function occurs when a return statement is executed or when the end of the function is reached.
The return statement can take either of the two forms:
return
return(x)
In the first case, the returned value of the function is 0; in the second, it is the value of the expression in parentheses.
Variables used in functions can be declared as automatic by a
statement of the form:
auto x,y,z
There can be only one auto statement in a function and it
must be the first statement in the definition. These automatic
variables are allocated space and initialized to zero on entry to
the function and thrown away on return. The values of any variables
with the same names outside the function are not
disturbed. Functions can be called recursively and the automatic
variables at each call level are protected. The parameters named in
a function definition are treated in the same way as the automatic
variables of that function, with the single exception that they are
given a value on entry to the function.
An example of a function definition follows:
define a(x,y){ auto z z = x*y return(z) }The value of this function, when called, is the product of its two arguments.
A function is called by the appearance of its name, followed by a string of arguments enclosed in parentheses and separated by commas. The result is unpredictable if the wrong number of arguments is used.
If the function do_something is defined as shown above,
then the line:
do_something(7,3.14)
prints the result:
21.98Similarly, the line:
causes the value of x to become 60.
Functions without arguments can still perform useful operations or
return useful results. Such functions are defined and called using
parentheses with nothing between them. For example:
b ()
calls the function named b.
Subscripted variables can be freely used in expressions, function calls and return statements.
An array name can be used as an argument to a function, as in:
f(a[])
Array names can also be declared as automatic in a function definition with the use of empty brackets:
define f(a[ ]) auto a[ ]When an array name is so used, the entire contents of the array are copied for the use of the function, then thrown away on exit from the function. Array names that refer to whole arrays cannot be used in any other context.
A relation in one of the control statements is an expression of the
form:
expression1 relop expression2
where the two expressions are related by one of the six relational
operators:
< > <= >= == !=
Note that a double equal sign (==) stands for ``equal to'' and an exclamationequal sign (!=) stands for ``not equal to''. The meaning of the remaining relational operators is their normal arithmetic and logical meaning.
Beware of using a single equal sign (=) instead of the double equal sign (==) in a relational. Both of these symbols are legal, so no diagnostic message is produced. However, the operation will not perform the intended comparison.
The if statement causes execution of its range if and only if the relation is true. Then control passes to the next statement in the sequence.
The while statement causes repeated execution of its range as long as the relation is true. The relation is tested before each execution of its range and if the relation is false, control passes to the next statement beyond the range of the while statement.
The for statement begins by executing
expression1. Then the relation is tested and, if true, the
statements in the range of the for statement are
executed. Then expression2 is executed. The relation is
tested, and so on. The typical use of the for statement
is for a controlled iteration, as in the statement:
for (i=1; i<=10; i=i+1)
which will print the integers 1 to 10.
The following are some examples of the use of the control statements:
define f(n){ auto i, x x=1 for(i=1; i<=n; i=i+1) x=x*i return(x) }The line:
f(a)prints a factorial if a is a positive integer.
The following is the definition of a function that computes values of the binomial coefficient (m and n are assumed to be positive integers):
define b(n,m){ auto x, j x=1 for(j=1; j<=m; j=j+1) x=x*(nj+1)/j return(x) }
The following function computes values of the exponential function by summing the appropriate series without regard to possible truncation errors:
scale = 20 define e(x){ auto a, b, c, d, n a = 1 b = 1 c = 1 d = 0 n = 1 while(1==1) { a = a*x b = b*n c = c + a/b n = n + 1 if(c==d) return(c) d = c } }
not only makes the indicated assignment, but also prints the
resulting value. The following is an example of a use of the value
of an assignment statement even when it is not placed in
parentheses:
x = a[i=i+1]
This causes a value to be assigned to x and also increments i before it is used as a subscript.
Construction  Equivalent  Notes 

x=y=z  x =(y=z)  
x += y  x = x+y  
x = y  x = xy  
x = y  x = xy  
x /= y  x = x/y  
x %= y  x = x%y  
x ^= y  x = x^y  
x =+ y  x = x+y  obsolete 
x = y  x = xy  obsolete 
x = y  x = xy  obsolete 
x =/ y  x = x/y  obsolete 
x =% y  x = x%y  obsolete 
x =^ y  x = x^y  obsolete 
x++  (x=x+1)1  result is value of x before incrementing 
x  (x=x1)+1  result is value of x before decrementing 
++x  x = x+1  result is value of x after incrementing 
x  x = x1  result is value of x after decrementing 
++++ Construction  Equivalent  Notes  ++++ x=y=z  x =(y=z)   ++++ x += y  x = x+y   ++++ x = y  x = xy   ++++ x *= y  x = x*y   ++++ x /= y  x = x/y   ++++ x %= y  x = x%y   ++++ x ^= y  x = x^y   ++++ x =+ y  x = x+y  obsolete  ++++ x = y  x = xy  obsolete  ++++ x =* y  x = x*y  obsolete  ++++ x =/ y  x = x/y  obsolete  ++++ x =% y  x = x%y  obsolete  ++++ x =^ y  x = x^y  obsolete  ++++ x++  (x=x+1)1  result is value of x before incrementing  ++++ x  (x=x1)+1  result is value of x before decrementing  ++++ ++x  x = x+1  result is value of x after incrementing  ++++ x  x = x1  result is value of x after decrementing  ++++Note that some of the constructions above are marked obsolete. Although they are still supported, use of the alternative constructions is recommended.
If one of these constructions is used inadvertently, it is possible for something legal but unexpected to happen. There is a real difference between x=y and x= y. The first replaces x by xy and the second by y.
when bc is invoked. This command loads the library functions sine, cosine, arctangent, natural logarithm, exponential, and Bessel functions of integer order. These are named s, u, a, l, e, and j(n,x) respectively. This library sets scale to 20 by default.
bc will read and execute the named file or files before accepting commands from the keyboard. In this way, user programs and function definitions can be loaded.
Some of these constructions are casesensitive.
There are several types of expressions:
identifiers
Simple identifiers are named expressions. They have an initial value
of 0.
arrayname[expression]
Array elements are named expressions. They have an initial value
of 0.
scale, ibase and obase
The internal registers scale, ibase, and
obase are all named expressions. scale is the
number of digits after the decimal point to be retained in
arithmetic operations and has an initial value of 0. ibase
and obase are the input and output number radixes
respectively. Both ibase and obase have initial
values of 10.
A whole array passed as an argument is specified by the array name followed by empty square brackets. All function arguments are passed by value. As a result, changes made to the formal parameters have no effect on the actual arguments. If the function terminates by executing a return statement, the value of the function is the value of the expression in the parentheses of the return statement, or 0 if no expression is provided or if there is no return statement. Three builtin functions are listed below:
These operators are listed below:
expr < expr
expr > expr
expr <= expr
expr >= expr
expr == expr
expr != expr
All identifiers, global and local, have initial values of 0. Identifiers declared as auto are allocated on entry to the function and released on returning from the function. They, therefore, do not retain values between function calls. Note that auto arrays are specified by the array namer, followed by empty square brackets.
Automatic variables in bc do not work the same way as in C. On entry to a function, the old values of the names that appear as parameters and as automatic variables are pushed onto a stack. Until return is made from the function, reference to these names is only to the new values.
All three expressions must be present. Syntax of the for
statement is:
for (expression; relation; expression) statement
quit is interpreted when read, not when executed.
Trigonometric values should be given in radians.
ISO/IEC DIS 99452:1992, Information technology Portable Operating System Interface (POSIX) Part 2: Shell and Utilities (IEEE Std 1003.21992);
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.