Skip to content

Instantly share code, notes, and snippets.

@chadbrewbaker
Created June 5, 2022 15:16
Show Gist options
  • Save chadbrewbaker/53b4280f22d370a36699118d4376d456 to your computer and use it in GitHub Desktop.
Save chadbrewbaker/53b4280f22d370a36699118d4376d456 to your computer and use it in GitHub Desktop.
Template for running C code in Google Colab
prog = """
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
size_t buf_size = 1 << 18; // 256KiB
char* buf = (char*) malloc(buf_size);
memset((void*)buf, 'X', buf_size); // output Xs
while (1) {
size_t remaining = buf_size;
while (remaining > 0) {
// Keep invoking `write` until we've written the entirety
// of the buffer. Remember that write returns how much
// it could write into the destination -- in this case,
// our pipe.
ssize_t written = write(
STDOUT_FILENO, buf + (buf_size - remaining), remaining
);
remaining -= written;
}
}
} """
text_file = open("fiz.c", "w")
n = text_file.write(prog)
text_file.close()
!ls
!clang fiz.c -O3 -o fiz.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment