Skip to content

Instantly share code, notes, and snippets.

@Costava
Costava / randrgbpal.c
Created October 24, 2021 09:23
Print a list of 256 random RGB colors, except some colors at front of list are predetermined.
/*
* Print a list of 256 random RGB colors,
* except some colors at front of list are predetermined.
* Also prints the seed used (epoch time in seconds).
* R component is first.
* Colors are at least a certain distance apart.
* May fail to find a color that is not too close
* to previously generated colors, then the program prints and exits.
* Takes no arguments.
*
@Costava
Costava / zig_libraries.txt
Created July 14, 2021 03:38
List of collections of libraries written in Zig ( https://github.com/ziglang/zig ) or closely related to Zig
https://github.com/ziglang/zig/wiki/Community-Projects
https://github.com/nrdmn/awesome-zig
https://aquila.red/
https://zig.pm/
https://astrolabe.pm/
https://github.com/zigforum/needed-libraries
/*
* File: stacksize.c
* Author: Costava
*
* Example compilation:
* gcc stacksize.c -o stacksize -std=c89 -Wall -Wextra -Wconversion
*/
#include <stdint.h>
#include <stdio.h>
let foo = {num: 11};
let bar = {num: 22};
foo.other = bar;
bar.other = foo;
let myList = [foo, bar];
JSON.stringify(myList); // Fails
// Firefox 89: Uncaught TypeError: cyclic object value
/*
* Example compilation:
* gcc enum_no_warnings.c -std=c99 -Wall -Wextra -Wconversion
*/
enum food {
BANANA = 1,
CARROT = 2,
ORANGE = 3
};
@Costava
Costava / sscanf_overflow.c
Created April 29, 2021 04:17
The return value of and the value read by sscanf are misleading/wrong when an integer greater than INT_MAX is read.
//
// File: sscanf_overflow.c
// Author: Costava
//
// If sscanf is given a string of an integer greater than INT_MAX,
// sscanf will still return 1 even though the integer cannot have been
// read correctly into the given variable.
//
// errno gets set to ERANGE
// errno can be checked to see if sscanf actually succeeded.
@Costava
Costava / output_grid_image.c
Last active October 15, 2021 01:26
Print to stdout a P6 .ppm image of a grid
/*
* output_grid_image.c
*
* Print to stdout a P6 .ppm image of a grid.
* Specify the sizing and color of the grid in the user-defined values section,
* then compile and run.
* Program returns 0 on success.
* 1 if error allocating memory or outputting to stdout.
*
* Example compilation:
// Return random string of given length.
// Use Math.random function.
function mathRandomString(length) {
const set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i += 1) {
result += set[Math.floor(Math.random() * set.length)];
}
@Costava
Costava / caesarshiftall.c
Created April 11, 2021 17:22
Print the given string and all of its shifted variations
/*
* caesarshiftall.c
*
* Print the given string and all of its shifted variations.
* Print a newline after each.
* Return 0 on success.
* Return non-zero on failure. See ERROR_ defines.
*
* Example compilation:
* gcc caesarshiftall.c -o caesarshiftall -std=c89 -Wall -Wextra -Wconversion