Skip to content

Instantly share code, notes, and snippets.

View imaami's full-sized avatar
🐺
C/C++/Linux ░▒▓▉▊▋▌▍▎▏

Juuso Alasuutari imaami

🐺
C/C++/Linux ░▒▓▉▊▋▌▍▎▏
  • Finland
View GitHub Profile
static const char *const SOURCE=(const char[]){
#embed __FILE__
,0}+72;
#include <stdio.h>
int main() {
puts(SOURCE);
}
@imaami
imaami / main.sh
Last active July 20, 2023 20:30
main? main.
#!/usr/bin/bash
#; / / #;
#; / / #;
#; / / #;
#;/ / #;
#; / #;
#; / ; ; ; ; #;
#; / ####### #;
#;/ ## C ## #;
#; ;#####; #;
@imaami
imaami / dstr_fini_test.c
Created July 9, 2023 21:43
Testing optimization levels
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stringify_(x) #x
#define stringify(x) stringify_(x)
@imaami
imaami / dstr_view_demo.c
Last active July 9, 2023 00:58
An SSO thing in C, WIP
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stringify_(x) #x
#define stringify(x) stringify_(x)
@imaami
imaami / terror.c
Created July 1, 2023 11:38
strerror() but more cursed
#include <pthread.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static void *fn (void *p)
{
pthread_detach(pthread_self());
@imaami
imaami / documentarist.sh
Last active February 22, 2024 13:46
Use ChatGPT to document your C/C++ code.
#!/usr/bin/env bash
#
# IMPORTANT: The HTTP authorization header must be found
# in ~/.openai in full, not just the API key.
#
# Usage: ./documentarist.sh /path/file.c "my_function()"
#
# Requirements: dos2unix, cpp, clang-format, jq, curl
#
@imaami
imaami / Makefile
Last active March 6, 2024 19:52
#template<>
override BIN := test
override SRC := test.c msg.c
override OBJ := $(SRC:%=%.o)
override DEP := $(SRC:%=%.d)
CFLAGS := -O2 -march=native -mtune=native -flto
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
@imaami
imaami / of_size.c
Last active April 16, 2023 16:27
Polish notation sizeof()
#include <stdint.h>
#include <stdio.h>
/** Polish notation sizeof(). */
#define of_size(x) __typeof__( \
_Generic((x) \
, int: of_signed (x) \
, unsigned: of_unsigned (x)) )
#define of_signed(x) \
@imaami
imaami / typeof_unqual.c
Created April 15, 2023 13:17
Backport of C23's typeof_unqual in plain C18
#define typeof_unqual(x) __typeof__(((void)1, *(__typeof__(x) *)(void *)0))
int main(void) {
const int ci = 1;
typeof_unqual(ci) i1 = ci;
typeof_unqual(const int) i2 = ci;
i1 = 0;
i2 = 0;
return i1 + i2;
}
@imaami
imaami / gnuc_typeof_unqual.c
Last active April 15, 2023 13:20
Backport of C23's typeof_unqual using GNU extensions
/* If you compile with clang and `-Weverything' you'll probably want
* to use `-Wno-gnu-statement-expression-from-macro-expansion', too.
*
* Plain C18 version:
* https://gist.github.com/imaami/92b38f45142f8a8390fefa2d972bc63d
*/
#define typeof_unqual(x) __typeof__(({ *(__typeof__(x) *)(void *)0; }))
int main(void)
{