# bc

- Influenced by:
- Paradigm:
- Typing discipline:
- Versions and implementations (Collapse all | Expand all):

bc (short for bench calculator) is an arbitrary precision calculator language. It has loops and conditional statements, so it is considered to be a simple programming language. It can be used either as an interactive shell or as a scripting language, within shell scripts.

## Elements of syntax:

Inline comments | # |
---|---|

Non-nestable comments | /* ... */ |

Case-sensitivity | Yes |

Variable identifier regexp | [a-z][_a-z0-9]* |

Variable assignment | <varname> = <value> |

Block | { ... } |

Comparison | < > <= >= |

Function definition | define f(p1, p2, ...) ... |

Function call | f(a, b, ...) |

Function call with no parameters | f() |

## Links:

## Examples:

### Hello, World!:

Example for versions bc 1.06```
print "Hello, World!\n";
```

### Factorial:

Example for versions bc 1.06This example uses recursive factorial definition.

```
define factorial(n) {
if (n == 0) return(1);
return(n * factorial(n - 1));
}
for (n = 0; n <= 16; n++) {
print n; "! = "; factorial(n);
}
```

### Fibonacci numbers:

Example for versions bc 1.06This example uses recursive definition of Fibonacci numbers. Note that printing “…” is put into the loop, since both code and output are shown in the same console, and printing “…” after the loop will result in the following output:

`for (n = 1; n <= 16; n++) {`

`print fibonacci(n); ", "`

`}`

`1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, print "..."`

`...`

```
define fibonacci(n) {
if (n <= 2) return(1);
return(fibonacci(n-1)+fibonacci(n-2));
}
for (n = 1; n <= 16; n++) {
print fibonacci(n); ", "
if (n==16) print "..."
}
```

### Fibonacci numbers:

Example for versions bc 1.06This example uses Binet’s formula. Note that bc is an arbitrary-precision calculator, so after floating-point calculations the numbers have to be rounded to integers. This has to be done manually, since bc doesn’t provide built-in rounding.

```
for (n = 1; n <= 16; n++) {
scale = 10
x = (((1 + sqrt(5)) * .5) ^ n - ((1 - sqrt(5)) * .5) ^ n) / sqrt(5)
scale = 0
print (x+0.5)/1; ", "
if (n==16) print "..."
}
```

## Comments

]]>blog comments powered by Disqus

]]>