added more to the c-fun article

This commit is contained in:
2026-06-05 00:02:26 +12:00
parent e0521c8bd4
commit 322a90d73c

View File

@@ -144,8 +144,8 @@ a start statement i.e. `int i = 0` (the bit of code that gets run before anythin
an end statement i.e. `i++` the statement that runs once the middle has been checked.
and if you are wondering how `int macro_var(_i_) = (start_func, 0)` assigns `macro_var(_i_)` to zero,
so am i, i will update this when i work this out.
so am i, i will update this when i work this out. <br/>
*update i have found out why this works check out [The comma operator](#the-comma-operator).
```c
#include <stdio.h>
@@ -490,6 +490,66 @@ void main() {
```
## The comma operator
Firstly you are asking yourself what the fuck is the comma operator is i was too when i saw the code for the defer macro. However the comma operator is fairly simple, the operator separates different expression much like how the `;` stop an expression. Although this is when the strangeness appears the comma operator will return the last expression value as the value of the total expression. This can lead to some neat readable code.
```c
int a = 1, b = 2;
int r = (a, b);
return r;
```
some real world use
```c
... // some fatal error occurd
retrun (errno = EINVAL, -1);
```
```c
int y;
if (y = f(x), y > x) {
return y;
}
if (y = g(x), y > u) {
return y;
}
if (y = q(x), y > b) {
return y;
}
```
## functions in functions (gcc only)
gcc has this really strange [compiler extensions](https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/C-Extensions.html) that can be used to some effect in the creation of clean code.
This one is the ability to define [functions in functions](https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Nested-Functions.html#Nested-Functions). However dont use then out side the scope because this will go wrong.
```c
#include <stdio.h>
int main() {
printf("start\n");
char something[] = "this is outside the scope";
void print_name(char name[]) {
printf("%s\n", something);
printf("name is : %s", name);
};
print_name("jim");
}
```