====== C Language ====== ====== Things i should read up on ====== * [[https://en.cppreference.com/w/c/language/operator_precedence|Operator precedence order]] * [[https://en.cppreference.com/w/c/language/eval_order|Evaluation order]] * [[http://c-faq.com/index.html|C FAQ]] * [[https://www.agner.org/optimize/calling_conventions.pdf|C++ ABI]] -- Not exclusively for C, but still relevant * [[https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html|Statements and Declarations in Expressions]] * What inline in combination with export and static actually does [[https://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99|SO1]], [[https://stackoverflow.com/questions/216510/what-does-extern-inline-do|SO2]], [[https://en.cppreference.com/w/c/language/inline|CPP reference]] * [[http://www.catb.org/esr/structure-packing/]] -- The lost art of structure packing [[http://www.underhanded-c.org/|Underhanded C Contest]] [[http://uguu.org/sources.html|uguu]] -- Anime ascii art ====== IOCCC ====== [[https://www.ioccc.org/|The International Obfuscated C Code Contest]] * [[https://stackoverflow.com/questions/19298778/why-does-the-1987-korn-oneliner-print-unix|Best one-liner 1987]] ====== 8 phases of translation in C ====== [[https://stackoverflow.com/questions/1476892/poster-with-the-8-phases-of-translation-in-the-c-language/1479972#1479972|stackoverflow]] ANSI C translation phases ========================= +-------------------------------------------------+ | map physical characters to source character set | | replace line terminators with newlines | | decode trigraph sequences | +-------------------------------------------------+ | V +---------------------------------------+ | join lines along trailing backslashes | +---------------------------------------+ | V +-------------------------------------------------------------+ | decompose into preprocessing tokens and whitespace/comments | | strip comments | | retain newlines | +-------------------------------------------------------------+ | V +------------------------------------------------+ | execute preprocessing directives/invoke macros | | process included files | +------------------------------------------------+ | V +----------------------------------------------------------------+ | decode escape sequences in character constants/string literals | +----------------------------------------------------------------+ | V +--------------------------------------+ | concatenate adjacent string literals | +--------------------------------------+ | V +------------------------------------------+ | convert preprocessing tokens to C tokens | | analyze and translate tokens | +------------------------------------------+ | V +-----------------------------+ | resolve external references | | link libraries | | build program image | +-----------------------------+ ====== Right-to-left rule ====== [[https://stackoverflow.com/questions/8392228/complicated-right-to-left-rule-when-defining-a-variable|SO thread]] [[https://www.reddit.com/r/learnprogramming/comments/66a4og/the_rightleft_rule_useful_for_understanding_cc/|Reddit]] To decode the expression ''char (*(*x())[5])()'' x : x is a x() : function *x() : returning pointer to (*x())[5] : a 5-element array of *(*x())[5] : pointer to (*(*x())[5])() : function char (*(*x())[5])() : returning char ====== Pointer vs Array ====== They are NOT the same, despite what people say! // Example of char array vs char pointer char *str1 = "Foo"; // Pointer. "Foo" resides in TEXT-section char str2[] = "Bar"; // Array. "Bar" resides in RAM, copied to DATA section during C-startup str1[2] = 'O'; // Illegal, undefined behaviour str2[2] = 'R'; // This is fine, it alters content of RAM ====== Const ====== uint8_t const *foo; // Points at constant. Can't change the value that foo points at. const uint8_t *foo; // Same as above. uint8_t *const foo; // Constant pointer. Can't change the address it is holding. ====== stdint.h, printf and scanf ====== ''stdint.h'' has definitions of ''uint8_t'' etc. ''inttypes.h'' has definitions of ''PRIu8'' etc, for use with printf and scanf. [[https://en.cppreference.com/w/c/types/integer|This site]] lists all available macros and types. #include #include ... printf("Printing a uint8_t: %" PRIu8 " woop woop!", var8bit); ====== Snippets ====== ===== Debug macro ===== #define STRINGIFY_(X) #X #define STRINGIFY(X) STRINGIFY_(X) #define PING() debug_print_str(__FILE__ " : " STRINGIFY(__LINE__) "\n")