| 1 | .RP |
| 2 | .TL |
| 3 | BC \- An Arbitrary Precision Desk-Calculator Language |
| 4 | .AU |
| 5 | Lorinda Cherry |
| 6 | .AU |
| 7 | Robert Morris |
| 8 | .AI |
| 9 | .MH |
| 10 | .AB |
| 11 | BC is a language and a compiler for doing arbitrary precision arithmetic |
| 12 | on the PDP-11 under the |
| 13 | .UX |
| 14 | time-sharing |
| 15 | system. The output of the compiler is interpreted and executed by |
| 16 | a collection of routines which can input, output, and do |
| 17 | arithmetic on indefinitely large integers and on scaled fixed-point |
| 18 | numbers. |
| 19 | .PP |
| 20 | These routines are themselves based on a dynamic storage allocator. |
| 21 | Overflow does not occur until all available core storage |
| 22 | is exhausted. |
| 23 | .PP |
| 24 | The language has a complete control structure as well as immediate-mode |
| 25 | operation. Functions can be defined and saved for later execution. |
| 26 | .PP |
| 27 | Two five hundred-digit numbers can be multiplied to give a |
| 28 | thousand digit result in about ten seconds. |
| 29 | .PP |
| 30 | A small collection of library functions is also available, |
| 31 | including sin, cos, arctan, log, exponential, and Bessel functions of |
| 32 | integer order. |
| 33 | .PP |
| 34 | Some of the uses of this compiler are |
| 35 | .IP \- |
| 36 | to do computation with large integers, |
| 37 | .IP \- |
| 38 | to do computation accurate to many decimal places, |
| 39 | .IP \- |
| 40 | conversion of numbers from one base to another base. |
| 41 | .AE |
| 42 | .PP |
| 43 | .SH |
| 44 | Introduction |
| 45 | .PP |
| 46 | BC is a language and a compiler for doing arbitrary precision |
| 47 | arithmetic on the |
| 48 | .UX |
| 49 | time-sharing system [1]. |
| 50 | The compiler was written to make conveniently available a |
| 51 | collection of routines (called DC [5]) which are capable of doing |
| 52 | arithmetic on integers of arbitrary size. The compiler |
| 53 | is by no means intended to provide a complete programming |
| 54 | language. |
| 55 | It is a minimal language facility. |
| 56 | .PP |
| 57 | There is a scaling provision that permits the |
| 58 | use of decimal point notation. |
| 59 | Provision is made for input and output in bases other than |
| 60 | decimal. Numbers can be converted from decimal to octal by |
| 61 | simply setting the output base to equal 8. |
| 62 | .PP |
| 63 | The actual limit on the number of digits that can |
| 64 | be handled depends on the amount of storage available on the machine. |
| 65 | Manipulation of numbers with many hundreds of digits |
| 66 | is possible even on the smallest versions of |
| 67 | .UX . |
| 68 | .PP |
| 69 | The syntax of BC has been deliberately selected to agree |
| 70 | substantially with the C language [2]. Those who |
| 71 | are familiar with C will find few surprises in this language. |
| 72 | .SH |
| 73 | Simple Computations with Integers |
| 74 | .PP |
| 75 | The simplest kind of statement is an arithmetic expression |
| 76 | on a line by itself. |
| 77 | For instance, if you type in the line: |
| 78 | .DS |
| 79 | 142857 + 285714 |
| 80 | .DE |
| 81 | the program responds immediately with the line |
| 82 | .DS |
| 83 | 428571 |
| 84 | .DE |
| 85 | The operators \-, *, /, %, and ^ can also be used; they |
| 86 | indicate subtraction, multiplication, division, remaindering, and |
| 87 | exponentiation, respectively. Division of integers produces an |
| 88 | integer result truncated toward zero. |
| 89 | Division by zero produces an error |
| 90 | comment. |
| 91 | .PP |
| 92 | Any term in an expression may be prefixed by a minus sign to |
| 93 | indicate that it is to be negated (the `unary' minus sign). |
| 94 | The expression |
| 95 | .DS |
| 96 | 7+\-3 |
| 97 | .DE |
| 98 | is interpreted to mean that \-3 is to be added to 7. |
| 99 | .PP |
| 100 | More complex expressions with several operators and with |
| 101 | parentheses are interpreted just as in |
| 102 | Fortran, with ^ having the greatest binding |
| 103 | power, then * and % and /, and finally + and \-. |
| 104 | Contents of parentheses are evaluated before material |
| 105 | outside the parentheses. |
| 106 | Exponentiations are |
| 107 | performed from right to left and the other operators |
| 108 | from left to right. |
| 109 | The two expressions |
| 110 | .DS |
| 111 | a^b^c and a^(b^c) |
| 112 | .DE |
| 113 | are equivalent, as are the two expressions |
| 114 | .DS |
| 115 | a*b*c and (a*b)*c |
| 116 | .DE |
| 117 | BC shares with Fortran and C the undesirable convention that |
| 118 | .DS |
| 119 | a/b*c is equivalent to (a/b)*c |
| 120 | .DE |
| 121 | .PP |
| 122 | Internal storage registers to hold numbers have single lower-case |
| 123 | letter names. The value of an expression can be assigned to |
| 124 | a register in the usual way. The statement |
| 125 | .DS |
| 126 | x = x + 3 |
| 127 | .DE |
| 128 | has the effect of increasing by three the value of the contents of the |
| 129 | register named x. |
| 130 | When, as in this case, the outermost operator is an =, the |
| 131 | assignment is performed but the result is not printed. |
| 132 | Only 26 of these named storage registers are available. |
| 133 | .PP |
| 134 | There is a built-in square root function whose |
| 135 | result is truncated to an integer (but see scaling below). |
| 136 | The lines |
| 137 | .DS |
| 138 | x = sqrt(191) |
| 139 | x |
| 140 | .DE |
| 141 | produce the printed result |
| 142 | .DS |
| 143 | 13 |
| 144 | .DE |
| 145 | .SH |
| 146 | Bases |
| 147 | .PP |
| 148 | There are special internal quantities, called `ibase' and `obase'. |
| 149 | The contents of `ibase', initially set to 10, |
| 150 | determines the base used for interpreting numbers read in. |
| 151 | For example, the lines |
| 152 | .DS |
| 153 | ibase = 8 |
| 154 | 11 |
| 155 | .DE |
| 156 | will produce the output line |
| 157 | .DS |
| 158 | 9 |
| 159 | .DE |
| 160 | and you are all set up to do octal to decimal conversions. |
| 161 | Beware, however of trying to change the input base back |
| 162 | to decimal by typing |
| 163 | .DS |
| 164 | ibase = 10 |
| 165 | .DE |
| 166 | Because the number 10 is interpreted as octal, this statement will |
| 167 | have no effect. |
| 168 | For those who deal in hexadecimal notation, |
| 169 | the characters A\-F are permitted in numbers |
| 170 | (no matter what base is in effect) |
| 171 | and are |
| 172 | interpreted as digits having values 10\-15 respectively. |
| 173 | The statement |
| 174 | .DS |
| 175 | ibase = A |
| 176 | .DE |
| 177 | will change you back to decimal input base no matter what the |
| 178 | current input base is. |
| 179 | Negative and large positive input bases are |
| 180 | permitted but useless. |
| 181 | No mechanism has been provided for the input of arbitrary |
| 182 | numbers in bases less than 1 and greater than 16. |
| 183 | .PP |
| 184 | The contents of `obase', initially set to 10, are used as the base for output |
| 185 | numbers. The lines |
| 186 | .DS |
| 187 | obase = 16 |
| 188 | 1000 |
| 189 | .DE |
| 190 | will produce the output line |
| 191 | .DS |
| 192 | 3E8 |
| 193 | .DE |
| 194 | which is to be interpreted as a 3-digit hexadecimal number. |
| 195 | Very large output bases are permitted, and they are sometimes useful. |
| 196 | For example, large numbers can be output in groups of five digits |
| 197 | by setting `obase' to 100000. |
| 198 | Strange (i.e. 1, 0, or negative) output bases are |
| 199 | handled appropriately. |
| 200 | .PP |
| 201 | Very large numbers are split across lines with 70 characters per line. |
| 202 | Lines which are continued end with \\. |
| 203 | Decimal output conversion is practically instantaneous, but output |
| 204 | of very large numbers (i.e., more than 100 digits) with other bases |
| 205 | is rather slow. |
| 206 | Non-decimal output conversion of |
| 207 | a one hundred digit number takes about |
| 208 | three seconds. |
| 209 | .PP |
| 210 | It is best to remember that `ibase' and `obase' have no effect |
| 211 | whatever on the course of internal computation or |
| 212 | on the evaluation of expressions, but only affect input and |
| 213 | output conversion, respectively. |
| 214 | .SH |
| 215 | Scaling |
| 216 | .PP |
| 217 | A third special internal quantity called `scale' is |
| 218 | used to determine the scale of calculated |
| 219 | quantities. |
| 220 | Numbers may have |
| 221 | up to 99 decimal digits after the decimal point. |
| 222 | This fractional part is retained in further computations. |
| 223 | We refer to the number of digits after the decimal point of |
| 224 | a number as its scale. |
| 225 | .PP |
| 226 | When two scaled numbers are combined by |
| 227 | means of one of the arithmetic operations, the result |
| 228 | has a scale determined by the following rules. For |
| 229 | addition and subtraction, the scale of the result is the larger |
| 230 | of the scales of the two operands. In this case, |
| 231 | there is never any truncation of the result. |
| 232 | For multiplications, the scale of the result is never |
| 233 | less than the maximum of the two scales of the operands, |
| 234 | never more than the sum of the scales of the operands |
| 235 | and, subject to those two restrictions, |
| 236 | the scale of the result is set equal to the contents of the internal |
| 237 | quantity `scale'. |
| 238 | The scale of a quotient is the contents of the internal |
| 239 | quantity `scale'. The scale of a remainder is |
| 240 | the sum of the scales of the quotient and the divisor. |
| 241 | The result of an exponentiation is scaled as if |
| 242 | the implied multiplications were performed. |
| 243 | An exponent must be an integer. |
| 244 | The scale of a square root is set to the maximum of the scale |
| 245 | of the argument and the contents of `scale'. |
| 246 | .PP |
| 247 | All of the internal operations are actually carried out in terms |
| 248 | of integers, with digits being discarded when necessary. |
| 249 | In every case where digits are discarded, truncation and |
| 250 | not rounding is performed. |
| 251 | .PP |
| 252 | The contents of |
| 253 | `scale' must be no greater than |
| 254 | 99 and no less than 0. It is initially set to 0. |
| 255 | In case you need more than 99 fraction digits, you may arrange |
| 256 | your own scaling. |
| 257 | .PP |
| 258 | The internal quantities `scale', `ibase', and `obase' can be |
| 259 | used in expressions just like other variables. |
| 260 | The line |
| 261 | .DS |
| 262 | scale = scale + 1 |
| 263 | .DE |
| 264 | increases the value of `scale' by one, and the line |
| 265 | .DS |
| 266 | scale |
| 267 | .DE |
| 268 | causes the current value of `scale' to be printed. |
| 269 | .PP |
| 270 | The value of `scale' retains its meaning as a |
| 271 | number of decimal digits to be retained in internal |
| 272 | computation even when `ibase' or `obase' are not equal to 10. |
| 273 | The internal computations (which are still conducted in decimal, |
| 274 | regardless of the bases) are performed to the specified number |
| 275 | of decimal digits, never hexadecimal or octal or any |
| 276 | other kind of digits. |
| 277 | .SH |
| 278 | Functions |
| 279 | .PP |
| 280 | The name of a function is a single lower-case letter. |
| 281 | Function names are permitted to collide with simple |
| 282 | variable names. |
| 283 | Twenty-six different defined functions are permitted |
| 284 | in addition to the twenty-six variable names. |
| 285 | The line |
| 286 | .DS |
| 287 | define a(x){ |
| 288 | .DE |
| 289 | begins the definition of a function with one argument. |
| 290 | This line must be followed by one or more statements, |
| 291 | which make up the body of the function, ending |
| 292 | with a right brace }. |
| 293 | Return of control from a function occurs when a return |
| 294 | statement is executed or when the end of the function is reached. |
| 295 | The return statement can take either |
| 296 | of the two forms |
| 297 | .DS |
| 298 | return |
| 299 | return(x) |
| 300 | .DE |
| 301 | In the first case, the value of the function is 0, and in |
| 302 | the second, the value of the expression in parentheses. |
| 303 | .PP |
| 304 | Variables used in the function can be declared as automatic |
| 305 | by a statement of the form |
| 306 | .DS |
| 307 | auto x,y,z |
| 308 | .DE |
| 309 | There can be only one `auto' statement in a function and it must |
| 310 | be the first statement in the definition. |
| 311 | These automatic variables are allocated space and initialized |
| 312 | to zero on entry to the function and thrown away on return. The |
| 313 | values of any variables with the same names outside the function |
| 314 | are not disturbed. |
| 315 | Functions may be called recursively and the automatic variables |
| 316 | at each level of call are protected. |
| 317 | The parameters named in a function definition are treated in |
| 318 | the same way as the automatic variables of that function |
| 319 | with the single exception that they are given a value |
| 320 | on entry to the function. |
| 321 | An example of a function definition is |
| 322 | .DS |
| 323 | define a(x,y){ |
| 324 | auto z |
| 325 | z = x*y |
| 326 | return(z) |
| 327 | } |
| 328 | .DE |
| 329 | The value of this function, when called, will be the |
| 330 | product of its |
| 331 | two arguments. |
| 332 | .PP |
| 333 | A function is called by the appearance of its name |
| 334 | followed by a string of arguments enclosed in |
| 335 | parentheses and separated by commas. |
| 336 | The result |
| 337 | is unpredictable if the wrong number of arguments is used. |
| 338 | .PP |
| 339 | Functions with no arguments are defined and called using |
| 340 | parentheses with nothing between them: b(). |
| 341 | .PP |
| 342 | If the function |
| 343 | .ft I |
| 344 | a |
| 345 | .ft |
| 346 | above has been defined, then the line |
| 347 | .DS |
| 348 | a(7,3.14) |
| 349 | .DE |
| 350 | would cause the result 21.98 to be printed and the line |
| 351 | .DS |
| 352 | x = a(a(3,4),5) |
| 353 | .DE |
| 354 | would cause the value of x to become 60. |
| 355 | .SH |
| 356 | Subscripted Variables |
| 357 | .PP |
| 358 | A single lower-case letter variable name |
| 359 | followed by an expression in brackets is called a subscripted |
| 360 | variable (an array element). |
| 361 | The variable name is called the array name and the expression |
| 362 | in brackets is called the subscript. |
| 363 | Only one-dimensional arrays are |
| 364 | permitted. The names of arrays are permitted to |
| 365 | collide with the names of simple variables and function names. |
| 366 | Any fractional |
| 367 | part of a subscript is discarded before use. |
| 368 | Subscripts must be greater than or equal to zero and |
| 369 | less than or equal to 2047. |
| 370 | .PP |
| 371 | Subscripted variables may be freely used in expressions, in |
| 372 | function calls, and in return statements. |
| 373 | .PP |
| 374 | An array name may be used as an argument to a function, |
| 375 | or may be declared as automatic in |
| 376 | a function definition by the use of empty brackets: |
| 377 | .DS |
| 378 | f(a[\|]) |
| 379 | define f(a[\|]) |
| 380 | auto a[\|] |
| 381 | .DE |
| 382 | When an array name is so used, the whole contents of the array |
| 383 | are copied for the use of the function, and thrown away on exit |
| 384 | from the function. |
| 385 | Array names which refer to whole arrays cannot be used |
| 386 | in any other contexts. |
| 387 | .SH |
| 388 | Control Statements |
| 389 | .PP |
| 390 | The `if', the `while', and the `for' statements |
| 391 | may be used to alter the flow within programs or to cause iteration. |
| 392 | The range of each of them is a statement or |
| 393 | a compound statement consisting of a collection of |
| 394 | statements enclosed in braces. |
| 395 | They are written in the following way |
| 396 | .DS |
| 397 | if(relation) statement |
| 398 | while(relation) statement |
| 399 | for(expression1; relation; expression2) statement |
| 400 | .DE |
| 401 | or |
| 402 | .DS |
| 403 | if(relation) {statements} |
| 404 | while(relation) {statements} |
| 405 | for(expression1; relation; expression2) {statements} |
| 406 | .DE |
| 407 | .PP |
| 408 | A relation in one of the control statements is an expression of the form |
| 409 | .DS |
| 410 | x>y |
| 411 | .DE |
| 412 | where two expressions are related by one of the six relational |
| 413 | operators <, >, <=, >=, ==, or !=. |
| 414 | The relation == |
| 415 | stands for `equal to' and != stands for `not equal to'. |
| 416 | The meaning of the remaining relational operators is |
| 417 | clear. |
| 418 | .PP |
| 419 | BEWARE of using = instead of == in a relational. Unfortunately, |
| 420 | both of them are legal, so you will not get a diagnostic |
| 421 | message, but = really will not do a comparison. |
| 422 | .PP |
| 423 | The `if' statement causes execution of its range |
| 424 | if and only if the relation is true. |
| 425 | Then control passes to the next statement in sequence. |
| 426 | .PP |
| 427 | The `while' statement causes execution of its range |
| 428 | repeatedly as long as the relation |
| 429 | is true. The relation is tested before each execution |
| 430 | of its range and if the relation |
| 431 | is false, control passes to the next statement beyond the range |
| 432 | of the while. |
| 433 | .PP |
| 434 | The `for' statement begins |
| 435 | by executing `expression1'. Then the relation is tested |
| 436 | and, if true, the statements in the range of the `for' are executed. |
| 437 | Then `expression2' is executed. The relation is tested, and so on. |
| 438 | The typical use of the `for' statement is for a controlled iteration, |
| 439 | as in the statement |
| 440 | .DS |
| 441 | for(i=1; i<=10; i=i+1) i |
| 442 | .DE |
| 443 | which will print the integers from 1 to 10. |
| 444 | Here are some examples of the use of the control statements. |
| 445 | .DS |
| 446 | define f(n){ |
| 447 | auto i, x |
| 448 | x=1 |
| 449 | for(i=1; i<=n; i=i+1) x=x*i |
| 450 | return(x) |
| 451 | } |
| 452 | .DE |
| 453 | The line |
| 454 | .DS |
| 455 | f(a) |
| 456 | .DE |
| 457 | will print |
| 458 | .ft I |
| 459 | a |
| 460 | .ft |
| 461 | factorial if |
| 462 | .ft I |
| 463 | a |
| 464 | .ft |
| 465 | is a positive integer. |
| 466 | Here is the definition of a function which will |
| 467 | compute values of the binomial coefficient |
| 468 | (m and n are assumed to be positive integers). |
| 469 | .DS |
| 470 | define b(n,m){ |
| 471 | auto x, j |
| 472 | x=1 |
| 473 | for(j=1; j<=m; j=j+1) x=x*(n\-j+1)/j |
| 474 | return(x) |
| 475 | } |
| 476 | .DE |
| 477 | The following function computes values of the exponential function |
| 478 | by summing the appropriate series |
| 479 | without regard for possible truncation errors: |
| 480 | .DS |
| 481 | scale = 20 |
| 482 | define e(x){ |
| 483 | auto a, b, c, d, n |
| 484 | a = 1 |
| 485 | b = 1 |
| 486 | c = 1 |
| 487 | d = 0 |
| 488 | n = 1 |
| 489 | while(1==1){ |
| 490 | a = a*x |
| 491 | b = b*n |
| 492 | c = c + a/b |
| 493 | n = n + 1 |
| 494 | if(c==d) return(c) |
| 495 | d = c |
| 496 | } |
| 497 | } |
| 498 | .DE |
| 499 | .SH |
| 500 | Some Details |
| 501 | .PP |
| 502 | There are some language features that every user should know |
| 503 | about even if he will not use them. |
| 504 | .PP |
| 505 | Normally statements are typed one to a line. It is also permissible |
| 506 | to type several statements on a line separated by semicolons. |
| 507 | .PP |
| 508 | If an assignment statement is parenthesized, it then has |
| 509 | a value and it can be used anywhere that an expression can. |
| 510 | For example, the line |
| 511 | .DS |
| 512 | (x=y+17) |
| 513 | .DE |
| 514 | not only makes the indicated assignment, but also prints the |
| 515 | resulting value. |
| 516 | .PP |
| 517 | Here is an example of a use of the value of an |
| 518 | assignment statement even when it is not parenthesized. |
| 519 | .DS |
| 520 | x = a[i=i+1] |
| 521 | .DE |
| 522 | causes a value to be assigned to x and also increments i |
| 523 | before it is used as a subscript. |
| 524 | .PP |
| 525 | The following constructs work in BC in exactly the same manner |
| 526 | as they do in the C language. Consult the appendix or the |
| 527 | C manuals [2] for their exact workings. |
| 528 | .DS |
| 529 | .ta 2i |
| 530 | x=y=z is the same as x=(y=z) |
| 531 | x =+ y x = x+y |
| 532 | x =\- y x = x\-y |
| 533 | x =* y x = x*y |
| 534 | x =/ y x = x/y |
| 535 | x =% y x = x%y |
| 536 | x =^ y x = x^y |
| 537 | x++ (x=x+1)\-1 |
| 538 | x\-\- (x=x\-1)+1 |
| 539 | ++x x = x+1 |
| 540 | \-\-x x = x\-1 |
| 541 | .DE |
| 542 | Even if you don't intend to use the constructs, |
| 543 | if you type one inadvertently, something correct but unexpected |
| 544 | may happen. |
| 545 | .PP |
| 546 | WARNING! In some of these constructions, spaces are |
| 547 | significant. |
| 548 | There is a real difference between |
| 549 | x=\-y and x= \-y. |
| 550 | The first replaces x by x\-y and the second by \-y. |
| 551 | .SH |
| 552 | Three Important Things |
| 553 | .PP |
| 554 | 1. To exit a BC program, type `quit'. |
| 555 | .PP |
| 556 | 2. There is a comment convention identical to that of C and |
| 557 | of PL/I. Comments begin with `/*' and end with `*/'. |
| 558 | .PP |
| 559 | 3. There is a library of math functions which may be obtained by |
| 560 | typing at command level |
| 561 | .DS |
| 562 | bc \-l |
| 563 | .DE |
| 564 | This command will load a set of library functions |
| 565 | which, at the time of writing, consists of sine (named `s'), |
| 566 | cosine (`c'), arctangent (`a'), natural logarithm (`l'), |
| 567 | exponential (`e') and Bessel functions of integer order (`j(n,x)'). Doubtless more functions will be added |
| 568 | in time. |
| 569 | The library sets the scale to 20. You can reset it to something |
| 570 | else if you like. |
| 571 | The design of these mathematical library routines |
| 572 | is discussed elsewhere [3]. |
| 573 | .PP |
| 574 | If you type |
| 575 | .DS |
| 576 | bc file ... |
| 577 | .DE |
| 578 | BC will read and execute the named file or files before accepting |
| 579 | commands from the keyboard. In this way, you may load your |
| 580 | favorite programs and function definitions. |
| 581 | .SH |
| 582 | Acknowledgement |
| 583 | .PP |
| 584 | The compiler is written in YACC [4]; its original |
| 585 | version was written by S. C. Johnson. |
| 586 | .SH |
| 587 | References |
| 588 | .IP [1] |
| 589 | K. Thompson and D. M. Ritchie, |
| 590 | .ft I |
| 591 | UNIX Programmer's Manual, |
| 592 | .ft |
| 593 | Bell Laboratories, |
| 594 | 1978. |
| 595 | .IP [2] |
| 596 | B. W. Kernighan and |
| 597 | D. M. Ritchie, |
| 598 | .ft I |
| 599 | The C Programming Language, |
| 600 | .ft |
| 601 | Prentice-Hall, 1978. |
| 602 | .IP [3] |
| 603 | R. Morris, |
| 604 | .ft I |
| 605 | A Library of Reference Standard Mathematical Subroutines, |
| 606 | .ft |
| 607 | Bell Laboratories internal memorandum, 1975. |
| 608 | .IP [4] |
| 609 | S. C. Johnson, |
| 610 | .ft I |
| 611 | YACC \(em Yet Another Compiler-Compiler. |
| 612 | .ft |
| 613 | Bell Laboratories Computing Science Technical Report #32, 1978. |
| 614 | .IP [5] |
| 615 | R. Morris and L. L. Cherry, |
| 616 | .ft I |
| 617 | DC \- An Interactive Desk Calculator. |
| 618 | .ft |
| 619 | .LP |
| 620 | .bp |
| 621 | .ft B |
| 622 | .DS C |
| 623 | Appendix |
| 624 | .DE |
| 625 | .ft |
| 626 | .NH |
| 627 | Notation |
| 628 | .PP |
| 629 | In the following pages syntactic categories are in \fIitalics\fP; |
| 630 | literals are in \fBbold\fP; material in brackets [\|] is optional. |
| 631 | .NH |
| 632 | Tokens |
| 633 | .PP |
| 634 | Tokens consist of keywords, identifiers, constants, operators, |
| 635 | and separators. |
| 636 | Token separators may be blanks, tabs or comments. |
| 637 | Newline characters or semicolons separate statements. |
| 638 | .NH 2 |
| 639 | Comments |
| 640 | .PP |
| 641 | Comments are introduced by the characters /* and terminated by |
| 642 | */. |
| 643 | .NH 2 |
| 644 | Identifiers |
| 645 | .PP |
| 646 | There are three kinds of identifiers \- ordinary identifiers, array identifiers |
| 647 | and function identifiers. |
| 648 | All three types consist of single lower-case letters. |
| 649 | Array identifiers are followed by square brackets, possibly |
| 650 | enclosing an expression describing a subscript. |
| 651 | Arrays are singly dimensioned and may contain up to 2048 |
| 652 | elements. |
| 653 | Indexing begins at zero so an array may be indexed from 0 to 2047. |
| 654 | Subscripts are truncated to integers. |
| 655 | Function identifiers are followed by parentheses, possibly enclosing arguments. |
| 656 | The three types of identifiers do not conflict; |
| 657 | a program can have a variable named \fBx\fP, |
| 658 | an array named \fBx\fP and a function named \fBx\fP, all of which are separate and |
| 659 | distinct. |
| 660 | .NH 2 |
| 661 | Keywords |
| 662 | .PP |
| 663 | The following are reserved keywords: |
| 664 | .ft B |
| 665 | .ta .5i 1.0i |
| 666 | .nf |
| 667 | ibase if |
| 668 | obase break |
| 669 | scale define |
| 670 | sqrt auto |
| 671 | length return |
| 672 | while quit |
| 673 | for |
| 674 | .fi |
| 675 | .ft |
| 676 | .NH 2 |
| 677 | Constants |
| 678 | .PP |
| 679 | Constants consist of arbitrarily long numbers |
| 680 | with an optional decimal point. |
| 681 | The hexadecimal digits \fBA\fP\-\fBF\fP are also recognized as digits with |
| 682 | values 10\-15, respectively. |
| 683 | .NH 1 |
| 684 | Expressions |
| 685 | .PP |
| 686 | The value of an expression is printed unless the main |
| 687 | operator is an assignment. |
| 688 | Precedence is the same as the order |
| 689 | of presentation here, with highest appearing first. |
| 690 | Left or right associativity, where applicable, is |
| 691 | discussed with each operator. |
| 692 | .bp |
| 693 | .NH 2 |
| 694 | Primitive expressions |
| 695 | .NH 3 |
| 696 | Named expressions |
| 697 | .PP |
| 698 | Named expressions are |
| 699 | places where values are stored. |
| 700 | Simply stated, |
| 701 | named expressions are legal on the left |
| 702 | side of an assignment. |
| 703 | The value of a named expression is the value stored in the place named. |
| 704 | .NH 4 |
| 705 | \fIidentifiers\fR |
| 706 | .PP |
| 707 | Simple identifiers are named expressions. |
| 708 | They have an initial value of zero. |
| 709 | .NH 4 |
| 710 | \fIarray-name\fP\|[\|\fIexpression\fP\|] |
| 711 | .PP |
| 712 | Array elements are named expressions. |
| 713 | They have an initial value of zero. |
| 714 | .NH 4 |
| 715 | \fBscale\fR, \fBibase\fR and \fBobase\fR |
| 716 | .PP |
| 717 | The internal registers |
| 718 | \fBscale\fP, \fBibase\fP and \fBobase\fP are all named expressions. |
| 719 | \fBscale\fP is the number of digits after the decimal point to be |
| 720 | retained in arithmetic operations. |
| 721 | \fBscale\fR has an initial value of zero. |
| 722 | \fBibase\fP and \fBobase\fP are the input and output number |
| 723 | radix respectively. |
| 724 | Both \fBibase\fR and \fBobase\fR have initial values of 10. |
| 725 | .NH 3 |
| 726 | Function calls |
| 727 | .NH 4 |
| 728 | \fIfunction-name\fB\|(\fR[\fIexpression\fR\|[\fB,\|\fIexpression\|\fR.\|.\|.\|]\|]\fB) |
| 729 | .PP |
| 730 | A function call consists of a function name followed by parentheses |
| 731 | containing a comma-separated list of |
| 732 | expressions, which are the function arguments. |
| 733 | A whole array passed as an argument is specified by the |
| 734 | array name followed by empty square brackets. |
| 735 | All function arguments are passed by |
| 736 | value. |
| 737 | As a result, changes made to the formal parameters have |
| 738 | no effect on the actual arguments. |
| 739 | If the function terminates by executing a return |
| 740 | statement, the value of the function is |
| 741 | the value of the expression in the parentheses of the return |
| 742 | statement or is zero if no expression is provided |
| 743 | or if there is no return statement. |
| 744 | .NH 4 |
| 745 | sqrt\|(\|\fIexpression\fP\|) |
| 746 | .PP |
| 747 | The result is the square root of the expression. |
| 748 | The result is truncated in the least significant decimal place. |
| 749 | The scale of the result is |
| 750 | the scale of the expression or the |
| 751 | value of |
| 752 | .ft B |
| 753 | scale, |
| 754 | .ft |
| 755 | whichever is larger. |
| 756 | .NH 4 |
| 757 | length\|(\|\fIexpression\fP\|) |
| 758 | .PP |
| 759 | The result is the total number of significant decimal digits in the expression. |
| 760 | The scale of the result is zero. |
| 761 | .NH 4 |
| 762 | scale\|(\|\fIexpression\fP\|) |
| 763 | .PP |
| 764 | The result is the scale of the expression. |
| 765 | The scale of the result is zero. |
| 766 | .NH 3 |
| 767 | Constants |
| 768 | .PP |
| 769 | Constants are primitive expressions. |
| 770 | .NH 3 |
| 771 | Parentheses |
| 772 | .PP |
| 773 | An expression surrounded by parentheses is |
| 774 | a primitive expression. |
| 775 | The parentheses are used to alter the |
| 776 | normal precedence. |
| 777 | .NH 2 |
| 778 | Unary operators |
| 779 | .PP |
| 780 | The unary operators |
| 781 | bind right to left. |
| 782 | .NH 3 |
| 783 | \-\|\fIexpression\fP |
| 784 | .PP |
| 785 | The result is the negative of the expression. |
| 786 | .NH 3 |
| 787 | ++\|\fInamed-expression\fP |
| 788 | .PP |
| 789 | The named expression is |
| 790 | incremented by one. |
| 791 | The result is the value of the named expression after |
| 792 | incrementing. |
| 793 | .NH 3 |
| 794 | \-\-\|\fInamed-expression\fP |
| 795 | .PP |
| 796 | The named expression is |
| 797 | decremented by one. |
| 798 | The result is the value of the named expression after |
| 799 | decrementing. |
| 800 | .NH 3 |
| 801 | \fInamed-expression\fP\|++ |
| 802 | .PP |
| 803 | The named expression is |
| 804 | incremented by one. |
| 805 | The result is the value of the named expression before |
| 806 | incrementing. |
| 807 | .NH 3 |
| 808 | \fInamed-expression\fP\|\-\- |
| 809 | .PP |
| 810 | The named expression is |
| 811 | decremented by one. |
| 812 | The result is the value of the named expression before |
| 813 | decrementing. |
| 814 | .NH 2 |
| 815 | Exponentiation operator |
| 816 | .PP |
| 817 | The exponentiation operator binds right to left. |
| 818 | .NH 3 |
| 819 | \fIexpression\fP ^ \fIexpression\fP |
| 820 | .PP |
| 821 | The result is the first |
| 822 | expression raised to the power of the |
| 823 | second expression. |
| 824 | The second expression must be an integer. |
| 825 | If \fIa\fP |
| 826 | is the scale of the left expression |
| 827 | and \fIb\fP is the absolute value |
| 828 | of the right expression, |
| 829 | then the scale of the result is: |
| 830 | .PP |
| 831 | min\|(\|\fIa\(mub\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP\|)\|) |
| 832 | .NH 2 |
| 833 | Multiplicative operators |
| 834 | .PP |
| 835 | The operators *, /, % bind left to right. |
| 836 | .NH 3 |
| 837 | \fIexpression\fP * \fIexpression\fP |
| 838 | .PP |
| 839 | The result is the product |
| 840 | of the two expressions. |
| 841 | If \fIa\fP and \fIb\fP are the |
| 842 | scales of the two expressions, |
| 843 | then the scale of the result is: |
| 844 | .PP |
| 845 | min\|(\|\fIa+b\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP,\|\fIb\fP\|)\|) |
| 846 | .NH 3 |
| 847 | \fIexpression\fP / \fIexpression\fP |
| 848 | .PP |
| 849 | The result is the quotient of the two expressions. |
| 850 | The scale of the result is the value of \fBscale\fR. |
| 851 | .NH 3 |
| 852 | \fIexpression\fP % \fIexpression\fP |
| 853 | .PP |
| 854 | The % operator produces the remainder of the division |
| 855 | of the two expressions. |
| 856 | More precisely, |
| 857 | \fIa\fP%\fIb\fP is \fIa\fP\-\fIa\fP/\fIb\fP*\fIb\fP. |
| 858 | .PP |
| 859 | The scale of the result is the sum of the scale of |
| 860 | the divisor and the value of |
| 861 | .ft B |
| 862 | scale |
| 863 | .ft |
| 864 | .NH 2 |
| 865 | Additive operators |
| 866 | .PP |
| 867 | The additive operators bind left to right. |
| 868 | .NH 3 |
| 869 | \fIexpression\fP + \fIexpression\fP |
| 870 | .PP |
| 871 | The result is the sum of the two expressions. |
| 872 | The scale of the result is |
| 873 | the maximun of the scales of the expressions. |
| 874 | .NH 3 |
| 875 | \fIexpression\fP \- \fIexpression\fP |
| 876 | .PP |
| 877 | The result is the difference of the two expressions. |
| 878 | The scale of the result is the |
| 879 | maximum of the scales of the expressions. |
| 880 | .NH 2 |
| 881 | assignment operators |
| 882 | .PP |
| 883 | The assignment operators bind right to left. |
| 884 | .NH 3 |
| 885 | \fInamed-expression\fP = \fIexpression\fP |
| 886 | .PP |
| 887 | This expression results in assigning the value of the expression |
| 888 | on the right |
| 889 | to the named expression on the left. |
| 890 | .NH 3 |
| 891 | \fInamed-expression\fP =+ \fIexpression\fP |
| 892 | .NH 3 |
| 893 | \fInamed-expression\fP =\- \fIexpression\fP |
| 894 | .NH 3 |
| 895 | \fInamed-expression\fP =* \fIexpression\fP |
| 896 | .NH 3 |
| 897 | \fInamed-expression\fP =/ \fIexpression\fP |
| 898 | .NH 3 |
| 899 | \fInamed-expression\fP =% \fIexpression\fP |
| 900 | .NH 3 |
| 901 | \fInamed-expression\fP =^ \fIexpression\fP |
| 902 | .PP |
| 903 | The result of the above expressions is equivalent |
| 904 | to ``named expression = named expression OP expression'', |
| 905 | where OP is the operator after the = sign. |
| 906 | .NH 1 |
| 907 | Relations |
| 908 | .PP |
| 909 | Unlike all other operators, the relational operators |
| 910 | are only valid as the object of an \fBif\fP, \fBwhile\fP, |
| 911 | or inside a \fBfor\fP statement. |
| 912 | .NH 2 |
| 913 | \fIexpression\fP < \fIexpression\fP |
| 914 | .NH 2 |
| 915 | \fIexpression\fP > \fIexpression\fP |
| 916 | .NH 2 |
| 917 | \fIexpression\fP <= \fIexpression\fP |
| 918 | .NH 2 |
| 919 | \fIexpression\fP >= \fIexpression\fP |
| 920 | .NH 2 |
| 921 | \fIexpression\fP == \fIexpression\fP |
| 922 | .NH 2 |
| 923 | \fIexpression\fP != \fIexpression\fP |
| 924 | .NH 1 |
| 925 | Storage classes |
| 926 | .PP |
| 927 | There are only two storage classes in BC, global and automatic |
| 928 | (local). |
| 929 | Only identifiers that are to be local to a function need be |
| 930 | declared with the \fBauto\fP command. |
| 931 | The arguments to a function |
| 932 | are local to the function. |
| 933 | All other identifiers are assumed to be global |
| 934 | and available to all functions. |
| 935 | All identifiers, global and local, have initial values |
| 936 | of zero. |
| 937 | Identifiers declared as \fBauto\fP are allocated on entry to the function |
| 938 | and released on returning from the function. |
| 939 | They therefore do not retain values between function calls. |
| 940 | \fBauto\fP arrays are specified by the array name followed by empty square brackets. |
| 941 | .PP |
| 942 | Automatic variables in BC do not work in exactly the same way |
| 943 | as in either C or PL/I. On entry to a function, the old values of |
| 944 | the names that appear as parameters and as automatic |
| 945 | variables are pushed onto a stack. |
| 946 | Until return is made from the function, reference to these |
| 947 | names refers only to the new values. |
| 948 | .NH 1 |
| 949 | Statements |
| 950 | .PP |
| 951 | Statements must be separated by semicolon or newline. |
| 952 | Except where altered by control statements, execution |
| 953 | is sequential. |
| 954 | .NH 2 |
| 955 | Expression statements |
| 956 | .PP |
| 957 | When a statement is an expression, unless |
| 958 | the main operator is an assignment, the value |
| 959 | of the expression is printed, followed by a newline character. |
| 960 | .NH 2 |
| 961 | Compound statements |
| 962 | .PP |
| 963 | Statements may be grouped together and used when one statement is expected |
| 964 | by surrounding them with { }. |
| 965 | .NH 2 |
| 966 | Quoted string statements |
| 967 | .PP |
| 968 | "any string" |
| 969 | .sp .5 |
| 970 | This statement prints the string inside the quotes. |
| 971 | .NH 2 |
| 972 | If statements |
| 973 | .sp .5 |
| 974 | \fBif\|(\|\fIrelation\fB\|)\|\fIstatement\fR |
| 975 | .PP |
| 976 | The substatement is executed if the relation is true. |
| 977 | .NH 2 |
| 978 | While statements |
| 979 | .sp .5 |
| 980 | \fBwhile\|(\|\fIrelation\fB\|)\|\fIstatement\fR |
| 981 | .PP |
| 982 | The statement is executed while the relation |
| 983 | is true. |
| 984 | The test occurs before each execution of the statement. |
| 985 | .NH 2 |
| 986 | For statements |
| 987 | .sp .5 |
| 988 | \fBfor\|(\|\fIexpression\fB; \fIrelation\fB; \fIexpression\fB\|)\|\fIstatement\fR |
| 989 | .PP |
| 990 | The for statement is the same as |
| 991 | .nf |
| 992 | .ft I |
| 993 | first-expression |
| 994 | \fBwhile\|(\fPrelation\|\fB) {\fP |
| 995 | statement |
| 996 | last-expression |
| 997 | } |
| 998 | .ft R |
| 999 | .fi |
| 1000 | .PP |
| 1001 | All three expressions must be present. |
| 1002 | .NH 2 |
| 1003 | Break statements |
| 1004 | .sp .5 |
| 1005 | \fBbreak\fP |
| 1006 | .PP |
| 1007 | \fBbreak\fP causes termination of a \fBfor\fP or \fBwhile\fP statement. |
| 1008 | .NH 2 |
| 1009 | Auto statements |
| 1010 | .sp .5 |
| 1011 | \fBauto \fIidentifier\fR\|[\|\fB,\fIidentifier\fR\|] |
| 1012 | .PP |
| 1013 | The auto statement causes the values of the identifiers to be pushed down. |
| 1014 | The identifiers can be ordinary identifiers or array identifiers. |
| 1015 | Array identifiers are specified by following the array name by empty square |
| 1016 | brackets. |
| 1017 | The auto statement must be the first statement |
| 1018 | in a function definition. |
| 1019 | .NH 2 |
| 1020 | Define statements |
| 1021 | .sp .5 |
| 1022 | .nf |
| 1023 | \fBdefine(\|\fR[\fIparameter\|\fR[\fB\|,\|\fIparameter\|.\|.\|.\|\fR]\|]\|\fB)\|{\fI |
| 1024 | statements\|\fB}\fR |
| 1025 | .fi |
| 1026 | .PP |
| 1027 | The define statement defines a function. |
| 1028 | The parameters may |
| 1029 | be ordinary identifiers or array names. |
| 1030 | Array names must be followed by empty square brackets. |
| 1031 | .NH 2 |
| 1032 | Return statements |
| 1033 | .sp .5 |
| 1034 | \fBreturn\fP |
| 1035 | .sp .5 |
| 1036 | \fBreturn(\fI\|expression\|\fB)\fR |
| 1037 | .PP |
| 1038 | The return statement causes termination of a function, |
| 1039 | popping of its auto variables, and |
| 1040 | specifies the result of the function. |
| 1041 | The first form is equivalent to \fBreturn(0)\fR. |
| 1042 | The result of the function is the result of the expression |
| 1043 | in parentheses. |
| 1044 | .NH 2 |
| 1045 | Quit |
| 1046 | .PP |
| 1047 | The quit statement stops execution of a BC program and returns |
| 1048 | control to UNIX when it is first encountered. |
| 1049 | Because it is not treated as an executable statement, |
| 1050 | it cannot be used |
| 1051 | in a function definition or in an |
| 1052 | .ft B |
| 1053 | if, for, |
| 1054 | .ft |
| 1055 | or |
| 1056 | .ft B |
| 1057 | while |
| 1058 | .ft |
| 1059 | statement. |