BC(1) | General Commands Manual | BC(1) |

`bc`

—
arbitrary-precision arithmetic language and
calculator

`bc` |
[`-cl` `-e`
expressionfile ... |

`bc`

is an interactive processor for a language
which resembles C but provides unlimited precision arithmetic. It takes input
from any expressions on the command line and any files given, then reads the
standard input.
Options available:
`-c`

`bc`

is actually a preprocessor for dc(1), which it invokes automatically, unless the`-c`

(compile only) option is present. In this case the generated dc(1) instructions are sent to the standard output, instead of being interpreted by a running dc(1) process.`-e`

`expression`- Evaluate
`expression`. If multiple`-e`

options are specified, they are processed in the order given, separated by newlines. `-l`

- Include an arbitrary precision math library. The definitions in the library are available to command line expressions and are documented below.

`bc`

programs is as follows:
‘L’ means letter a-z; ‘E’ means expression;
‘S’ means statement. As a non-portable extension, it is possible
to use long names in addition to single letter names. A long name is a
sequence starting with a lowercase letter followed by any number of lowercase
letters and digits. The underscore character (‘_’) counts as a
letter.
Comments
are enclosed in /* and */ are enclosed in # and the next newline

simple variables: L array elements: L [ E ] The words `ibase', `obase', and `scale' The word `last' or a single dot

arbitrarily long numbers with optional sign and decimal point ( E ) sqrt ( E ) length ( E ) number of significant decimal digits scale ( E ) number of digits right of decimal point L ( E , ... , E )

Operator |
Associativity |
Description |

++ -- | none | increment, decrement |

- | none | unary minus |

^ | right | power |

* / % | left | multiply, divide, modulus |

+ - | left | plus, minus |

= += -= *= /= %= ^= | right | assignment |

== <= >= != < > | none | relational |

! | none | boolean not |

&& | left | boolean and |

|| | left | boolean or |

- The relational operators may appear in any expression. The IEEE Std 1003.1-2008 (“POSIX.1”) standard only allows them in the conditional expression of an ‘if’, ‘while’ or ‘for’ statement.
- The relational operators have a lower precedence than the assignment
operators. This has the consequence that the expression
**a = b < c**is interpreted as**(a = b) < c**, which is probably not what the programmer intended. - In contrast with the C language, the relational operators all have the
same precedence, and are non-associative. The expression
**a < b < c**will produce a syntax error. - The boolean operators (!, && and ||) are non-portable extensions.
- The boolean not (!) operator has much lower precedence than the same
operator in the C language. This has the consequence that the expression
**!a < b**is interpreted as**!(a < b)**. Prudent programmers use parentheses when writing expressions involving boolean operators.

E { S ; ... ; S } if ( E ) S if ( E ) S else S while ( E ) S for ( E ; E ; E ) S null statement break continue quit a string of characters, enclosed in double quotes print E ,..., E

define L ( L ,..., L ) { auto L, ... , L S; ... S return ( E ) }

return return () return E

`-l`

flag on the command line:
- s(x)
- sine
- c(x)
- cosine
- e(x)
- exponential
- l(x)
- log
- a(x)
- arctangent
- j(n,x)
- Bessel function

scale = 20 define e(x){ auto a, b, c, i, s a = 1 b = 1 s = 1 for(i=1; 1==1; i++){ a = a*x b = b*i c = a/b if(c == 0) return(s) s = s+c } }

`for(i=1; i<=10; i++) e(i)`

$ bc -l -e 'scale = 500; 4 * a(1)' -e quit

`bc`

supports interactive command line editing,
via the editline(3) library.
It is enabled by default if input is from a tty. Previous lines can be
recalled and edited with the arrow keys, and other GNU Emacs-style editing
keys may be used as well.
The editline(3) library is
configured with a .editrc file - refer to
editrc(5) for more information.
- /usr/share/misc/bc.library
- math library, read when the
`-l`

option is specified on the command line.

`bc`

utility is compliant with the
IEEE Std 1003.1-2008
(“POSIX.1”) specification.
The flags [`-ce`

`bc`

command first appeared in
Version 6 AT&T UNIX. A complete rewrite of
the `bc`

command first appeared in
OpenBSD 3.5.
`bc`

command was
written by Robert Morris and
Lorinda Cherry. The current version of the
`bc`

utility was written by
Otto Moerbeek.
`quit`

’ statement is interpreted when
read, not when executed.
Some non-portable extensions, as found in the GNU version of the
`bc`

utility are not implemented (yet).October 9, 2017 | OpenBSD-current |