C playground

Compile and run C in your browser, without installing gcc: several files with their .h, scanf with prepared input, the compile error highlighted on its line and even processes with fork() and threads with pthread, imitated so that you can practise IC2 on your phone. It is the same environment as the IC2 practical mock exam.

How to use it

  • Files: one tab per file. + creates a new one; the pencil (or a double click on its name) renames it and the cross deletes it (it asks for a second click). Header files are included with #include "grades.h", as on your computer.
  • Run compiles every .c file (the one with main last) and runs the program. If it does not compile, the error appears in the Console with its file and line, which is highlighted in the editor.
  • Input: what scanf and fgets will read, one line per read, and the console shows what was read as if you had typed it. It is not interactive: type the data before running.
  • Your code is saved automatically in this browser. Download gives you the open file and .zip all of them, ready to compile with gcc on your Linux. Upload adds your own .c, .h or data files (.txt) that your program can open with fopen or open.
  • Share creates a link with your code inside: whoever opens it sees your files exactly as they are. The code travels inside the link itself; nothing is stored on the website.
  • Helps turns on or off the suggestions, bracket closing, extra colours and the highlighted error line: without them you work as with gedit in the exam. The expand button (top right) makes the environment fill the window and A− / A+ change the text size.

Processes and threads in the browser

There is no Linux under the browser, so the environment imitates what IC2 uses:

  • fork() creates a child process with its own memory: if the child changes a variable, the parent does not notice, as on Fedora. getpid(), getppid() (an orphan child gets 1 as its parent), wait, waitpid, exit and WEXITSTATUS work. The program is always process 4310 and its children, 4311, 4312…
  • sleep and usleep do not make you really wait (a few seconds at most), but the output comes out in clock order: what the parent prints after its wait appears after what the child prints before it ends.
  • Threads (pthread_create, pthread_join, pthread_exit and mutexes) run one after another, so you will not see a race condition here: for that, your Fedora. If main ends without pthread_join, the environment tells you the threads never got to run.
  • There is no exec, no pipes, no signals and no ps or pstree: those screenshots, on your Linux.

Paste this into the playground and run it:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
    int x = 10;
    pid_t pid = fork();

    if (pid == 0) {
        x = 99;   // only the child's x changes
        printf("Child %d (parent %d): x = %d\n", getpid(), getppid(), x);
        sleep(2);
        exit(3);
    }

    int status;
    wait(&status);
    printf("Parent %d: my child returned %d and my x is still %d\n", getpid(), WEXITSTATUS(status), x);
    return 0;
}

What you can and cannot do

  • It compiles with xcc, a C compiler written for WebAssembly (open source, MIT licence), with its standard library: stdio.h, stdlib.h, string.h, math.h, ctype.h, files with fopen and with open/read/write, malloc and free.
  • It is stricter than gcc in a few things: a scanf("%d", x) without & does not compile (in gcc it is only a warning, and then the program breaks).
  • It warns about the errors that would be a Segmentation fault on Fedora: an index outside an array, a NULL pointer or a recursion that never ends, with its line.
  • Each run can last at most 15 seconds: an infinite loop stops by itself, and you also have the Stop button.
  • The first time it downloads the compiler (half a megabyte); after that it starts at once.

Practise with it