imaami

u/imaami



r/C_Programming•

Here's a neat little hex char parsing trick. Not terribly practical, but it's not like you have anything better to do than have a look. It's only mildly atrocious.

#include <stdio.h>

static inline unsigned
from_hex_char (char c)
{
	return (' '|c) % 29 % 19;
}

int
main (int    c,
      char **v)
{
	for (int i = 1; i < c; ++i) {
		unsigned long long x = 0;
		for (char *p = &v[i][2 * (v[i][0] == '0' &&
		                     (' '|v[i][1]) == 'x')]; *p; ++p) {
			x <<= 4U;
			x |= from_hex_char(*p);
		}
		(void)printf("%llu\n", x);
	}
}
3122


r/Suomi•

Tein palindromin

Meemit ja Huumori

Iso hieno kusirutto, rikoksia. Saksa piruna hioi maan. Etsi ripuli, uhoa; tajusi rajoilla kallio ja risuja. Tao huilu, piriste; naamioi hanuripaska. Saisko kirottu risukone ihosi?

45149

r/C_Programming•

To celebrate this perfectly ordinary Wednesday, here's a Linux implementation of a C23 compiler in C23. Requires at least clang-21 or gcc-15. No AI was used during development.

// Compile with `-std=gnu23`
int main() {
	[[gnu::retain]] [[gnu::used]]
	static const unsigned char cc1[] = {
		#embed "/proc/self/exe"
	};
}
8510

r/C_Programming•

Here's an interesting use case for C23's typeof (and optionally auto): returning untagged, untyped "transient" structs by value. The example here is slightly contrived, but resembles something genuinely useful.

#include <errno.h>
#include <stdio.h>
#include <string.h>

static struct {
	char msg[128];
} oof (int         error,
       int         line,
       char const *text,
       char const *file,
       char const *func)
{
	typeof (oof(0, 0, 0, 0, 0)) r = {};
	char const *f = strrchr(file, '/');
	if (!f || !*++f)
		f = file;
	(void)snprintf(r.msg, sizeof r.msg,
	               "%s:%d:%s: %s: %s",
	           
1525

r/C_Programming•

What can I say.

#include <pthread.h>
#include <stdatomic.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define len(x) (sizeof (x) / sizeof (x)[0])
#define oof(e, f) do if (e) {(void)fprintf( \
        stderr, "%s: pthread_" #f ": %s\n", \
        __func__, strerror(e)); abort();} while(0)

static void *run (void *a);

#define HELL "Hello, World!"

static struct {
	pthread_t         tid[sizeof HELL - 1];
	pthread_barrier_t bar;
	_Atomic(uint16_t) ctr;
	uint16_t          there[sizeof HELL - 1];
	char              world[sizeof HELL];
} hello (
4630


r/outlier_ai•

I was onboarded as primarily a C expert with C++ skills. The onboarding assessment was about C++, although some of the questions seemed to indicate that the quiz author didn't fully understand the differences between C and C++. And one of the questions was about code that's actually undefined behavior because the order of evaluation isn't sequenced, yet the multiple-choice options all assumed there to be a definite right answer.

The above was a little sus. But I passed. Then I accepted Hopper, and although I do have some Python experience as well, I have never stated anywhere that I'm a Python programmer. Neither was I enrolled as such. But I started the assessment quiz, and jesus christ.

43