diff options
author | FreeArtMan <dos21h@gmail.com> | 2022-08-06 19:26:31 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2022-08-06 19:26:31 +0100 |
commit | 078df39f6abab46ff5015bea20cf44c4a4925999 (patch) | |
tree | 662f7a0c7e371b12d61a9979f872b343cdabb059 /md | |
parent | a7444c2bf041aa620f9242cd9c3bbcc6955a19df (diff) | |
download | md-content-078df39f6abab46ff5015bea20cf44c4a4925999.tar.gz md-content-078df39f6abab46ff5015bea20cf44c4a4925999.zip |
Add if and for
Diffstat (limited to 'md')
-rw-r--r-- | md/notes/undefined_c/titles.md | 148 |
1 files changed, 144 insertions, 4 deletions
diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md index 11d5271..2a47cc0 100644 --- a/md/notes/undefined_c/titles.md +++ b/md/notes/undefined_c/titles.md @@ -4,7 +4,22 @@ 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 +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 @@ -35,6 +50,8 @@ int main() { } ``` +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 @@ -48,9 +65,16 @@ int main() { } ``` +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. n +When code suppose to run on 32bit and 64bit platform the size of type may vary. Need to take in account this case. @@ -59,17 +83,121 @@ Need to take in account this case. ### Functions +Function syntax, there is nothing interesting on functions +``` +<RETURN_TYPE> <NAME>(<TYPE> <NAME>,..) { + <EXPR> +} +``` + +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 <stdio.h> +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 (<INITIAL>;<TERMINATE CONDITION>;<AFTER CYCLE>) { +} +``` + +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 ### Recursion ### 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 @@ -77,12 +205,23 @@ Need to take in account this case. ### Write plugins -### Intermediate usage +## Advanced cases -## Linking +### 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 @@ -103,6 +242,7 @@ Need to take in account this case. ### Check architecture ### ARMv8 ### AVR8 +### Emscripten ## Graphics |