Logarithmic-time string-to-enum lookup using only standard C library functions: https://godbolt.org/z/6aaxehzf5
#cprogramminglanguage #clanguage
Compiler Explorer - C

typedef enum { THING_INVALID = -1, #define THING(X) THING_ ## X, //#include "thingdefs.h" // start of thingdefs.h #ifndef THING #error "Missing function-like macro definition" #endif THING(apple) THING(banana) THING(orange) #undef THING // end of thingdefs.h } thing_t; static const char *const dict[] = { #define THING(X) #X, //#include "thingdefs.h" // start of thingdefs.h #ifndef THING #error "Missing function-like macro definition" #endif THING(apple) THING(banana) THING(orange) #undef THING // end of thingdefs.h }; static int compare_strings(const void *const key, const void *const elemp) { assert(key); assert(elemp); const char *const *const stringp = elemp; return strcmp(key, *stringp); } thing_t string_to_id(const char *const key) { char const *prev = ""; for (size_t i = 0; i < _Countof dict; ++i) { char const *const elem = dict[i]; assert(strcmp(prev, elem) < 0); prev = elem; } (void)prev; const char *const *const elemp = bsearch(key, dict, _Countof dict, sizeof dict[0], compare_strings); if (elemp) { return (thing_t)(elemp - dict); } else { return THING_INVALID; } } int main(const int argc, const char *const argv[const static argc + 1]) { for (size_t i = 0; i < argc; ++i) { thing_t const t = string_to_id(argv[i]); switch (t) { case THING_apple: puts("Crunch!"); break; case THING_banana: puts("Squish!"); break; case THING_orange: puts("Splatter!"); break; case THING_INVALID: fputs("Invalid argument", stderr); break; } } return 0; }

Continuano gli esperimenti con Claude AI. Creare un interprete di linguaggio C e anche un compilatore/linker per Windows sono alla portata di Claude Sonnet 4.6 in versione gratuita? Scopriamolo assieme in questo video! #clanguage #artificialintelligence #claudeai #compiler #interpreter #x86 https://www.youtube.com/watch?v=zHHHjynC8Zg
Sfida per Claude: Interprete e Compilatore C nel browser / Linguaggio C

YouTube

[LINGUAGEM C] LAB 4 - Layout de memória e compilação

https://bolha.tube/w/riGbu9tN1UHc9NN8C3BYxe

[LINGUAGEM C] LAB 4 - Layout de memória e compilação

PeerTube

[LINGUAGEM C] Aula 8 - O que acontece na compilação

https://bolha.tube/w/rnKhsbje7mdVhWaR5MsfMS

[LINGUAGEM C] Aula 8 - O que acontece na compilação

PeerTube

[LINGUAGEM C] Aula 7 - Layout e mapeamento de memória

https://bolha.tube/w/33UkmUqkovWk7SB4fsgm6c

[LINGUAGEM C] Aula 7 - Layout e mapeamento de memória

PeerTube

[LINGUAGEM C] LAB # 3 - Como programas são executados

https://bolha.tube/w/jYaN6F2upvcVWUoknzkfU4

[LINGUAGEM C] LAB # 3 - Como programas são executados

PeerTube

[LINGUAGEM C] AO VIVO - Mudanças no curso

https://bolha.tube/w/quv2brZi7fjscKqr6gFrjA

[LINGUAGEM C] AO VIVO - Mudanças no curso

PeerTube

[LINGUAGEM C] LAB #2 - Do que programas são feitos

https://bolha.tube/w/dYDjfiJsUD9ju8kcec8GxL

[LINGUAGEM C] LAB #2 - Do que programas são feitos

PeerTube

[LINGUAGEM C] Aula 6 - Como programas são executados

https://bolha.tube/w/hh2pEuCUL4RnVa15js5JZs

[LINGUAGEM C] Aula 6 - Como programas são executados

PeerTube

Okay, more C (or just generally curly-braces-style-languages) discourse: I am a strong believer in aggressively using braces everywhere.

Very occasionally I will omit them on single-line if/for/while statements, but even that makes me feel uncomfortable.

I've seen far too many bugs resulting from not being aggressive about block delimiter usage over the years.

Bonus: Diffs adding/removing single lines of code are cleaner.

Toy systems language choice: braces are *required*.

#CLanguage