-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propose a new 5th page to introduce argument counting
- Loading branch information
Showing
4 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Optional arguments: General case | ||
|
||
:::{admonition} Work In Progress | ||
:class: warning | ||
This page is a draft | ||
::: | ||
|
||
OK so we now know how to have a few optional arguments with a default value, but what if we want to do something entierly different depending on the number of arguments provided ? | ||
|
||
## Objective | ||
|
||
A `range` macro similar to the python builtin function, that allows one to iterate over a range of integers: | ||
- `range(STOP)`{l=python} iterates from 0 to _STOP_, _STOP_ excluded | ||
- `range(START, STOP)`{l=python} iterates from _START_ to _STOP_, _STOP_ excluded | ||
- `range(START, STOP, STEP)`{l=python} iterates from _START_ to _STOP_ by steps of _STEP_, _STOP_ excluded | ||
|
||
Examples: | ||
- `range(5)`: `0 1 2 3 4` | ||
- `range(3, 8)`: `3 4 5 6 7` | ||
- `range(2, 7, 3)`: `2 5` | ||
|
||
How will it look like in C ? Well it's up to you, here are a few propositions: | ||
:::::{tab-set} | ||
::::{tab-item} Syntax 1 | ||
```{code-block} C | ||
:caption: Indented correctly by you text editor by default | ||
for (int in_range(i, 5)) | ||
/* 0 1 2 3 4 */; | ||
for (int in_range(i, 3, 8)) | ||
/* 3 4 5 6 7 */; | ||
for (int in_range(i, 2, 7, 3)) | ||
/* 2 5 */; | ||
``` | ||
:::: | ||
::::{tab-item} Syntax 2 | ||
```{code-block} C | ||
:caption: Single parenthesis level | ||
for_in_range(int, i, 5) | ||
/* 0 1 2 3 4 */; | ||
for_in_range(int, i, 3, 8) | ||
/* 3 4 5 6 7 */; | ||
for_in_range(int, i, 2, 7, 3) | ||
/* 2 5 */; | ||
``` | ||
:::: | ||
::::{tab-item} Syntax 3 | ||
```{code-block} C | ||
:caption: Explicit variable declaration | ||
int i; | ||
for_in_range(i, 5) | ||
/* 0 1 2 3 4 */; | ||
for_in_range(i, 3, 8) | ||
/* 3 4 5 6 7 */; | ||
for_in_range(i, 2, 7, 3) | ||
/* 2 5 */; | ||
``` | ||
:::: | ||
::::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,5 @@ Black Magic | |
02_use | ||
03_log | ||
04_default | ||
05_argcount | ||
05_range | ||
quotes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdio.h> | ||
|
||
#define ARG_10(A, B, C, D, E, F, G, H, I, J, ...) J | ||
#define ARG_COUNT(...) ARG_10(__VA_ARGS__ __VA_OPT__(,) 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) | ||
|
||
#define FOO(...) printf("FOO was called with %i arguments\n", ARG_COUNT(__VA_ARGS__)) | ||
|
||
#define SPELL_0 "zero" | ||
#define SPELL_1 "one" | ||
#define SPELL_2 "two" | ||
#define SPELL_3 "three" | ||
#define SPELL_4 "four" | ||
|
||
#define SPELL(N) SPELL_ ## N | ||
|
||
int main() | ||
{ | ||
FOO(); | ||
FOO(1, 2, 3, 4, 5); | ||
|
||
printf("%s %s\n", SPELL(4), SPELL(2)); | ||
} |