C in Basthon
Open the console or the notebook in C (URL parameter kernel=c). Examples are in the gallery, and the headers added by Basthon (images, drawing, download) are described on the C: what Basthon adds page.
Each cell (or the script of the console) is compiled to WebAssembly, and the cells share their declarations: variables, functions and types stay usable from one cell to the next. No need for a main function: the statements run directly. The last expression, when it is not followed by a ;, is shown with its type:
#include <stdio.h>
int square(int x) {
return x * x;
}
for (int i = 0; i < 5; i++)
printf("%d ", square(i));
square(12)0 1 4 9 16
(int) 144The classic mistakes are reported: a missing & in a scanf (the cell is then not run), an array overflow, the dereference of a NULL pointer or an infinite recursion (try it, post).
The introduction notebook tours the kernel (try it), and a program sorts an array of structures with qsort (try it).
What you can do
Building images
image_show, declared in <image.h>, shows an image file: PNG or Netpbm image (PBM, PGM or PPM, as text or binary). An image can thus be built pixel by pixel with the functions of <stdio.h> alone:
#include <stdio.h>
#include <image.h>
FILE *f = fopen("gradient.ppm", "w");
fprintf(f, "P3\n256 100\n255\n");
for (int y = 0; y < 100; y++)
for (int x = 0; x < 256; x++)
fprintf(f, "%d %d %d\n", x, 0, 255 - x);
fclose(f);
image_show("gradient.ppm");A notebook of the gallery goes further: an image in memory as an array of structures, filters (negative, greyscale), the binary P6 format written with a single fwrite (try it, post). A program of the console draws a sunset (try it).
Drawing
<canvas.h> draws in a drawing area (a 2D canvas of the browser). The drawing is shown at the end of the run, or at each call of canvas_show():
#include <canvas.h>
canvas_create(300, 200); // white background
canvas_fill_color("orange"); // CSS colour…
canvas_fill_rect(20, 20, 100, 60);
canvas_fill_rgb(0, 100, 200); // … or RGB components
canvas_fill_circle(200, 100, 50);
canvas_stroke_color("black");
canvas_line_width(3);
canvas_line(0, 190, 300, 190);
canvas_font("20px sans-serif");
canvas_fill_text("Hello", 20, 140);Every function (colours, shapes, paths, text, pixels) is on the API page.
The origin is at the top left, the y axis points down. The drawing is not animated nor interactive (keyboard, mouse) yet.
The gallery offers drawings (shapes, loops, bar chart, steps of a sort) (try it), fractals (Mandelbrot, recursive tree, Koch snowflake, Sierpiński triangle) (try it) and the Mandelbrot set in the console (try it).
Completion
The Tab key completes the names of the headers after #include <, then what the included headers declare (functions, macros, types, constants: strl gives strlen( after #include <string.h>), the keywords and the names of the code already run or being written. The fields of a structure (s., p->) are not completed.
Files and modules
Extra files are available in the filesystem (fopen…). The open button (or the module URL parameter) installs *.c and *.h files, to include with #include "my_module.h".
Downloading a file
basthon_download, declared in <basthon.h>, downloads a file of the filesystem, for instance a PPM image built with fprintf (see Images):
#include <basthon.h>
basthon_download("gradient.ppm");The drawing of <canvas.h> is downloaded with canvas_download("drawing.png") (or "drawing.jpg" for JPEG), see the API page.