1bc(1)                       General Commands Manual                      bc(1)
2
3
4

NAME

6       bc - An arbitrary precision calculator language
7

SYNTAX

9       bc [ -hlwsqv ] [long-options] [  file ... ]
10

VERSION

12       This man page documents GNU bc version 1.06.
13

DESCRIPTION

15       bc  is a language that supports arbitrary precision numbers with inter‐
16       active execution of statements.  There are  some  similarities  in  the
17       syntax  to  the  C  programming  language.   A standard math library is
18       available by command line option.  If requested, the  math  library  is
19       defined before processing any files.  bc starts by processing code from
20       all the files listed on the command line in the  order  listed.   After
21       all  files  have been processed, bc reads from the standard input.  All
22       code is executed as it is read.  (If a file contains a command to  halt
23       the processor, bc will never read from the standard input.)
24
25       This  version  of  bc contains several extensions beyond traditional bc
26       implementations and the POSIX draft standard.  Command line options can
27       cause these extensions to print a warning or to be rejected.  This doc‐
28       ument describes the language accepted by  this  processor.   Extensions
29       will be identified as such.
30
31   OPTIONS
32       -h, --help
33              Print the usage and exit.
34
35       -i, --interactive
36              Force interactive mode.
37
38       -l, --mathlib
39              Define the standard math library.
40
41       -w, --warn
42              Give warnings for extensions to POSIX bc.
43
44       -s, --standard
45              Process exactly the POSIX bc language.
46
47       -q, --quiet
48              Do not print the normal GNU bc welcome.
49
50       -v, --version
51              Print the version number and copyright and quit.
52
53   NUMBERS
54       The most basic element in bc is the number.  Numbers are arbitrary pre‐
55       cision numbers.  This precision is both in the  integer  part  and  the
56       fractional part.  All numbers are represented internally in decimal and
57       all computation is done in decimal.  (This  version  truncates  results
58       from divide and multiply operations.)  There are two attributes of num‐
59       bers, the length and the scale.  The length is the total number of sig‐
60       nificant  decimal  digits in a number and the scale is the total number
61       of decimal digits after the decimal point.  For example:
62               .000001 has a length of 6 and scale of 6.
63               1935.000 has a length of 7 and a scale of 3.
64
65   VARIABLES
66       Numbers are stored in two types  of  variables,  simple  variables  and
67       arrays.   Both  simple  variables and array variables are named.  Names
68       begin with a letter followed by  any  number  of  letters,  digits  and
69       underscores.   All  letters  must  be  lower case.  (Full alpha-numeric
70       names are an extension. In POSIX bc all names are a single  lower  case
71       letter.)   The  type  of  variable  is clear by the context because all
72       array variable names will be followed by brackets ([]).
73
74       There are four special variables, scale, ibase, obase, and last.  scale
75       defines  how  some  operations use digits after the decimal point.  The
76       default value of scale is 0. ibase and obase define the conversion base
77       for input and output numbers.  The default for both input and output is
78       base 10.  last (an extension) is a variable that has the value  of  the
79       last  printed  number.  These will be discussed in further detail where
80       appropriate.  All of these variables may have values assigned  to  them
81       as well as used in expressions.
82
83   COMMENTS
84       Comments in bc start with the characters /* and end with the characters
85       */.  Comments may start anywhere and appear as a single  space  in  the
86       input.   (This causes comments to delimit other input items.  For exam‐
87       ple, a comment can not be found in the  middle  of  a  variable  name.)
88       Comments  include  any newlines (end of line) between the start and the
89       end of the comment.
90
91       To support the use of scripts for bc, a single line  comment  has  been
92       added  as  an extension.  A single line comment starts at a # character
93       and continues to the next end of the line.  The end of  line  character
94       is not part of the comment and is processed normally.
95
96   EXPRESSIONS
97       The  numbers  are manipulated by expressions and statements.  Since the
98       language was designed to be interactive, statements and expressions are
99       executed  as  soon  as possible.  There is no "main" program.  Instead,
100       code is executed as it is encountered.  (Functions, discussed in detail
101       later, are defined when encountered.)
102
103       A  simple  expression  is  just  a constant. bc converts constants into
104       internal decimal numbers using the current input base, specified by the
105       variable ibase. (There is an exception in functions.)  The legal values
106       of ibase are 2 through 16.  Assigning a value  outside  this  range  to
107       ibase will result in a value of 2 or 16.  Input numbers may contain the
108       characters 0-9 and A-F. (Note: They must be capitals.  Lower case  let‐
109       ters  are  variable names.)  Single digit numbers always have the value
110       of the digit regardless of the value of ibase.  (i.e.  A  =  10.)   For
111       multi-digit  numbers,  bc  changes all input digits greater or equal to
112       ibase to the value of ibase-1.  This makes the number FFF always be the
113       largest 3 digit number of the input base.
114
115       Full expressions are similar to many other high level languages.  Since
116       there is only one kind of number, there are no rules for mixing  types.
117       Instead, there are rules on the scale of expressions.  Every expression
118       has a scale.  This is derived from the scale of original  numbers,  the
119       operation performed and in many cases, the value of the variable scale.
120       Legal values of the variable scale are 0 to the maximum  number  repre‐
121       sentable by a C integer.
122
123       In  the following descriptions of legal expressions, "expr" refers to a
124       complete expression and "var" refers to a simple or an array  variable.
125       A simple variable is just a
126              name
127       and an array variable is specified as
128              name[expr]
129       Unless  specifically  mentioned  the scale of the result is the maximum
130       scale of the expressions involved.
131
132       - expr The result is the negation of the expression.
133
134       ++ var The variable is incremented by one and  the  new  value  is  the
135              result of the expression.
136
137       -- var The  variable  is  decremented  by  one and the new value is the
138              result of the expression.
139
140       var ++
141               The result of the expression is the value of the  variable  and
142              then the variable is incremented by one.
143
144       var -- The  result  of  the expression is the value of the variable and
145              then the variable is decremented by one.
146
147       expr + expr
148              The result of the expression is the sum of the two expressions.
149
150       expr - expr
151              The result of the  expression  is  the  difference  of  the  two
152              expressions.
153
154       expr * expr
155              The  result  of the expression is the product of the two expres‐
156              sions.
157
158       expr / expr
159              The result of the expression is the quotient of the two  expres‐
160              sions.   The  scale  of  the result is the value of the variable
161              scale.
162
163       expr % expr
164              The result of the expression is the "remainder" and it  is  com‐
165              puted  in  the following way.  To compute a%b, first a/b is com‐
166              puted to scale digits.  That result is used to compute a-(a/b)*b
167              to  the scale of the maximum of scale+scale(b) and scale(a).  If
168              scale is set to zero and  both  expressions  are  integers  this
169              expression is the integer remainder function.
170
171       expr ^ expr
172              The result of the expression is the value of the first raised to
173              the second. The second expression must be an integer.   (If  the
174              second  expression is not an integer, a warning is generated and
175              the expression is truncated to get an integer value.)  The scale
176              of  the  result  is  scale  if the exponent is negative.  If the
177              exponent is positive the scale of the result is the  minimum  of
178              the  scale  of the first expression times the value of the expo‐
179              nent and the maximum of scale and the scale of the first expres‐
180              sion.    (e.g.   scale(a^b)   =   min(scale(a)*b,   max(  scale,
181              scale(a))).)  It should be noted that expr^0 will always  return
182              the value of 1.
183
184       ( expr )
185              This  alters  the standard precedence to force the evaluation of
186              the expression.
187
188       var = expr
189              The variable is assigned the value of the expression.
190
191       var <op>= expr
192              This is equivalent to "var = var <op> expr" with  the  exception
193              that  the  "var"  part  is evaluated only once.  This can make a
194              difference if "var" is an array.
195
196        Relational expressions are a special kind of  expression  that  always
197       evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is
198       true.  These may appear in any legal expression.   (POSIX  bc  requires
199       that  relational expressions are used only in if, while, and for state‐
200       ments and that only one relational test may  be  done  in  them.)   The
201       relational operators are
202
203       expr1 < expr2
204              The result is 1 if expr1 is strictly less than expr2.
205
206       expr1 <= expr2
207              The result is 1 if expr1 is less than or equal to expr2.
208
209       expr1 > expr2
210              The result is 1 if expr1 is strictly greater than expr2.
211
212       expr1 >= expr2
213              The result is 1 if expr1 is greater than or equal to expr2.
214
215       expr1 == expr2
216              The result is 1 if expr1 is equal to expr2.
217
218       expr1 != expr2
219              The result is 1 if expr1 is not equal to expr2.
220
221       Boolean  operations  are  also  legal.  (POSIX bc does NOT have boolean
222       operations). The result of all boolean operations  are  0  and  1  (for
223       false  and  true)  as in relational expressions.  The boolean operators
224       are:
225
226       !expr  The result is 1 if expr is 0.
227
228       expr && expr
229              The result is 1 if both expressions are non-zero.
230
231       expr || expr
232              The result is 1 if either expression is non-zero.
233
234       The expression precedence is as follows: (lowest to highest)
235              || operator, left associative
236              && operator, left associative
237              ! operator, nonassociative
238              Relational operators, left associative
239              Assignment operator, right associative
240              + and - operators, left associative
241              *, / and % operators, left associative
242              ^ operator, right associative
243              unary - operator, nonassociative
244              ++ and -- operators, nonassociative
245
246       This precedence was chosen so that POSIX compliant bc programs will run
247       correctly. This will cause the use of the relational and logical opera‐
248       tors to have some unusual behavior when used  with  assignment  expres‐
249       sions.  Consider the expression:
250              a = 3 < 5
251
252       Most C programmers would assume this would assign the result of "3 < 5"
253       (the value 1) to the variable "a".  What this does in bc is assign  the
254       value 3 to the variable "a" and then compare 3 to 5.  It is best to use
255       parenthesis when  using  relational  and  logical  operators  with  the
256       assignment operators.
257
258       There  are  a  few  more  special  expressions that are provided in bc.
259       These have to do with user defined functions  and  standard  functions.
260       They  all  appear  as "name(parameters)".  See the section on functions
261       for user defined functions.  The standard functions are:
262
263       length ( expression )
264              The value of the length function is the  number  of  significant
265              digits in the expression.
266
267       read ( )
268              The  read  function  (an  extension) will read a number from the
269              standard  input,  regardless  of  where  the  function   occurs.
270              Beware, this can cause problems with the mixing of data and pro‐
271              gram in the standard input.  The best use for this  function  is
272              in  a previously written program that needs input from the user,
273              but never allows program code to be input from  the  user.   The
274              value  of the read function is the number read from the standard
275              input using the current value of the variable ibase for the con‐
276              version base.
277
278       scale ( expression )
279              The  value  of  the scale function is the number of digits after
280              the decimal point in the expression.
281
282       sqrt ( expression )
283              The value of the sqrt function is the square root of the expres‐
284              sion.  If the expression is negative, a run time error is gener‐
285              ated.
286
287   STATEMENTS
288       Statements (as in most algebraic languages) provide the  sequencing  of
289       expression  evaluation.  In bc statements are executed "as soon as pos‐
290       sible."  Execution happens when a newline in encountered and  there  is
291       one or more complete statements.  Due to this immediate execution, new‐
292       lines are very important in bc. In fact, both a semicolon and a newline
293       are  used  as  statement separators.  An improperly placed newline will
294       cause a syntax error.  Because newlines are statement separators, it is
295       possible  to  hide  a  newline  by  using the backslash character.  The
296       sequence "\<nl>", where <nl> is the newline appears to bc as whitespace
297       instead of a newline.  A statement list is a series of statements sepa‐
298       rated by semicolons and newlines.  The following is a list of bc state‐
299       ments  and what they do: (Things enclosed in brackets ([]) are optional
300       parts of the statement.)
301
302       expression
303              This statement does one of two things.  If the expression starts
304              with  "<variable>  <assignment>  ...", it is considered to be an
305              assignment statement.  If the expression is  not  an  assignment
306              statement,  the  expression is evaluated and printed to the out‐
307              put.  After the number is printed, a newline  is  printed.   For
308              example,  "a=1"  is  an  assignment  statement and "(a=1)" is an
309              expression that has an embedded assignment.   All  numbers  that
310              are  printed  are  printed in the base specified by the variable
311              obase. The legal values for obase  are  2  through  BC_BASE_MAX.
312              (See  the  section  LIMITS.)   For bases 2 through 16, the usual
313              method of writing numbers is used.  For bases greater  than  16,
314              bc  uses  a multi-character digit method of printing the numbers
315              where each higher base digit is printed as  a  base  10  number.
316              The  multi-character digits are separated by spaces.  Each digit
317              contains the number of characters required to represent the base
318              ten  value  of "obase-1".  Since numbers are of arbitrary preci‐
319              sion, some numbers may not be printable on a single output line.
320              These  long  numbers will be split across lines using the "\" as
321              the last character on a line.  The maximum number of  characters
322              printed  per  line  is 70.  Due to the interactive nature of bc,
323              printing a number  causes  the  side  effect  of  assigning  the
324              printed value to the special variable last. This allows the user
325              to recover the last value printed without having to  retype  the
326              expression  that printed the number.  Assigning to last is legal
327              and will overwrite the last  printed  value  with  the  assigned
328              value.  The newly assigned value will remain until the next num‐
329              ber is printed or another value  is  assigned  to  last.   (Some
330              installations  may allow the use of a single period (.) which is
331              not part of a number as a short hand notation for for last.)
332
333       string The string is printed to the output.  Strings start with a  dou‐
334              ble  quote  character  and contain all characters until the next
335              double quote character.   All  characters  are  take  literally,
336              including  any  newline.   No newline character is printed after
337              the string.
338
339       print list
340              The print statement (an extension) provides  another  method  of
341              output.   The  "list" is a list of strings and expressions sepa‐
342              rated by commas.  Each string or expression is  printed  in  the
343              order  of the list.  No terminating newline is printed.  Expres‐
344              sions are evaluated and their value is printed and  assigned  to
345              the variable last. Strings in the print statement are printed to
346              the output and may contain special characters.  Special  charac‐
347              ters  start with the backslash character (\).  The special char‐
348              acters  recognized  by  bc  are  "a"  (alert   or   bell),   "b"
349              (backspace),  "f"  (form  feed),  "n"  (newline),  "r" (carriage
350              return), "q" (double quote), "t"  (tab),  and  "\"  (backslash).
351              Any other character following the backslash will be ignored.
352
353       { statement_list }
354              This  is  the compound statement.  It allows multiple statements
355              to be grouped together for execution.
356
357       if ( expression ) statement1 [else statement2]
358              The if statement evaluates the expression  and  executes  state‐
359              ment1  or  statement2  depending on the value of the expression.
360              If the expression  is  non-zero,  statement1  is  executed.   If
361              statement2 is present and the value of the expression is 0, then
362              statement2 is executed.  (The else clause is an extension.)
363
364       while ( expression ) statement
365              The while statement will execute the statement while the expres‐
366              sion  is non-zero.  It evaluates the expression before each exe‐
367              cution of the statement.   Termination of the loop is caused  by
368              a zero expression value or the execution of a break statement.
369
370       for ( [expression1] ; [expression2] ; [expression3] ) statement
371              The  for statement controls repeated execution of the statement.
372              Expression1 is evaluated before the loop.  Expression2 is evalu‐
373              ated before each execution of the statement.  If it is non-zero,
374              the statement is evaluated.  If it is zero, the loop  is  termi‐
375              nated.   After  each  execution of the statement, expression3 is
376              evaluated before the reevaluation of  expression2.   If  expres‐
377              sion1  or  expression3  are missing, nothing is evaluated at the
378              point they would be evaluated.  If expression2 is missing, it is
379              the  same  as  substituting  the  value 1 for expression2.  (The
380              optional expressions are an extension.  POSIX  bc  requires  all
381              three  expressions.)   The  following is equivalent code for the
382              for statement:
383              expression1;
384              while (expression2) {
385                 statement;
386                 expression3;
387              }
388
389       break  This statement causes a forced exit of the most recent enclosing
390              while statement or for statement.
391
392       continue
393              The  continue  statement  (an extension)  causes the most recent
394              enclosing for statement to start the next iteration.
395
396       halt   The halt statement (an extension) is an executed statement  that
397              causes  the  bc processor to quit only when it is executed.  For
398              example, "if (0 == 1) halt"  will  not  cause  bc  to  terminate
399              because the halt is not executed.
400
401       return Return  the  value 0 from a function.  (See the section on func‐
402              tions.)
403
404       return ( expression )
405              Return the value of the expression from a  function.   (See  the
406              section on functions.)  As an extension, the parenthesis are not
407              required.
408
409   PSEUDO STATEMENTS
410       These statements are not statements in the traditional sense.  They are
411       not  executed  statements.   Their  function  is performed at "compile"
412       time.
413
414       limits Print the local limits enforced by  the  local  version  of  bc.
415              This is an extension.
416
417       quit   When the quit statement is read, the bc processor is terminated,
418              regardless of where the quit statement is found.   For  example,
419              "if (0 == 1) quit" will cause bc to terminate.
420
421       warranty
422              Print a longer warranty notice.  This is an extension.
423
424   FUNCTIONS
425       Functions  provide  a method of defining a computation that can be exe‐
426       cuted later.  Functions in bc always compute a value and return  it  to
427       the  caller.   Function  definitions  are "dynamic" in the sense that a
428       function is undefined until a definition is encountered in  the  input.
429       That  definition is then used until another definition function for the
430       same name is encountered.  The new definition then replaces  the  older
431       definition.  A function is defined as follows:
432              define name ( parameters ) { newline
433                  auto_list   statement_list }
434       A function call is just an expression of the form "name(parameters)".
435
436       Parameters are numbers or arrays (an extension).  In the function defi‐
437       nition, zero or more parameters are defined by listing their names sep‐
438       arated  by  commas.  Numbers are only call by value parameters.  Arrays
439       are only call by variable.  Arrays are specified in the parameter defi‐
440       nition by the notation "name[]".   In the function call, actual parame‐
441       ters are full expressions for number parameters.  The same notation  is
442       used  for  passing  arrays as for defining array parameters.  The named
443       array is passed by variable to the function.   Since  function  defini‐
444       tions are dynamic, parameter numbers and types are checked when a func‐
445       tion is called.  Any mismatch in number or  types  of  parameters  will
446       cause a runtime error.  A runtime error will also occur for the call to
447       an undefined function.
448
449       The auto_list is an optional list of variables  that  are  for  "local"
450       use.   The  syntax of the auto list (if present) is "auto name, ... ;".
451       (The semicolon is optional.)  Each name is the name of  an  auto  vari‐
452       able.   Arrays  may  be specified by using the same notation as used in
453       parameters.  These variables have their values pushed onto a  stack  at
454       the  start of the function.  The variables are then initialized to zero
455       and used throughout the execution of the function.  At  function  exit,
456       these  variables  are popped so that the original value (at the time of
457       the function call) of these variables are restored.  The parameters are
458       really  auto  variables that are initialized to a value provided in the
459       function call.  Auto variables are  different  than  traditional  local
460       variables because if function A calls function B, B may access function
461       A's auto variables by just using the same name, unless function  B  has
462       called  them  auto  variables.  Due to the fact that auto variables and
463       parameters are pushed onto a stack, bc supports recursive functions.
464
465       The function body is a list of bc statements.   Again,  statements  are
466       separated  by semicolons or newlines.  Return statements cause the ter‐
467       mination of a function and the return of a value.  There are  two  ver‐
468       sions  of  the return statement.  The first form, "return", returns the
469       value 0 to the calling expression.  The second form, "return (  expres‐
470       sion )", computes the value of the expression and returns that value to
471       the calling expression.  There is an implied "return (0)" at the end of
472       every function.  This allows a function to terminate and return 0 with‐
473       out an explicit return statement.
474
475       Functions also change the usage of the variable ibase.   All  constants
476       in  the function body will be converted using the value of ibase at the
477       time of the function call.  Changes of ibase will be ignored during the
478       execution  of the function except for the standard function read, which
479       will always use the current value of ibase for conversion of numbers.
480
481       As an extension,  the  format  of  the  definition  has  been  slightly
482       relaxed.   The  standard requires the opening brace be on the same line
483       as the define keyword and all other parts must be on  following  lines.
484       This  version  of bc will allow any number of newlines before and after
485       the opening brace of the function.  For example, the following  defini‐
486       tions are legal.
487              define d (n) { return (2*n); }
488              define d (n)
489                { return (2*n); }
490
491   MATH LIBRARY
492       If  bc  is  invoked with the -l option, a math library is preloaded and
493       the default scale is set to 20.   The  math  functions  will  calculate
494       their  results  to  the  scale set at the time of their call.  The math
495       library defines the following functions:
496
497       s (x)  The sine of x, x is in radians.
498
499       c (x)  The cosine of x, x is in radians.
500
501       a (x)  The arctangent of x, arctangent returns radians.
502
503       l (x)  The natural logarithm of x.
504
505       e (x)  The exponential function of raising e to the value x.
506
507       j (n,x)
508              The bessel function of integer order n of x.
509
510   EXAMPLES
511       In /bin/sh,  the following will assign the value of "pi" to  the  shell
512       variable pi.
513               pi=$(echo "scale=10; 4*a(1)" | bc -l)
514
515       The following is the definition of the exponential function used in the
516       math library.  This function is written in POSIX bc.
517              scale = 20
518
519              /* Uses the fact that e^x = (e^(x/2))^2
520                 When x is small enough, we use the series:
521                   e^x = 1 + x + x^2/2! + x^3/3! + ...
522              */
523
524              define e(x) {
525                auto  a, d, e, f, i, m, v, z
526
527                /* Check the sign of x. */
528                if (x<0) {
529                  m = 1
530                  x = -x
531                }
532
533                /* Precondition x. */
534                z = scale;
535                scale = 4 + z + .44*x;
536                while (x > 1) {
537                  f += 1;
538                  x /= 2;
539                }
540
541                /* Initialize the variables. */
542                v = 1+x
543                a = x
544                d = 1
545
546                for (i=2; 1; i++) {
547                  e = (a *= x) / (d *= i)
548                  if (e == 0) {
549                    if (f>0) while (f--)  v = v*v;
550                    scale = z
551                    if (m) return (1/v);
552                    return (v/1);
553                  }
554                  v += e
555                }
556              }
557
558       The following is code that uses the extended features of bc  to  imple‐
559       ment a simple program for calculating checkbook balances.  This program
560       is best kept in a file so that it can be used many times without having
561       to retype it at every use.
562              scale=2
563              print "\nCheck book program!\n"
564              print "  Remember, deposits are negative transactions.\n"
565              print "  Exit by a 0 transaction.\n\n"
566
567              print "Initial balance? "; bal = read()
568              bal /= 1
569              print "\n"
570              while (1) {
571                "current balance = "; bal
572                "transaction? "; trans = read()
573                if (trans == 0) break;
574                bal -= trans
575                bal /= 1
576              }
577              quit
578
579       The following is the definition of the recursive factorial function.
580              define f (x) {
581                if (x <= 1) return (1);
582                return (f(x-1) * x);
583              }
584
585   READLINE AND LIBEDIT OPTIONS
586       GNU bc can be compiled (via a configure option) to use the GNU readline
587       input editor library or the BSD libedit library.  This allows the  user
588       to do editing of lines before sending them to bc.  It also allows for a
589       history of previous lines typed.  When this option is selected, bc  has
590       one  more special variable.  This special variable, history is the num‐
591       ber of lines of history retained.  For readline, a value  of  -1  means
592       that  an  unlimited  number of history lines are retained.  Setting the
593       value of history to a positive number restricts the number  of  history
594       lines  to  the  number given.  The value of 0 disables the history fea‐
595       ture.  The default value is 100. For more information,  read  the  user
596       manuals  for  the GNU readline, history and BSD libedit libraries.  One
597       can not enable both readline and libedit at the same time.
598
599   DIFFERENCES
600       This version of bc was implemented from the POSIX P1003.2/D11 draft and
601       contains  several  differences and extensions relative to the draft and
602       traditional implementations.  It is not implemented in the  traditional
603       way  using  dc(1).   This  version is a single process which parses and
604       runs a byte code translation of the  program.   There  is  an  "undocu‐
605       mented"  option (-c) that causes the program to output the byte code to
606       the standard output instead of running it.   It  was  mainly  used  for
607       debugging the parser and preparing the math library.
608
609       A  major  source  of  differences  is  extensions,  where  a feature is
610       extended to add more functionality and additions,  where  new  features
611       are added.  The following is the list of differences and extensions.
612
613       LANG   This  version does not conform to the POSIX standard in the pro‐
614              cessing of the LANG environment  variable  and  all  environment
615              variables starting with LC_.
616
617       names  Traditional and POSIX bc have single letter names for functions,
618              variables and arrays.  They have been extended to be multi-char‐
619              acter  names  that  start with a letter and may contain letters,
620              numbers and the underscore character.
621
622       Strings
623              Strings are not allowed to contain NUL characters.   POSIX  says
624              all characters must be included in strings.
625
626       last   POSIX bc does not have a last variable.  Some implementations of
627              bc use the period (.) in a similar way.
628
629       comparisons
630              POSIX bc allows comparisons only in the if statement, the  while
631              statement,  and  the  second  expression  of  the for statement.
632              Also, only one relational operation is allowed in each of  those
633              statements.
634
635       if statement, else clause
636              POSIX bc does not have an else clause.
637
638       for statement
639              POSIX  bc  requires  all  expressions  to  be present in the for
640              statement.
641
642       &&, ||, !
643              POSIX bc does not have the logical operators.
644
645       read function
646              POSIX bc does not have a read function.
647
648       print statement
649              POSIX bc does not have a print statement .
650
651       continue statement
652              POSIX bc does not have a continue statement.
653
654       return statement
655              POSIX bc requires parentheses around the return expression.
656
657       array parameters
658              POSIX bc does not (currently) support array parameters in  full.
659              The POSIX grammar allows for arrays in function definitions, but
660              does not provide a method to  specify  an  array  as  an  actual
661              parameter.   (This  is most likely an oversight in the grammar.)
662              Traditional implementations of bc have only call by value  array
663              parameters.
664
665       function format
666              POSIX  bc  requires  the  opening  brace on the same line as the
667              define key word and the auto statement on the next line.
668
669       =+, =-, =*, =/, =%, =^
670              POSIX bc does not require these "old style" assignment operators
671              to be defined.  This version may allow these "old style" assign‐
672              ments.  Use the limits statement to see if the installed version
673              supports  them.   If  it does support the "old style" assignment
674              operators, the statement "a =- 1" will decrement a by 1  instead
675              of setting a to the value -1.
676
677       spaces in numbers
678              Other  implementations of bc allow spaces in numbers.  For exam‐
679              ple, "x=1 3" would assign the value 13 to the variable  x.   The
680              same statement would cause a syntax error in this version of bc.
681
682       errors and execution
683              This  implementation  varies from other implementations in terms
684              of what code will be executed when syntax and other  errors  are
685              found  in the program.  If a syntax error is found in a function
686              definition, error recovery tries to  find  the  beginning  of  a
687              statement  and  continue  to  parse the function.  Once a syntax
688              error is found  in  the  function,  the  function  will  not  be
689              callable  and  becomes undefined.  Syntax errors in the interac‐
690              tive execution code will invalidate the current execution block.
691              The execution block is terminated by an end of line that appears
692              after a complete sequence of statements.  For example,
693              a = 1
694              b = 2
695       has two execution blocks and
696              { a = 1
697                b = 2 }
698       has one execution block.  Any runtime error will terminate  the  execu‐
699       tion of the current execution block.  A runtime warning will not termi‐
700       nate the current execution block.
701
702       Interrupts
703              During an interactive session, the SIGINT signal (usually gener‐
704              ated  by  the  control-C character from the terminal) will cause
705              execution of the current execution block to be interrupted.   It
706              will  display  a  "runtime"  error indicating which function was
707              interrupted.  After all runtime structures have been cleaned up,
708              a  message  will  be printed to notify the user that bc is ready
709              for more input.  All previously defined functions remain defined
710              and  the  value  of  all non-auto variables are the value at the
711              point of interruption.  All auto variables and function  parame‐
712              ters  are  removed  during  the clean up process.  During a non-
713              interactive session, the SIGINT signal will terminate the entire
714              run of bc.
715
716   LIMITS
717       The  following are the limits currently in place for this bc processor.
718       Some of them may have been changed by an installation.  Use the  limits
719       statement to see the actual values.
720
721       BC_BASE_MAX
722              The  maximum  output  base is currently set at 999.  The maximum
723              input base is 16.
724
725       BC_DIM_MAX
726              This is currently an arbitrary limit of  65535  as  distributed.
727              Your installation may be different.
728
729       BC_SCALE_MAX
730              The  number  of  digits  after  the  decimal point is limited to
731              INT_MAX digits.  Also, the number of digits before  the  decimal
732              point is limited to INT_MAX digits.
733
734       BC_STRING_MAX
735              The  limit  on  the  number of characters in a string is INT_MAX
736              characters.
737
738       exponent
739              The value of the exponent in the raise operation (^) is  limited
740              to LONG_MAX.
741
742       variable names
743              The  current  limit  on  the number of unique names is 32767 for
744              each of simple variables, arrays and functions.
745

