summaryrefslogtreecommitdiff
path: root/md/writeup/c_macro_tricks.md
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2023-02-04 14:10:26 +0000
committerFreeArtMan <dos21h@gmail.com>2023-02-04 14:10:26 +0000
commit380f0fe40b1ab20790e75dac779e73667cc5ac72 (patch)
tree9fa3b9c93b58c966daa22f955282bc3c181d909a /md/writeup/c_macro_tricks.md
parentdf881efab146ca3ee61bf8936f948dd976fc4740 (diff)
downloadmd-content-380f0fe40b1ab20790e75dac779e73667cc5ac72.tar.gz
md-content-380f0fe40b1ab20790e75dac779e73667cc5ac72.zip
Update all content to new pygmentize
Diffstat (limited to 'md/writeup/c_macro_tricks.md')
-rw-r--r--md/writeup/c_macro_tricks.md37
1 files changed, 18 insertions, 19 deletions
diff --git a/md/writeup/c_macro_tricks.md b/md/writeup/c_macro_tricks.md
index 4068272..e9f9d0f 100644
--- a/md/writeup/c_macro_tricks.md
+++ b/md/writeup/c_macro_tricks.md
@@ -15,10 +15,10 @@ this allows to see resulting source that going to be compiled, macro errors
could be hard to debug, but this is first thing, test them before and then
be sure that everything works. Lets continue with some more deep stuff.
-<!-- ####################################################################### -->
+
## __VA_ARGS__ keyword
-<!-- ####################################################################### -->
+
### Single argument macros
Writting macros with single argument
@@ -42,14 +42,14 @@ F(})
any kind of argument can be passed to macro, and that allows to make some tricks
#### Result
-```
+```c
int main
(){
printf("hello world\n");
}
```
-<!-- ####################################################################### -->
+
### Multi argument macro
writting macro with multiple unamed arguments
@@ -74,7 +74,7 @@ Previouse example works just fine, but if add multiple arguments the __VA_ARGS__
just prints them as a whole string
#### Result
-```
+```c
int main
(){
printf("hello world\n");
@@ -84,7 +84,7 @@ printf("hello world\n");
```
-<!-- ####################################################################### -->
+
### Mixing named arguments and unamed arguments
Mixing together named and unnamed arguments
@@ -108,7 +108,7 @@ F2(int main,{my code},{more code})
#### Result
-```
+```c
2,3,4,5
1
@@ -116,7 +116,7 @@ F2(int main,{my code},{more code})
int main
```
-<!-- ####################################################################### -->
+
## Define struct with macros
Lets move to some more practical example lets just define macro that going to
@@ -172,7 +172,6 @@ struct add {int a; int b;};;
struct dirst {int c; ;};;
```
-<!-- ####################################################################### -->
## Detect number of arguments
There is one trick that can be used to detect number of arguments passed to
@@ -222,12 +221,12 @@ F(1,2,3,4,5)
5
```
-<!-- ####################################################################### -->
+
## Variable argument macro match macro according number of arguments
Detect number of arguments and match macro according to number of arguments
-```
+```c
#define FUN3(X1,X2,X3,...) "there is 3"
#define FUN2(X1,X2,...) "there is 2"
#define FUN1(X1,...) "there is 1"
@@ -237,14 +236,14 @@ Detect number of arguments and match macro according to number of arguments
```
#### Source
-```
+```c
FUN(add,int a,int b);
FUN(mul,int a,int b,int c);
FUN(div,int a);
```
#### Result
-```
+```c
void add ( "there is 2");
void mul ( "there is 3");
void div ( "there is 1");
@@ -257,7 +256,7 @@ void div ( "there is 1");
Best part of it that it can match also typdefed structures. So now macroses
can contain typechecking
-```
+```c
#define type_str(T) _Generic( (T), int: "int",\
long: "long",\
A: "A",\
@@ -287,7 +286,7 @@ int main()
#### Result
-```
+```block
long
int
A
@@ -305,7 +304,7 @@ default: "UNK UNK" )
```
#### Source
-```
+```c
typedef struct A
{
@@ -336,7 +335,7 @@ int int
A A
```
### Generic printf
-```
+```c
#define FF "%f "
#define FS "%s "
#define FD "%d "
@@ -355,7 +354,7 @@ A A
```
#### Source
-```
+```c
int main()
{
A a;
@@ -372,7 +371,7 @@ int main()
#### Result
-```
+```bash
1 2.000000
3 4
big float 0.010000