Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
B (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == The following examples are from the ''Users' Reference to B'' by Ken Thompson:<ref name=bur /> <syntaxhighlight lang="c"> /* The following function will print a non-negative number, n, to the base b, where 2<=b<=10. This routine uses the fact that in the ASCII character set, the digits 0 to 9 have sequential code values. */ printn(n,b) { extrn putchar; auto a; /* Wikipedia note: the auto keyword declares a variable with automatic storage (lifetime is function scope), not "automatic typing" as in C++11. */ if(a=n/b) /* assignment, not test for equality */ printn(a, b); /* recursive */ putchar(n%b + '0'); } </syntaxhighlight> <syntaxhighlight lang="c"> /* The following program will calculate the constant e-2 to about 4000 decimal digits, and print it 50 characters to the line in groups of 5 characters. The method is simple output conver- sion of the expansion 1/2! + 1/3! + ... = .111... where the bases of the digits are 2, 3, 4, ... */ main() { extrn putchar, n, v; auto i, c, col, a; i = col = 0; while(i<n) v[i++] = 1; while(col<2*n) { a = n+1; c = i = 0; while(i<n) { c =+ v[i]*10; v[i++] = c%a; c =/ a--; } putchar(c+'0'); if(!(++col%5)) putchar(col%50?' ':'*n'); } putchar('*n*n'); } v[2000]; n 2000; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)