ENVIRONMENT VARIABLES

747       The following environment variables are processed by bc:
748
749       POSIXLY_CORRECT
750              This is the same as the -s option.
751
752       BC_ENV_ARGS
753              This is another mechanism to get arguments to bc.  The format is
754              the  same  as  the  command line arguments.  These arguments are
755              processed first, so any files listed in the environent arguments
756              are  processed  before  any  command  line argument files.  This
757              allows the user to set up "standard" options  and  files  to  be
758              processed  at every invocation of bc.  The files in the environ‐
759              ment variables would typically contain function definitions  for
760              functions the user wants defined every time bc is run.
761
762       BC_LINE_LENGTH
763              This  should be an integer specifing the number of characters in
764              an output line for numbers. This includes the backslash and new‐
765              line characters for long numbers.
766

DIAGNOSTICS

768       If  any file on the command line can not be opened, bc will report that
769       the file is unavailable and terminate.  Also, there are compile and run
770       time diagnostics that should be self-explanatory.
771

BUGS

773       Error recovery is not very good yet.
774
775       Email  bug  reports  to  bug-bc@gnu.org.   Be  sure to include the word
776       ``bc'' somewhere in the ``Subject:'' field.
777

AUTHOR

779       Philip A. Nelson
780       philnelson@acm.org
781

ACKNOWLEDGEMENTS

783       The author would like to thank  Steve  Sommars  (Steve.Sommars@att.com)
784       for  his extensive help in testing the implementation.  Many great sug‐
785       gestions were given.  This is a much better product due to his involve‐
786       ment.
787
788
789
790                                       .                                 bc(1)
Impressum