title:Undefine C keywords:c,linux,asm # Undefined C There is possible to piece of code inside online c compiler like https://www.onlinegdb.com/online_c_compiler Or run locally. With base check is done with gcc compiler. ## Compile __hello_world.c__ ```c int main() { printf("Hello world\n"); } ``` ```bash gcc hello_world.c -o hello_world gcc -m32 hello_world.c -o hello_world_32 #for 32bit target ``` ## Syntax ### Variables Standard list of available types #### Check type size All types have size that are declared in bytes. Some of the types are machine dependents. like int/long, if there is needed machine independent types then there are int32_t/uint32_t/int64_t/uint64_t Each architecture 8bit/16bit/32bit/64bit will have different size for those types Use __sizeof()__ Running on x86 machine ```c #include #include #include int main() { printf("Sizeof int %lu\n",sizeof(int)); printf("Sizeof int32_t %lu\n",sizeof(int32_t)); printf("Sizeof int64_t %lu\n",sizeof(int64_t)); printf("Sizeof long %lu\n",sizeof(long)); printf("Sizeof long long %lu\n",sizeof(long long)); } ``` Most safest/portable way is to use [u]int[8/16/32/64]_t types. Defined macros'es to get type max and min values are https://en.cppreference.com/w/c/types/limits ```c #include int main() { printf("INT_MIN %d\n",INT_MIN); printf("INT_MAX %d\n", INT_MAX); printf("LONG_MIN %ld\n",LONG_MIN); } ``` Example from AVR __stdint.h__ https://github.com/avrdudes/avr-libc/blob/main/include/stdint.h Example from Libc https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/stdint.h #### How to shoot the leg When code suppose to run on 32bit and 64bit platform the size of type may vary. Need to take in account this case. ### Functions Function syntax, there is nothing interesting on functions ``` ( ,..) { } ``` Write simple function ```c int fun1() { return -1; } ``` Function can have multiple return statements. Here is example whne function have 3 return values. ```c int fun2(int i) { if (i<0) return -1; if (i>0) return 1; return 0; } ``` Get address of function ```c printf("fun1 address %016x",&fun1);//64bit platform ``` ### If statement ```c if () ; if () {} ``` One of the way to check error of returned functions is ```c if ((c = getfun()) == 0) { } ``` Most simplest and outdated way to do this is when getting input from command line ```c #include int main() { int c; char ch; while ((c = getchar()) != EOF ) { ch = c; printf("Typed character %c\n",c); } } ``` ### For cycle For loop is one that may involve some trickery, its as simple as ```c for (;;) { } ``` Go over values from 1 till 10 ```c int i=0; for (i=1;i<=10;i++) { printf("%d\n",i) } ``` Now lets do it from 10 till 1 ```c int i=0; for (i=10;i>0;i--) { printf("%d\n",i) } ``` Now lets make one liner ```c for (i=0;i<10;i++,printf("%d\n",i)); ``` Yes there is possible to write as many expressions as needed. ### Structure Structure allows to combine types under one new type. Structure is convenient way how to combine set of types and reuse them as one. ```c struct struct1 { uint8_t a; uint16_t b; uint32_t c; uint64_t d; }; ``` Total intuitive size of structure would be ```c int total_szie = sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint64_t); int real_size = sizeof(struct1); ``` Types are placed inside structure to make fast access to them. Some instructions of CPU may require to access aligned memory addresses to not have penalty on accessing types inside structure. To directly mess with alignment of types use attribute ```c __attribute__ ((aligned (8))) ``` Use attributes to pack structure and be not architecture dependent. ```c struct struct2 { uint8_t a; uint16_t b; uint32_t c; uint64_t d; } __attribute__((packed)); ``` Now let check size of structure after it packed ```c int new_size = sizeof(struct2); ``` Also there is possible to add aligmnet to each time in structure ```c struct struct3 { uint8_t a __attribute__((aligned (8))); uint16_t b __attribute__((aligned (8))); uint32_t c __attribute__((aligned (8))); uint64_t d __attribute__((aligned (8))); } __attribute__((aligned (8))); ``` Now size of structure will be 32. All results on amd64, other arch may differ. ### How to shoot leg Forget that struct size is not consistent. ### Recursion Recursion is technique that could be useful to write shorter code and deal with cycles. One thing that recursion suffer is that it consumes stack memory and its have default limit on platform. ```c #include #include int fun_r(int i) { printf("val %d\n",i); fun_r(i+1); return 0; } int main() { fun_r(0); } ``` Program will fail after its reach out of stack range. When increase the default stack limit it go more further. Check default stack size ``` ulimit -s ``` Set stack size ``` ulimit -s 16384 ``` ### Macro ### Signed/Unsigned ### Pointers ### Endianess ### Static binary ### Dynamic binary ### Styles ### Compiler flags ### Allocate memory ### stdin,stdout,stderr ## Basic usage ### File manipulation ### File manipulation with syscalls ## Base usage ### Kernel module ### Write plugins ## Advanced cases ### Linking ### Extern ### Attributes ### Creating shared library ### Create static libraries ### Join all objects together ### Compile with musl ### Inspect elf files ### No standard library ### Memory leaks ### Code coverage ### Profiling ### Canary ### Atomic ### Multithreading ## Embedding ### Embed in C++ ### Embed in Go ### Embed in Swift ### Embed in JS ### Lua in C ### Python in C ## Multiplatform ### Cross compile ### Different flags ### Check architecture ### ARMv8 ### AVR8 ### Emscripten ## Graphics ### SDL2 ### GTK ### OpenGL ### Generate image