xref: /freebsd/contrib/bc/manuals/bc/EH.1.md (revision 78ae60b4)
1<!---
2
3SPDX-License-Identifier: BSD-2-Clause
4
5Copyright (c) 2018-2023 Gavin D. Howard and contributors.
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are met:
9
10* Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12
13* Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28
29-->
30
31# NAME
32
33bc - arbitrary-precision decimal arithmetic language and calculator
34
35# SYNOPSIS
36
37**bc** [**-cCghilPqRsvVw**] [**-\-digit-clamp**] [**-\-no-digit-clamp**] [**-\-global-stacks**] [**-\-help**] [**-\-interactive**] [**-\-mathlib**] [**-\-no-prompt**] [**-\-no-read-prompt**] [**-\-quiet**] [**-\-standard**] [**-\-warn**] [**-\-version**] [**-e** *expr*] [**-\-expression**=*expr*...] [**-f** *file*...] [**-\-file**=*file*...] [*file*...]
38
39# DESCRIPTION
40
41bc(1) is an interactive processor for a language first standardized in 1991 by
42POSIX. (See the **STANDARDS** section.) The language provides unlimited
43precision decimal arithmetic and is somewhat C-like, but there are differences.
44Such differences will be noted in this document.
45
46After parsing and handling options, this bc(1) reads any files given on the
47command line and executes them before reading from **stdin**.
48
49This bc(1) is a drop-in replacement for *any* bc(1), including (and
50especially) the GNU bc(1). It also has many extensions and extra features beyond
51other implementations.
52
53**Note**: If running this bc(1) on *any* script meant for another bc(1) gives a
54parse error, it is probably because a word this bc(1) reserves as a keyword is
55used as the name of a function, variable, or array. To fix that, use the
56command-line option **-r** *keyword*, where *keyword* is the keyword that is
57used as a name in the script. For more information, see the **OPTIONS** section.
58
59If parsing scripts meant for other bc(1) implementations still does not work,
60that is a bug and should be reported. See the **BUGS** section.
61
62# OPTIONS
63
64The following are the options that bc(1) accepts.
65
66**-C**, **-\-no-digit-clamp**
67
68:   Disables clamping of digits greater than or equal to the current **ibase**
69    when parsing numbers.
70
71    This means that the value added to a number from a digit is always that
72    digit's value multiplied by the value of ibase raised to the power of the
73    digit's position, which starts from 0 at the least significant digit.
74
75    If this and/or the **-c** or **-\-digit-clamp** options are given multiple
76    times, the last one given is used.
77
78    This option overrides the **BC_DIGIT_CLAMP** environment variable (see the
79    **ENVIRONMENT VARIABLES** section) and the default, which can be queried
80    with the **-h** or **-\-help** options.
81
82    This is a **non-portable extension**.
83
84**-c**, **-\-digit-clamp**
85
86:   Enables clamping of digits greater than or equal to the current **ibase**
87    when parsing numbers.
88
89    This means that digits that the value added to a number from a digit that is
90    greater than or equal to the ibase is the value of ibase minus 1 all
91    multiplied by the value of ibase raised to the power of the digit's
92    position, which starts from 0 at the least significant digit.
93
94    If this and/or the **-C** or **-\-no-digit-clamp** options are given
95    multiple times, the last one given is used.
96
97    This option overrides the **BC_DIGIT_CLAMP** environment variable (see the
98    **ENVIRONMENT VARIABLES** section) and the default, which can be queried
99    with the **-h** or **-\-help** options.
100
101    This is a **non-portable extension**.
102
103**-e** *expr*, **-\-expression**=*expr*
104
105:   Evaluates *expr*. If multiple expressions are given, they are evaluated in
106    order. If files are given as well (see the **-f** and **-\-file** options),
107    the expressions and files are evaluated in the order given. This means that
108    if a file is given before an expression, the file is read in and evaluated
109    first.
110
111    If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**,
112    see the **ENVIRONMENT VARIABLES** section), then after processing all
113    expressions and files, bc(1) will exit, unless **-** (**stdin**) was given
114    as an argument at least once to **-f** or **-\-file**, whether on the
115    command-line or in **BC_ENV_ARGS**. However, if any other **-e**,
116    **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-**
117    or equivalent is given, bc(1) will give a fatal error and exit.
118
119    This is a **non-portable extension**.
120
121**-f** *file*, **-\-file**=*file*
122
123:   Reads in *file* and evaluates it, line by line, as though it were read
124    through **stdin**. If expressions are also given (see the **-e** and
125    **-\-expression** options), the expressions are evaluated in the order
126    given.
127
128    If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**,
129    see the **ENVIRONMENT VARIABLES** section), then after processing all
130    expressions and files, bc(1) will exit, unless **-** (**stdin**) was given
131    as an argument at least once to **-f** or **-\-file**. However, if any other
132    **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after
133    **-f-** or equivalent is given, bc(1) will give a fatal error and exit.
134
135    This is a **non-portable extension**.
136
137**-g**, **-\-global-stacks**
138
139:   Turns the globals **ibase**, **obase**, and **scale** into stacks.
140
141    This has the effect that a copy of the current value of all three are pushed
142    onto a stack for every function call, as well as popped when every function
143    returns. This means that functions can assign to any and all of those
144    globals without worrying that the change will affect other functions.
145    Thus, a hypothetical function named **output(x,b)** that simply printed
146    **x** in base **b** could be written like this:
147
148        define void output(x, b) {
149            obase=b
150            x
151        }
152
153    instead of like this:
154
155        define void output(x, b) {
156            auto c
157            c=obase
158            obase=b
159            x
160            obase=c
161        }
162
163    This makes writing functions much easier.
164
165    However, since using this flag means that functions cannot set **ibase**,
166    **obase**, or **scale** globally, functions that are made to do so cannot
167    work anymore. There are two possible use cases for that, and each has a
168    solution.
169
170    First, if a function is called on startup to turn bc(1) into a number
171    converter, it is possible to replace that capability with various shell
172    aliases. Examples:
173
174        alias d2o="bc -e ibase=A -e obase=8"
175        alias h2b="bc -e ibase=G -e obase=2"
176
177    Second, if the purpose of a function is to set **ibase**, **obase**, or
178    **scale** globally for any other purpose, it could be split into one to
179    three functions (based on how many globals it sets) and each of those
180    functions could return the desired value for a global.
181
182    If the behavior of this option is desired for every run of bc(1), then users
183    could make sure to define **BC_ENV_ARGS** and include this option (see the
184    **ENVIRONMENT VARIABLES** section for more details).
185
186    If **-s**, **-w**, or any equivalents are used, this option is ignored.
187
188    This is a **non-portable extension**.
189
190**-h**, **-\-help**
191
192:   Prints a usage message and exits.
193
194**-I** *ibase*, **-\-ibase**=*ibase*
195
196:   Sets the builtin variable **ibase** to the value *ibase* assuming that
197    *ibase* is in base 10. It is a fatal error if *ibase* is not a valid number.
198
199    If multiple instances of this option are given, the last is used.
200
201    This is a **non-portable extension**.
202
203**-i**, **-\-interactive**
204
205:   Forces interactive mode. (See the **INTERACTIVE MODE** section.)
206
207    This is a **non-portable extension**.
208
209**-L**, **-\-no-line-length**
210
211:   Disables line length checking and prints numbers without backslashes and
212    newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see
213    the **ENVIRONMENT VARIABLES** section).
214
215    This is a **non-portable extension**.
216
217**-l**, **-\-mathlib**
218
219:   Sets **scale** (see the **SYNTAX** section) to **20** and loads the included
220    math library before running any code, including any expressions or files
221    specified on the command line.
222
223    To learn what is in the library, see the **LIBRARY** section.
224
225**-O** *obase*, **-\-obase**=*obase*
226
227:   Sets the builtin variable **obase** to the value *obase* assuming that
228    *obase* is in base 10. It is a fatal error if *obase* is not a valid number.
229
230    If multiple instances of this option are given, the last is used.
231
232    This is a **non-portable extension**.
233
234**-P**, **-\-no-prompt**
235
236:   Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode.
237    See the **TTY MODE** section.) This is mostly for those users that do not
238    want a prompt or are not used to having them in bc(1). Most of those users
239    would want to put this option in **BC_ENV_ARGS** (see the
240    **ENVIRONMENT VARIABLES** section).
241
242    These options override the **BC_PROMPT** and **BC_TTY_MODE** environment
243    variables (see the **ENVIRONMENT VARIABLES** section).
244
245    This is a **non-portable extension**.
246
247**-q**, **-\-quiet**
248
249:   This option is for compatibility with the GNU bc(1)
250    (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU
251    bc(1) prints a copyright header. This bc(1) only prints the copyright header
252    if one or more of the **-v**, **-V**, or **-\-version** options are given
253    unless the **BC_BANNER** environment variable is set and contains a non-zero
254    integer or if this bc(1) was built with the header displayed by default. If
255    *any* of that is the case, then this option *does* prevent bc(1) from
256    printing the header.
257
258    This is a **non-portable extension**.
259
260**-R**, **-\-no-read-prompt**
261
262:   Disables the read prompt in TTY mode. (The read prompt is only enabled in
263    TTY mode. See the **TTY MODE** section.) This is mostly for those users that
264    do not want a read prompt or are not used to having them in bc(1). Most of
265    those users would want to put this option in **BC_ENV_ARGS** (see the
266    **ENVIRONMENT VARIABLES** section). This option is also useful in hash bang
267    lines of bc(1) scripts that prompt for user input.
268
269    This option does not disable the regular prompt because the read prompt is
270    only used when the **read()** built-in function is called.
271
272    These options *do* override the **BC_PROMPT** and **BC_TTY_MODE**
273    environment variables (see the **ENVIRONMENT VARIABLES** section), but only
274    for the read prompt.
275
276    This is a **non-portable extension**.
277
278**-r** *keyword*, **-\-redefine**=*keyword*
279
280:   Redefines *keyword* in order to allow it to be used as a function, variable,
281    or array name. This is useful when this bc(1) gives parse errors when
282    parsing scripts meant for other bc(1) implementations.
283
284    The keywords this bc(1) allows to be redefined are:
285
286    * **abs**
287    * **asciify**
288    * **continue**
289    * **divmod**
290    * **else**
291    * **halt**
292    * **last**
293    * **limits**
294    * **maxibase**
295    * **maxobase**
296    * **maxscale**
297    * **modexp**
298    * **print**
299    * **read**
300	* **stream**
301
302    If any of those keywords are used as a function, variable, or array name in
303    a script, use this option with the keyword as the argument. If multiple are
304    used, use this option for all of them; it can be used multiple times.
305
306    Keywords are *not* redefined when parsing the builtin math library (see the
307    **LIBRARY** section).
308
309    It is a fatal error to redefine keywords mandated by the POSIX standard (see
310    the **STANDARDS** section). It is a fatal error to attempt to redefine words
311    that this bc(1) does not reserve as keywords.
312
313**-S** *scale*, **-\-scale**=*scale*
314
315:   Sets the builtin variable **scale** to the value *scale* assuming that
316    *scale* is in base 10. It is a fatal error if *scale* is not a valid number.
317
318    If multiple instances of this option are given, the last is used.
319
320    This is a **non-portable extension**.
321
322**-s**, **-\-standard**
323
324:   Process exactly the language defined by the standard (see the **STANDARDS**
325    section) and error if any extensions are used.
326
327    This is a **non-portable extension**.
328
329**-v**, **-V**, **-\-version**
330
331:   Print the version information (copyright header) and exits.
332
333    This is a **non-portable extension**.
334
335**-w**, **-\-warn**
336
337:   Like **-s** and **-\-standard**, except that warnings (and not errors) are
338    printed for non-standard extensions and execution continues normally.
339
340    This is a **non-portable extension**.
341
342**-z**, **-\-leading-zeroes**
343
344:   Makes bc(1) print all numbers greater than **-1** and less than **1**, and
345    not equal to **0**, with a leading zero.
346
347    This can be set for individual numbers with the **plz(x)**, **plznl(x)**,
348    **pnlz(x)**, and **pnlznl(x)** functions in the extended math library (see
349    the **LIBRARY** section).
350
351    This is a **non-portable extension**.
352
353All long options are **non-portable extensions**.
354
355# STDIN
356
357If no files or expressions are given by the **-f**, **-\-file**, **-e**, or
358**-\-expression** options, then bc(1) reads from **stdin**.
359
360However, there are a few caveats to this.
361
362First, **stdin** is evaluated a line at a time. The only exception to this is if
363the parse cannot complete. That means that starting a string without ending it
364or starting a function, **if** statement, or loop without ending it will also
365cause bc(1) to not execute.
366
367Second, after an **if** statement, bc(1) doesn't know if an **else** statement
368will follow, so it will not execute until it knows there will not be an **else**
369statement.
370
371# STDOUT
372
373Any non-error output is written to **stdout**. In addition, if history (see the
374**HISTORY** section) and the prompt (see the **TTY MODE** section) are enabled,
375both are output to **stdout**.
376
377**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal
378error (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if
379**stdout** is closed, as in **bc <file> >&-**, it will quit with an error. This
380is done so that bc(1) can report problems when **stdout** is redirected to a
381file.
382
383If there are scripts that depend on the behavior of other bc(1) implementations,
384it is recommended that those scripts be changed to redirect **stdout** to
385**/dev/null**.
386
387# STDERR
388
389Any error output is written to **stderr**.
390
391**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal
392error (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if
393**stderr** is closed, as in **bc <file> 2>&-**, it will quit with an error. This
394is done so that bc(1) can exit with an error code when **stderr** is redirected
395to a file.
396
397If there are scripts that depend on the behavior of other bc(1) implementations,
398it is recommended that those scripts be changed to redirect **stderr** to
399**/dev/null**.
400
401# SYNTAX
402
403The syntax for bc(1) programs is mostly C-like, with some differences. This
404bc(1) follows the POSIX standard (see the **STANDARDS** section), which is a
405much more thorough resource for the language this bc(1) accepts. This section is
406meant to be a summary and a listing of all the extensions to the standard.
407
408In the sections below, **E** means expression, **S** means statement, and **I**
409means identifier.
410
411Identifiers (**I**) start with a lowercase letter and can be followed by any
412number (up to **BC_NAME_MAX-1**) of lowercase letters (**a-z**), digits
413(**0-9**), and underscores (**\_**). The regex is **\[a-z\]\[a-z0-9\_\]\***.
414Identifiers with more than one character (letter) are a
415**non-portable extension**.
416
417**ibase** is a global variable determining how to interpret constant numbers. It
418is the "input" base, or the number base used for interpreting input numbers.
419**ibase** is initially **10**. If the **-s** (**-\-standard**) and **-w**
420(**-\-warn**) flags were not given on the command line, the max allowable value
421for **ibase** is **36**. Otherwise, it is **16**. The min allowable value for
422**ibase** is **2**. The max allowable value for **ibase** can be queried in
423bc(1) programs with the **maxibase()** built-in function.
424
425**obase** is a global variable determining how to output results. It is the
426"output" base, or the number base used for outputting numbers. **obase** is
427initially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and
428can be queried in bc(1) programs with the **maxobase()** built-in function. The
429min allowable value for **obase** is **2**. Values are output in the specified
430base.
431
432The *scale* of an expression is the number of digits in the result of the
433expression right of the decimal point, and **scale** is a global variable that
434sets the precision of any operations, with exceptions. **scale** is initially
435**0**. **scale** cannot be negative. The max allowable value for **scale** is
436**BC_SCALE_MAX** and can be queried in bc(1) programs with the **maxscale()**
437built-in function.
438
439bc(1) has both *global* variables and *local* variables. All *local*
440variables are local to the function; they are parameters or are introduced in
441the **auto** list of a function (see the **FUNCTIONS** section). If a variable
442is accessed which is not a parameter or in the **auto** list, it is assumed to
443be *global*. If a parent function has a *local* variable version of a variable
444that a child function considers *global*, the value of that *global* variable in
445the child function is the value of the variable in the parent function, not the
446value of the actual *global* variable.
447
448All of the above applies to arrays as well.
449
450The value of a statement that is an expression (i.e., any of the named
451expressions or operands) is printed unless the lowest precedence operator is an
452assignment operator *and* the expression is notsurrounded by parentheses.
453
454The value that is printed is also assigned to the special variable **last**. A
455single dot (**.**) may also be used as a synonym for **last**. These are
456**non-portable extensions**.
457
458Either semicolons or newlines may separate statements.
459
460## Comments
461
462There are two kinds of comments:
463
4641.	Block comments are enclosed in **/\*** and **\*/**.
4652.	Line comments go from **#** until, and not including, the next newline. This
466	is a **non-portable extension**.
467
468## Named Expressions
469
470The following are named expressions in bc(1):
471
4721.	Variables: **I**
4732.	Array Elements: **I[E]**
4743.	**ibase**
4754.	**obase**
4765.	**scale**
4776.	**last** or a single dot (**.**)
478
479Number 6 is a **non-portable extension**.
480
481Variables and arrays do not interfere; users can have arrays named the same as
482variables. This also applies to functions (see the **FUNCTIONS** section), so a
483user can have a variable, array, and function that all have the same name, and
484they will not shadow each other, whether inside of functions or not.
485
486Named expressions are required as the operand of **increment**/**decrement**
487operators  and as the left side of **assignment** operators (see the *Operators*
488subsection).
489
490## Operands
491
492The following are valid operands in bc(1):
493
4941.	Numbers (see the *Numbers* subsection below).
4952.	Array indices (**I[E]**).
4963.	**(E)**: The value of **E** (used to change precedence).
4974.	**sqrt(E)**: The square root of **E**. **E** must be non-negative.
4985.	**length(E)**: The number of significant decimal digits in **E**. Returns
499	**1** for **0** with no decimal places. If given a string, the length of the
500	string is returned. Passing a string to **length(E)** is a **non-portable
501	extension**.
5026.	**length(I[])**: The number of elements in the array **I**. This is a
503	**non-portable extension**.
5047.	**scale(E)**: The *scale* of **E**.
5058.	**abs(E)**: The absolute value of **E**. This is a **non-portable
506	extension**.
5079.	**is_number(E)**: **1** if the given argument is a number, **0** if it is a
508	string. This is a **non-portable extension**.
50910.	**is_string(E)**: **1** if the given argument is a string, **0** if it is a
510	number. This is a **non-portable extension**.
51111.	**modexp(E, E, E)**: Modular exponentiation, where the first expression is
512	the base, the second is the exponent, and the third is the modulus. All
513	three values must be integers. The second argument must be non-negative. The
514	third argument must be non-zero. This is a **non-portable extension**.
51511.	**divmod(E, E, I[])**: Division and modulus in one operation. This is for
516	optimization. The first expression is the dividend, and the second is the
517	divisor, which must be non-zero. The return value is the quotient, and the
518	modulus is stored in index **0** of the provided array (the last argument).
519	This is a **non-portable extension**.
52012.	**asciify(E)**: If **E** is a string, returns a string that is the first
521	letter of its argument. If it is a number, calculates the number mod **256**
522	and returns that number as a one-character string. This is a **non-portable
523	extension**.
52413.	**asciify(I[])**: A string that is made up of the characters that would
525	result from running **asciify(E)** on each element of the array identified
526	by the argument. This allows creating multi-character strings and storing
527	them. This is a **non-portable extension**.
52814.	**I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for
529	a non-**void** function (see the *Void Functions* subsection of the
530	**FUNCTIONS** section). The **E** argument(s) may also be arrays of the form
531	**I[]**, which will automatically be turned into array references (see the
532	*Array References* subsection of the **FUNCTIONS** section) if the
533	corresponding parameter in the function definition is an array reference.
53415.	**read()**: Reads a line from **stdin** and uses that as an expression. The
535	result of that expression is the result of the **read()** operand. This is a
536	**non-portable extension**.
53716.	**maxibase()**: The max allowable **ibase**. This is a **non-portable
538	extension**.
53917.	**maxobase()**: The max allowable **obase**. This is a **non-portable
540	extension**.
54118.	**maxscale()**: The max allowable **scale**. This is a **non-portable
542	extension**.
54319.	**line_length()**: The line length set with **BC_LINE_LENGTH** (see the
544	**ENVIRONMENT VARIABLES** section). This is a **non-portable extension**.
54520.	**global_stacks()**: **0** if global stacks are not enabled with the **-g**
546	or **-\-global-stacks** options, non-zero otherwise. See the **OPTIONS**
547	section. This is a **non-portable extension**.
54821.	**leading_zero()**: **0** if leading zeroes are not enabled with the **-z**
549	or **--leading-zeroes** options, non-zero otherwise. See the **OPTIONS**
550	section. This is a **non-portable extension**.
551
552## Numbers
553
554Numbers are strings made up of digits, uppercase letters, and at most **1**
555period for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase
556letters are equal to **9** plus their position in the alphabet, starting from
557**1** (i.e., **A** equals **10**, or **9+1**).
558
559If a digit or letter makes no sense with the current value of **ibase** (i.e.,
560they are greater than or equal to the current value of **ibase**), then the
561behavior depends on the existence of the **-c**/**-\-digit-clamp** or
562**-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the
563existence and setting of the **BC_DIGIT_CLAMP** environment variable (see the
564**ENVIRONMENT VARIABLES** section), or the default, which can be queried with
565the **-h**/**-\-help** option.
566
567If clamping is off, then digits or letters that are greater than or equal to the
568current value of **ibase** are not changed. Instead, their given value is
569multiplied by the appropriate power of **ibase** and added into the number. This
570means that, with an **ibase** of **3**, the number **AB** is equal to
571**3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or **41**.
572
573If clamping is on, then digits or letters that are greater than or equal to the
574current value of **ibase** are set to the value of the highest valid digit in
575**ibase** before being multiplied by the appropriate power of **ibase** and
576added into the number. This means that, with an **ibase** of **3**, the number
577**AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**,
578or **8**.
579
580There is one exception to clamping: single-character numbers (i.e., **A**
581alone). Such numbers are never clamped and always take the value they would have
582in the highest possible **ibase**. This means that **A** alone always equals
583decimal **10** and **Z** alone always equals decimal **35**. This behavior is
584mandated by the standard (see the STANDARDS section) and is meant to provide an
585easy way to set the current **ibase** (with the **i** command) regardless of the
586current value of **ibase**.
587
588If clamping is on, and the clamped value of a character is needed, use a leading
589zero, i.e., for **A**, use **0A**.
590
591## Operators
592
593The following arithmetic and logical operators can be used. They are listed in
594order of decreasing precedence. Operators in the same group have the same
595precedence.
596
597**++** **-\-**
598
599:   Type: Prefix and Postfix
600
601    Associativity: None
602
603    Description: **increment**, **decrement**
604
605**-** **!**
606
607:   Type: Prefix
608
609    Associativity: None
610
611    Description: **negation**, **boolean not**
612
613**\^**
614
615:   Type: Binary
616
617    Associativity: Right
618
619    Description: **power**
620
621**\*** **/** **%**
622
623:   Type: Binary
624
625    Associativity: Left
626
627    Description: **multiply**, **divide**, **modulus**
628
629**+** **-**
630
631:   Type: Binary
632
633    Associativity: Left
634
635    Description: **add**, **subtract**
636
637**=** **+=** **-=** **\*=** **/=** **%=** **\^=**
638
639:   Type: Binary
640
641    Associativity: Right
642
643    Description: **assignment**
644
645**==** **\<=** **\>=** **!=** **\<** **\>**
646
647:   Type: Binary
648
649    Associativity: Left
650
651    Description: **relational**
652
653**&&**
654
655:   Type: Binary
656
657    Associativity: Left
658
659    Description: **boolean and**
660
661**||**
662
663:   Type: Binary
664
665    Associativity: Left
666
667    Description: **boolean or**
668
669The operators will be described in more detail below.
670
671**++** **-\-**
672
673:   The prefix and postfix **increment** and **decrement** operators behave
674    exactly like they would in C. They require a named expression (see the
675    *Named Expressions* subsection) as an operand.
676
677    The prefix versions of these operators are more efficient; use them where
678    possible.
679
680**-**
681
682:   The **negation** operator returns **0** if a user attempts to negate any
683    expression with the value **0**. Otherwise, a copy of the expression with
684    its sign flipped is returned.
685
686**!**
687
688:   The **boolean not** operator returns **1** if the expression is **0**, or
689    **0** otherwise.
690
691    This is a **non-portable extension**.
692
693**\^**
694
695:   The **power** operator (not the **exclusive or** operator, as it would be in
696    C) takes two expressions and raises the first to the power of the value of
697    the second. The *scale* of the result is equal to **scale**.
698
699    The second expression must be an integer (no *scale*), and if it is
700    negative, the first value must be non-zero.
701
702**\***
703
704:   The **multiply** operator takes two expressions, multiplies them, and
705    returns the product. If **a** is the *scale* of the first expression and
706    **b** is the *scale* of the second expression, the *scale* of the result is
707    equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return
708    the obvious values.
709
710**/**
711
712:   The **divide** operator takes two expressions, divides them, and returns the
713    quotient. The *scale* of the result shall be the value of **scale**.
714
715    The second expression must be non-zero.
716
717**%**
718
719:   The **modulus** operator takes two expressions, **a** and **b**, and
720    evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the
721    result of step 1 to calculate **a-(a/b)\*b** to *scale*
722    **max(scale+scale(b),scale(a))**.
723
724    The second expression must be non-zero.
725
726**+**
727
728:   The **add** operator takes two expressions, **a** and **b**, and returns the
729    sum, with a *scale* equal to the max of the *scale*s of **a** and **b**.
730
731**-**
732
733:   The **subtract** operator takes two expressions, **a** and **b**, and
734    returns the difference, with a *scale* equal to the max of the *scale*s of
735    **a** and **b**.
736
737**=** **+=** **-=** **\*=** **/=** **%=** **\^=**
738
739:   The **assignment** operators take two expressions, **a** and **b** where
740    **a** is a named expression (see the *Named Expressions* subsection).
741
742    For **=**, **b** is copied and the result is assigned to **a**. For all
743    others, **a** and **b** are applied as operands to the corresponding
744    arithmetic operator and the result is assigned to **a**.
745
746**==** **\<=** **\>=** **!=** **\<** **\>**
747
748:   The **relational** operators compare two expressions, **a** and **b**, and
749    if the relation holds, according to C language semantics, the result is
750    **1**. Otherwise, it is **0**.
751
752    Note that unlike in C, these operators have a lower precedence than the
753    **assignment** operators, which means that **a=b\>c** is interpreted as
754    **(a=b)\>c**.
755
756    Also, unlike the standard (see the **STANDARDS** section) requires, these
757    operators can appear anywhere any other expressions can be used. This
758    allowance is a **non-portable extension**.
759
760**&&**
761
762:   The **boolean and** operator takes two expressions and returns **1** if both
763    expressions are non-zero, **0** otherwise.
764
765    This is *not* a short-circuit operator.
766
767    This is a **non-portable extension**.
768
769**||**
770
771:   The **boolean or** operator takes two expressions and returns **1** if one
772    of the expressions is non-zero, **0** otherwise.
773
774    This is *not* a short-circuit operator.
775
776    This is a **non-portable extension**.
777
778## Statements
779
780The following items are statements:
781
7821.	**E**
7832.	**{** **S** **;** ... **;** **S** **}**
7843.	**if** **(** **E** **)** **S**
7854.	**if** **(** **E** **)** **S** **else** **S**
7865.	**while** **(** **E** **)** **S**
7876.	**for** **(** **E** **;** **E** **;** **E** **)** **S**
7887.	An empty statement
7898.	**break**
7909.	**continue**
79110.	**quit**
79211.	**halt**
79312.	**limits**
79413.	A string of characters, enclosed in double quotes
79514.	**print** **E** **,** ... **,** **E**
79615.	**stream** **E** **,** ... **,** **E**
79716.	**I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for
798	a **void** function (see the *Void Functions* subsection of the
799	**FUNCTIONS** section). The **E** argument(s) may also be arrays of the form
800	**I[]**, which will automatically be turned into array references (see the
801	*Array References* subsection of the **FUNCTIONS** section) if the
802	corresponding parameter in the function definition is an array reference.
803
804Numbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**.
805
806Also, as a **non-portable extension**, any or all of the expressions in the
807header of a for loop may be omitted. If the condition (second expression) is
808omitted, it is assumed to be a constant **1**.
809
810The **break** statement causes a loop to stop iterating and resume execution
811immediately following a loop. This is only allowed in loops.
812
813The **continue** statement causes a loop iteration to stop early and returns to
814the start of the loop, including testing the loop condition. This is only
815allowed in loops.
816
817The **if** **else** statement does the same thing as in C.
818
819The **quit** statement causes bc(1) to quit, even if it is on a branch that will
820not be executed (it is a compile-time command).
821
822**Warning**: The behavior of this bc(1) on **quit** is slightly different from
823other bc(1) implementations. Other bc(1) implementations will exit as soon as
824they finish parsing the line that a **quit** command is on. This bc(1) will
825execute any completed and executable statements that occur before the **quit**
826statement before exiting.
827
828In other words, for the bc(1) code below:
829
830    for (i = 0; i < 3; ++i) i; quit
831
832Other bc(1) implementations will print nothing, and this bc(1) will print **0**,
833**1**, and **2** on successive lines before exiting.
834
835The **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit**
836if it is on a branch of an **if** statement that is not executed, bc(1) does not
837quit.)
838
839The **limits** statement prints the limits that this bc(1) is subject to. This
840is like the **quit** statement in that it is a compile-time command.
841
842An expression by itself is evaluated and printed, followed by a newline.
843
844## Strings
845
846If strings appear as a statement by themselves, they are printed without a
847trailing newline.
848
849In addition to appearing as a lone statement by themselves, strings can be
850assigned to variables and array elements. They can also be passed to functions
851in variable parameters.
852
853If any statement that expects a string is given a variable that had a string
854assigned to it, the statement acts as though it had received a string.
855
856If any math operation is attempted on a string or a variable or array element
857that has been assigned a string, an error is raised, and bc(1) resets (see the
858**RESET** section).
859
860Assigning strings to variables and array elements and passing them to functions
861are **non-portable extensions**.
862
863## Print Statement
864
865The "expressions" in a **print** statement may also be strings. If they are, there
866are backslash escape sequences that are interpreted specially. What those
867sequences are, and what they cause to be printed, are shown below:
868
869**\\a**:   **\\a**
870
871**\\b**:   **\\b**
872
873**\\\\**:   **\\**
874
875**\\e**:   **\\**
876
877**\\f**:   **\\f**
878
879**\\n**:   **\\n**
880
881**\\q**:   **"**
882
883**\\r**:   **\\r**
884
885**\\t**:   **\\t**
886
887Any other character following a backslash causes the backslash and character to
888be printed as-is.
889
890Any non-string expression in a print statement shall be assigned to **last**,
891like any other expression that is printed.
892
893## Stream Statement
894
895The expressions in a **stream** statement may also be strings.
896
897If a **stream** statement is given a string, it prints the string as though the
898string had appeared as its own statement. In other words, the **stream**
899statement prints strings normally, without a newline.
900
901If a **stream** statement is given a number, a copy of it is truncated and its
902absolute value is calculated. The result is then printed as though **obase** is
903**256** and each digit is interpreted as an 8-bit ASCII character, making it a
904byte stream.
905
906## Order of Evaluation
907
908All expressions in a statment are evaluated left to right, except as necessary
909to maintain order of operations. This means, for example, assuming that **i** is
910equal to **0**, in the expression
911
912    a[i++] = i++
913
914the first (or 0th) element of **a** is set to **1**, and **i** is equal to **2**
915at the end of the expression.
916
917This includes function arguments. Thus, assuming **i** is equal to **0**, this
918means that in the expression
919
920    x(i++, i++)
921
922the first argument passed to **x()** is **0**, and the second argument is **1**,
923while **i** is equal to **2** before the function starts executing.
924
925# FUNCTIONS
926
927Function definitions are as follows:
928
929```
930define I(I,...,I){
931	auto I,...,I
932	S;...;S
933	return(E)
934}
935```
936
937Any **I** in the parameter list or **auto** list may be replaced with **I[]** to
938make a parameter or **auto** var an array, and any **I** in the parameter list
939may be replaced with **\*I[]** to make a parameter an array reference. Callers
940of functions that take array references should not put an asterisk in the call;
941they must be called with just **I[]** like normal array parameters and will be
942automatically converted into references.
943
944As a **non-portable extension**, the opening brace of a **define** statement may
945appear on the next line.
946
947As a **non-portable extension**, the return statement may also be in one of the
948following forms:
949
9501.	**return**
9512.	**return** **(** **)**
9523.	**return** **E**
953
954The first two, or not specifying a **return** statement, is equivalent to
955**return (0)**, unless the function is a **void** function (see the *Void
956Functions* subsection below).
957
958## Void Functions
959
960Functions can also be **void** functions, defined as follows:
961
962```
963define void I(I,...,I){
964	auto I,...,I
965	S;...;S
966	return
967}
968```
969
970They can only be used as standalone expressions, where such an expression would
971be printed alone, except in a print statement.
972
973Void functions can only use the first two **return** statements listed above.
974They can also omit the return statement entirely.
975
976The word "void" is not treated as a keyword; it is still possible to have
977variables, arrays, and functions named **void**. The word "void" is only
978treated specially right after the **define** keyword.
979
980This is a **non-portable extension**.
981
982## Array References
983
984For any array in the parameter list, if the array is declared in the form
985
986```
987*I[]
988```
989
990it is a **reference**. Any changes to the array in the function are reflected,
991when the function returns, to the array that was passed in.
992
993Other than this, all function arguments are passed by value.
994
995This is a **non-portable extension**.
996
997# LIBRARY
998
999All of the functions below  are available when the **-l** or **-\-mathlib**
1000command-line flags are given.
1001
1002## Standard Library
1003
1004The standard (see the **STANDARDS** section) defines the following functions for
1005the math library:
1006
1007**s(x)**
1008
1009:   Returns the sine of **x**, which is assumed to be in radians.
1010
1011    This is a transcendental function (see the *Transcendental Functions*
1012    subsection below).
1013
1014**c(x)**
1015
1016:   Returns the cosine of **x**, which is assumed to be in radians.
1017
1018    This is a transcendental function (see the *Transcendental Functions*
1019    subsection below).
1020
1021**a(x)**
1022
1023:   Returns the arctangent of **x**, in radians.
1024
1025    This is a transcendental function (see the *Transcendental Functions*
1026    subsection below).
1027
1028**l(x)**
1029
1030:   Returns the natural logarithm of **x**.
1031
1032    This is a transcendental function (see the *Transcendental Functions*
1033    subsection below).
1034
1035**e(x)**
1036
1037:   Returns the mathematical constant **e** raised to the power of **x**.
1038
1039    This is a transcendental function (see the *Transcendental Functions*
1040    subsection below).
1041
1042**j(x, n)**
1043
1044:   Returns the bessel integer order **n** (truncated) of **x**.
1045
1046    This is a transcendental function (see the *Transcendental Functions*
1047    subsection below).
1048
1049## Transcendental Functions
1050
1051All transcendental functions can return slightly inaccurate results, up to 1 ULP
1052(https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and
1053the  article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains
1054why it is impossible and unnecessary to calculate exact results for the
1055transcendental functions.
1056
1057Because of the possible inaccuracy, I recommend that users call those functions
1058with the precision (**scale**) set to at least 1 higher than is necessary. If
1059exact results are *absolutely* required, users can double the precision
1060(**scale**) and then truncate.
1061
1062The transcendental functions in the standard math library are:
1063
1064* **s(x)**
1065* **c(x)**
1066* **a(x)**
1067* **l(x)**
1068* **e(x)**
1069* **j(x, n)**
1070
1071# RESET
1072
1073When bc(1) encounters an error or a signal that it has a non-default handler
1074for, it resets. This means that several things happen.
1075
1076First, any functions that are executing are stopped and popped off the stack.
1077The behavior is not unlike that of exceptions in programming languages. Then
1078the execution point is set so that any code waiting to execute (after all
1079functions returned) is skipped.
1080
1081Thus, when bc(1) resets, it skips any remaining code waiting to be executed.
1082Then, if it is interactive mode, and the error was not a fatal error (see the
1083**EXIT STATUS** section), it asks for more input; otherwise, it exits with the
1084appropriate return code.
1085
1086Note that this reset behavior is different from the GNU bc(1), which attempts to
1087start executing the statement right after the one that caused an error.
1088
1089# PERFORMANCE
1090
1091Most bc(1) implementations use **char** types to calculate the value of **1**
1092decimal digit at a time, but that can be slow. This bc(1) does something
1093different.
1094
1095It uses large integers to calculate more than **1** decimal digit at a time. If
1096built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is
1097**64**, then each integer has **9** decimal digits. If built in an environment
1098where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This
1099value (the number of decimal digits per large integer) is called
1100**BC_BASE_DIGS**.
1101
1102The actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with
1103the **limits** statement.
1104
1105In addition, this bc(1) uses an even larger integer for overflow checking. This
1106integer type depends on the value of **BC_LONG_BIT**, but is always at least
1107twice as large as the integer type used to store digits.
1108
1109# LIMITS
1110
1111The following are the limits on bc(1):
1112
1113**BC_LONG_BIT**
1114
1115:   The number of bits in the **long** type in the environment where bc(1) was
1116    built. This determines how many decimal digits can be stored in a single
1117    large integer (see the **PERFORMANCE** section).
1118
1119**BC_BASE_DIGS**
1120
1121:   The number of decimal digits per large integer (see the **PERFORMANCE**
1122    section). Depends on **BC_LONG_BIT**.
1123
1124**BC_BASE_POW**
1125
1126:   The max decimal number that each large integer can store (see
1127    **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**.
1128
1129**BC_OVERFLOW_MAX**
1130
1131:   The max number that the overflow type (see the **PERFORMANCE** section) can
1132    hold. Depends on **BC_LONG_BIT**.
1133
1134**BC_BASE_MAX**
1135
1136:   The maximum output base. Set at **BC_BASE_POW**.
1137
1138**BC_DIM_MAX**
1139
1140:   The maximum size of arrays. Set at **SIZE_MAX-1**.
1141
1142**BC_SCALE_MAX**
1143
1144:   The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**.
1145
1146**BC_STRING_MAX**
1147
1148:   The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**.
1149
1150**BC_NAME_MAX**
1151
1152:   The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**.
1153
1154**BC_NUM_MAX**
1155
1156:   The maximum length of a number (in decimal digits), which includes digits
1157    after the decimal point. Set at **BC_OVERFLOW_MAX-1**.
1158
1159Exponent
1160
1161:   The maximum allowable exponent (positive or negative). Set at
1162    **BC_OVERFLOW_MAX**.
1163
1164Number of vars
1165
1166:   The maximum number of vars/arrays. Set at **SIZE_MAX-1**.
1167
1168The actual values can be queried with the **limits** statement.
1169
1170These limits are meant to be effectively non-existent; the limits are so large
1171(at least on 64-bit machines) that there should not be any point at which they
1172become a problem. In fact, memory should be exhausted before these limits should
1173be hit.
1174
1175# ENVIRONMENT VARIABLES
1176
1177As **non-portable extensions**, bc(1) recognizes the following environment
1178variables:
1179
1180**POSIXLY_CORRECT**
1181
1182:   If this variable exists (no matter the contents), bc(1) behaves as if
1183    the **-s** option was given.
1184
1185**BC_ENV_ARGS**
1186
1187:   This is another way to give command-line arguments to bc(1). They should be
1188    in the same format as all other command-line arguments. These are always
1189    processed first, so any files given in **BC_ENV_ARGS** will be processed
1190    before arguments and files given on the command-line. This gives the user
1191    the ability to set up "standard" options and files to be used at every
1192    invocation. The most useful thing for such files to contain would be useful
1193    functions that the user might want every time bc(1) runs.
1194
1195    The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments,
1196    but it does not understand escape sequences. For example, the string
1197    **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string
1198    **"/home/gavin/some \"bc\" file.bc"** will include the backslashes.
1199
1200    The quote parsing will handle either kind of quotes, **'** or **"**. Thus,
1201    if you have a file with any number of single quotes in the name, you can use
1202    double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice
1203    versa if you have a file with double quotes. However, handling a file with
1204    both kinds of quotes in **BC_ENV_ARGS** is not supported due to the
1205    complexity of the parsing, though such files are still supported on the
1206    command-line where the parsing is done by the shell.
1207
1208**BC_LINE_LENGTH**
1209
1210:   If this environment variable exists and contains an integer that is greater
1211    than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output
1212    lines to that length, including the backslash (**\\**). The default line
1213    length is **70**.
1214
1215    The special value of **0** will disable line length checking and print
1216    numbers without regard to line length and without backslashes and newlines.
1217
1218**BC_BANNER**
1219
1220:   If this environment variable exists and contains an integer, then a non-zero
1221    value activates the copyright banner when bc(1) is in interactive mode,
1222    while zero deactivates it.
1223
1224    If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section),
1225    then this environment variable has no effect because bc(1) does not print
1226    the banner when not in interactive mode.
1227
1228    This environment variable overrides the default, which can be queried with
1229    the **-h** or **-\-help** options.
1230
1231**BC_SIGINT_RESET**
1232
1233:   If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section),
1234    then this environment variable has no effect because bc(1) exits on
1235    **SIGINT** when not in interactive mode.
1236
1237    However, when bc(1) is in interactive mode, then if this environment
1238    variable exists and contains an integer, a non-zero value makes bc(1) reset
1239    on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this
1240    environment variable exists and is *not* an integer, then bc(1) will exit on
1241    **SIGINT**.
1242
1243    This environment variable overrides the default, which can be queried with
1244    the **-h** or **-\-help** options.
1245
1246**BC_TTY_MODE**
1247
1248:   If TTY mode is *not* available (see the **TTY MODE** section), then this
1249    environment variable has no effect.
1250
1251    However, when TTY mode is available, then if this environment variable
1252    exists and contains an integer, then a non-zero value makes bc(1) use TTY
1253    mode, and zero makes bc(1) not use TTY mode.
1254
1255    This environment variable overrides the default, which can be queried with
1256    the **-h** or **-\-help** options.
1257
1258**BC_PROMPT**
1259
1260:   If TTY mode is *not* available (see the **TTY MODE** section), then this
1261    environment variable has no effect.
1262
1263    However, when TTY mode is available, then if this environment variable
1264    exists and contains an integer, a non-zero value makes bc(1) use a prompt,
1265    and zero or a non-integer makes bc(1) not use a prompt. If this environment
1266    variable does not exist and **BC_TTY_MODE** does, then the value of the
1267    **BC_TTY_MODE** environment variable is used.
1268
1269    This environment variable and the **BC_TTY_MODE** environment variable
1270    override the default, which can be queried with the **-h** or **-\-help**
1271    options.
1272
1273**BC_EXPR_EXIT**
1274
1275:   If any expressions or expression files are given on the command-line with
1276    **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment
1277    variable exists and contains an integer, a non-zero value makes bc(1) exit
1278    after executing the expressions and expression files, and a zero value makes
1279    bc(1) not exit.
1280
1281    This environment variable overrides the default, which can be queried with
1282    the **-h** or **-\-help** options.
1283
1284**BC_DIGIT_CLAMP**
1285
1286:   When parsing numbers and if this environment variable exists and contains an
1287    integer, a non-zero value makes bc(1) clamp digits that are greater than or
1288    equal to the current **ibase** so that all such digits are considered equal
1289    to the **ibase** minus 1, and a zero value disables such clamping so that
1290    those digits are always equal to their value, which is multiplied by the
1291    power of the **ibase**.
1292
1293    This never applies to single-digit numbers, as per the standard (see the
1294    **STANDARDS** section).
1295
1296    This environment variable overrides the default, which can be queried with
1297    the **-h** or **-\-help** options.
1298
1299# EXIT STATUS
1300
1301bc(1) returns the following exit statuses:
1302
1303**0**
1304
1305:   No error.
1306
1307**1**
1308
1309:   A math error occurred. This follows standard practice of using **1** for
1310    expected errors, since math errors will happen in the process of normal
1311    execution.
1312
1313    Math errors include divide by **0**, taking the square root of a negative
1314    number, attempting to convert a negative number to a hardware integer,
1315    overflow when converting a number to a hardware integer, overflow when
1316    calculating the size of a number, and attempting to use a non-integer where
1317    an integer is required.
1318
1319    Converting to a hardware integer happens for the second operand of the power
1320    (**\^**) operator and the corresponding assignment operator.
1321
1322**2**
1323
1324:   A parse error occurred.
1325
1326    Parse errors include unexpected **EOF**, using an invalid character, failing
1327    to find the end of a string or comment, using a token where it is invalid,
1328    giving an invalid expression, giving an invalid print statement, giving an
1329    invalid function definition, attempting to assign to an expression that is
1330    not a named expression (see the *Named Expressions* subsection of the
1331    **SYNTAX** section), giving an invalid **auto** list, having a duplicate
1332    **auto**/function parameter, failing to find the end of a code block,
1333    attempting to return a value from a **void** function, attempting to use a
1334    variable as a reference, and using any extensions when the option **-s** or
1335    any equivalents were given.
1336
1337**3**
1338
1339:   A runtime error occurred.
1340
1341    Runtime errors include assigning an invalid number to any global (**ibase**,
1342    **obase**, or **scale**), giving a bad expression to a **read()** call,
1343    calling **read()** inside of a **read()** call, type errors, passing the
1344    wrong number of arguments to functions, attempting to call an undefined
1345    function, and attempting to use a **void** function call as a value in an
1346    expression.
1347
1348**4**
1349
1350:   A fatal error occurred.
1351
1352    Fatal errors include memory allocation errors, I/O errors, failing to open
1353    files, attempting to use files that do not have only ASCII characters (bc(1)
1354    only accepts ASCII characters), attempting to open a directory as a file,
1355    and giving invalid command-line options.
1356
1357The exit status **4** is special; when a fatal error occurs, bc(1) always exits
1358and returns **4**, no matter what mode bc(1) is in.
1359
1360The other statuses will only be returned when bc(1) is not in interactive mode
1361(see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the
1362**RESET** section) and accepts more input when one of those errors occurs in
1363interactive mode. This is also the case when interactive mode is forced by the
1364**-i** flag or **-\-interactive** option.
1365
1366These exit statuses allow bc(1) to be used in shell scripting with error
1367checking, and its normal behavior can be forced by using the **-i** flag or
1368**-\-interactive** option.
1369
1370# INTERACTIVE MODE
1371
1372Per the standard (see the **STANDARDS** section), bc(1) has an interactive mode
1373and a non-interactive mode. Interactive mode is turned on automatically when
1374both **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and
1375**-\-interactive** option can turn it on in other situations.
1376
1377In interactive mode, bc(1) attempts to recover from errors (see the **RESET**
1378section), and in normal execution, flushes **stdout** as soon as execution is
1379done for the current input. bc(1) may also reset on **SIGINT** instead of exit,
1380depending on the contents of, or default for, the **BC_SIGINT_RESET**
1381environment variable (see the **ENVIRONMENT VARIABLES** section).
1382
1383# TTY MODE
1384
1385If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY
1386mode" is considered to be available, and thus, bc(1) can turn on TTY mode,
1387subject to some settings.
1388
1389If there is the environment variable **BC_TTY_MODE** in the environment (see the
1390**ENVIRONMENT VARIABLES** section), then if that environment variable contains a
1391non-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and
1392**stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment
1393variable exists but is *not* a non-zero integer, then bc(1) will not turn TTY
1394mode on.
1395
1396If the environment variable **BC_TTY_MODE** does *not* exist, the default
1397setting is used. The default setting can be queried with the **-h** or
1398**-\-help** options.
1399
1400TTY mode is different from interactive mode because interactive mode is required
1401in the bc(1) standard (see the **STANDARDS** section), and interactive mode
1402requires only **stdin** and **stdout** to be connected to a terminal.
1403
1404## Prompt
1405
1406If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it
1407can be turned on or off with an environment variable: **BC_PROMPT** (see the
1408**ENVIRONMENT VARIABLES** section).
1409
1410If the environment variable **BC_PROMPT** exists and is a non-zero integer, then
1411the prompt is turned on when **stdin**, **stdout**, and **stderr** are connected
1412to a TTY and the **-P** and **-\-no-prompt** options were not used. The read
1413prompt will be turned on under the same conditions, except that the **-R** and
1414**-\-no-read-prompt** options must also not be used.
1415
1416However, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled
1417with the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt**
1418options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT
1419VARIABLES** and **OPTIONS** sections for more details.
1420
1421# SIGNAL HANDLING
1422
1423Sending a **SIGINT** will cause bc(1) to do one of two things.
1424
1425If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or
1426the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES**
1427section), or its default, is either not an integer or it is zero, bc(1) will
1428exit.
1429
1430However, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its
1431default is an integer and non-zero, then bc(1) will stop executing the current
1432input and reset (see the **RESET** section) upon receiving a **SIGINT**.
1433
1434Note that "current input" can mean one of two things. If bc(1) is processing
1435input from **stdin** in interactive mode, it will ask for more input. If bc(1)
1436is processing input from a file in interactive mode, it will stop processing the
1437file and start processing the next file, if one exists, or ask for input from
1438**stdin** if no other file exists.
1439
1440This means that if a **SIGINT** is sent to bc(1) as it is executing a file, it
1441can seem as though bc(1) did not respond to the signal since it will immediately
1442start executing the next file. This is by design; most files that users execute
1443when interacting with bc(1) have function definitions, which are quick to parse.
1444If a file takes a long time to execute, there may be a bug in that file. The
1445rest of the files could still be executed without problem, allowing the user to
1446continue.
1447
1448**SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the
1449default handler for all other signals.
1450
1451# LOCALES
1452
1453This bc(1) ships with support for adding error messages for different locales
1454and thus, supports **LC_MESSAGES**.
1455
1456# SEE ALSO
1457
1458dc(1)
1459
1460# STANDARDS
1461
1462bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification
1463at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The
1464flags **-efghiqsvVw**, all long options, and the extensions noted above are
1465extensions to that specification.
1466
1467In addition, the behavior of the **quit** implements an interpretation of that
1468specification that is different from all known implementations. For more
1469information see the **Statements** subsection of the **SYNTAX** section.
1470
1471Note that the specification explicitly says that bc(1) only accepts numbers that
1472use a period (**.**) as a radix point, regardless of the value of
1473**LC_NUMERIC**.
1474
1475This bc(1) supports error messages for different locales, and thus, it supports
1476**LC_MESSAGES**.
1477
1478# BUGS
1479
1480Before version **6.1.0**, this bc(1) had incorrect behavior for the **quit**
1481statement.
1482
1483No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc .
1484
1485# AUTHORS
1486
1487Gavin D. Howard <gavin@gavinhoward.com> and contributors.
1488