BC(1)

NAME
bc - arbitrary-precision arithmetic language

SYNOPSIS
bc [-p dc] [-cdls] [file ...]

DESCRIPTION
bc is an arbitrary-precision arithmetic language with syntax similar to C. bc reads each file in sequence and compiles the bc code into dc code, which is then executed by spawning dc(1) as a subprocess. After all the files are loaded and executed then it reads from stdin.

OPTIONS

-p dc
    Dc program spawned to run the translated code generated by bc. By default, it is dc.

-c  Compile only mode. Generate dc code without spawning a dc subprocess. The compiled dc code is written to stdout.

-d  Debug mode. Enable yacc parser debugging output.

-l  Load the mathematical library that is loaded before any file from the command line.

-s  Enable the extended mode. In this mode, the automatic printing of expression results is suppressed, and only explicit print statements produce output. Names of variables and functions can be longer than 1 character, but still constrained to lower case latin characters. The output of bc in this mode is suitable only for the suckless dc version.

LANGUAGE

  Comments
Comments are enclosed in ‘/*’ and ‘*/’.

  Numbers
Numbers may contain digits 0-9 and, when the input base is greater than 10, letters A-F as hexadecimal digits. Numbers may include a decimal point.

  Variables
Variables are single lowercase letters ‘a’ through ‘z’. Variables hold arbitrary-precision numbers and are automatically initialized to 0. The special variable . holds the value of the last expression calculated, useful to avoid retyping long numbers.

  Arrays
Array elements are referenced as name[expr]. Arrays are single lowercase letters ‘a’ through ‘z’. Array indices may be any expression. Arrays are automatically initialized with all elements set to 0.

  Special Variables

scale
    The number of digits after the decimal point in results. Default is 0. Affects division, modulus, power, and square root operations.

ibase
    The input number base. Must be between 2 and 16 inclusive. Default is 10.

obase
    The output number base. Must be at least 2. Default is 10.

  Operators
Arithmetic operators in order of precedence (highest to lowest):

^ ^=
    Exponentiation (right associative). The exponent is truncated to an integer.

* / % *= /= %=
    Multiplication, division, modulus.

+ - += -=
    Addition, subtraction.

++ --
    Increment and decrement (prefix or postfix).

=   Assignment.

  Built-in Functions

sqrt(expr)
    Square root of expr.

length(expr)
    Number of significant decimal digits in expr.

scale(expr)
    Number of digits after the decimal point in expr.

  Expressions

Expressions are combinations of operators, function calls, numbers and variables following the normal arithmetic rules. Parenthesis can be used to modify the precedence of operators.

  Relational

Relational expressions are composed by expressions combined using relational operators. They only can be used in the context of a if, while or for statements.

Relational operators:

==  Equal to.

!=  Not equal to.

<   Less than.

<=  Less than or equal to.

>   Greater than.

>=  Greater than or equal to.

  Statements

expr
    A expression in a single line, or separated by the character file ...; is a statement. If the expression is not an assignment expression then the result value is printed to stdout unless the option -s is used.

print expr
    Print the value of expr followed by a newline.

print "string"
    Print string without a newline.

print "string", expr
    Print string without a newline, then print expr with a newline.

"string"
    A string by itself is printed without a newline.

if (rel) stat
    Execute stat if rel is non-zero.

while (rel) stat
    Execute stat repeatedly while rel is non-zero.

for (expr1; rel; expr2) stat
    Equivalent to expr1; while (rel) { stat; expr2 }. The 3 components of the for loop are required.

break
    Exit from the innermost while or for loop.

quit
    Exit bc when the statement is read, independently of being under an if statement or in a function.

return
    Return 0 from a function.

return ()
    Return 0 from a function.

return (expr)
    Return expr from a function.

{ stat-list }
    Group statements.

  Function Definitions
Functions are defined as:

    define name(parameters) {
    	auto local-variables
    	statements
    }

Notice that the opening brace must be in the same line than the define statement. Function names are single lowercase letters. Parameters and local variables are comma-separated lists of simple variables or arrays (denoted by array[]). The auto statement is optional and must appear first in the function body if present.

Functions are called as name(arguments). Array arguments must be passed as array[]. Functions return a value using the return statement. If no return is executed, the function returns 0.

  Namespaces
The variables, arrays and function names are composed of only one letter, but they are independent namespaces. The same letter can be used for a variable, array or function without problems.

LIBRARY
When invoked with the -l option, bc preloads a mathematical library that defines the following functions:

e(x)
    Exponential function. Returns e raised to the power x.

l(x)
    Natural logarithm of x.

s(x)
    Sine of x where x is in radians.

c(x)
    Cosine of x where x is in radians.

a(x)
    Arctangent of x. Returns the value in radians.

j(n,x)
    Bessel function of order n of x.

The library also sets scale to 20 by default.

IMPLEMENTATION NOTES
This implementation of bc is a compiler that translates bc language into dc commands.

Variables are mapped to dc registers. Functions are compiled to dc macros stored in registers identified by function name. Control flow statements are implemented using dc's conditional macro execution commands.

The maximum nesting level for control structures and function calls is 32.

SEE ALSO
dc(1) L. L. Cherry, R. H. Morris, BC — An Arbitrary Precision Desk-Calculator Language.

STANDARDS
POSIX.1-2013. The print statement and the -d and -s flags are extensions to the POSIX specification.