Skip to content

Commit

Permalink
Propose a new 5th page to introduce argument counting
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Aug 30, 2024
1 parent 3c14d31 commit b22969f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
58 changes: 58 additions & 0 deletions book/pages/05_range.md
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 */;
```
::::
:::::
2 changes: 1 addition & 1 deletion book/pages/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Black Magic
02_use
03_log
04_default
05_argcount
05_range
quotes
6 changes: 3 additions & 3 deletions book/samples/03_stringline3.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

#define log_log(LEVEL, MESSAGE, ...) \
printf("|" LEVEL "|`" __FILE__ "`|`%s`|" STRINGIZE_EVALUATED(__LINE__) "|" MESSAGE "\n", \
__func__ __VA_OPT__(, ) __VA_ARGS__)
__func__ __VA_OPT__(,) __VA_ARGS__)

#define log_debug(MESSAGE, ...) log_log("DEBUG", MESSAGE __VA_OPT__(, ) __VA_ARGS__)
#define log_error(MESSAGE, ...) log_log("ERROR", MESSAGE __VA_OPT__(, ) __VA_ARGS__)
#define log_debug(MESSAGE, ...) log_log("DEBUG", MESSAGE __VA_OPT__(,) __VA_ARGS__)
#define log_error(MESSAGE, ...) log_log("ERROR", MESSAGE __VA_OPT__(,) __VA_ARGS__)

void foobar(const char* file_name)
{
Expand Down
22 changes: 22 additions & 0 deletions book/samples/05_limitation.c
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));
}

0 comments on commit b22969f

Please sign in to comment.