英文:
Recursion in Macro Preprocessor
问题
I am trying to write a function that calculates Factorial RECURSIVELY but I have problem with compiling the code. BTW, I do not know if the code itself is coorect!
The code:
#include <stdio.h>
#define FACTORIAL(n) \
FACTORIAL_##n()
#define FACTORIAL_0() 0
#define FACTORIAL_1() 1
#define FACTORIAl_N(n) \
FACTORIAL(n - 1) + FACTORIAL(n - 2)
#define FACTORIAL_RESULT(n) \
FACTORIAL(n)
int main() {
int f2 = FACTORIAL_RESULT(2);
printf("%d\n", f2);
return 0;
}
I get a bunch of errors when compiling this code such as:
* Executing task: C:\MinGW\bin\gcc.exe c.c -o d:\C\output\c.exe -Wall -Wextra -g3
* Terminal will be reused by tasks, press any key to close it.
* Executing task: C:\MinGW\bin\gcc.exe c.c -o d:\C\output\c.exe -Wall -Wextra -g3
c.c: In function 'main':
c.c:4:5: warning: implicit declaration of function 'FACTORIAL_2'; did you mean 'FACTORIAL_0'? [-Wimplicit-function-declaration]
4 | FACTORIAL_##n()
| ^~~~~~~~~~
c.c:14:5: note: in expansion of macro 'FACTORIAL'
14 | FACTORIAL(n)
| ^~~~~~~~~
c.c:17:14: note: in expansion of macro 'FACTORIAL_RESULT'
17 | int f2 = FACTORIAL_RESULT(2);
| ^~~~~~~~~~~~~~~~
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user1\AppData\Local\Temp\ccuGFhBc.o: in function `main':
D:\C/c.c:17: undefined reference to `FACTORIAL_2'
collect2.exe: error: ld returned 1 exit status
* The terminal process "C:\MinGW\bin\gcc.exe 'c.c', '-o', 'd:\C\output\c.exe', '-Wall', '-Wextra', '-g3'" terminated with exit code: 1.
* Terminal will be reused by tasks, press any key to close it.
Anyone who can help me? Thanks in advance!
英文:
I am trying to write a function that calculates Factorial RECURSIVELY but I have problem with compiling the code. BTW, I do not know if the code itself is coorect!
The code:
#include <stdio.h>
#define FACTORIAL(n) \
FACTORIAL_##n()
#define FACTORIAL_0() 0
#define FACTORIAL_1() 1
#define FACTORIAl_N(n) \
FACTORIAL(n - 1) + FACTORIAL(n - 2)
#define FACTORIAL_RESULT(n) \
FACTORIAL(n)
int main() {
int f2 = FACTORIAL_RESULT(2);
printf("%d\n", f2);
return 0;
}
I get a bunch of errors when compiling this code such as:
* Executing task: C:\MinGW\bin\gcc.exe c.c -o d:\C\output\c.exe -Wall -Wextra -g3
* Terminal will be reused by tasks, press any key to close it.
* Executing task: C:\MinGW\bin\gcc.exe c.c -o d:\C\output\c.exe -Wall -Wextra -g3
c.c: In function 'main':
c.c:4:5: warning: implicit declaration of function 'FACTORIAL_2'; did you mean 'FACTORIAL_0'? [-Wimplicit-function-declaration]
4 | FACTORIAL_##n()
| ^~~~~~~~~~
c.c:14:5: note: in expansion of macro 'FACTORIAL'
14 | FACTORIAL(n)
| ^~~~~~~~~
c.c:17:14: note: in expansion of macro 'FACTORIAL_RESULT'
17 | int f2 = FACTORIAL_RESULT(2);
| ^~~~~~~~~~~~~~~~
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user1\AppData\Local\Temp\ccuGFhBc.o: in function `main':
D:\C/c.c:17: undefined reference to `FACTORIAL_2'
collect2.exe: error: ld returned 1 exit status
* The terminal process "C:\MinGW\bin\gcc.exe 'c.c', '-o', 'd:\C\output\c.exe', '-Wall', '-Wextra', '-g3'" terminated with exit code: 1.
* Terminal will be reused by tasks, press any key to close it.
Anyone who can help me? Thanks in advance!
答案1
得分: 2
C标准不指定宏的动态创建。 #define FACTORIAL_N(n) …
只会定义一个带有字面值 N
的单个宏;它不会定义一系列带有不同数字替代 N
的宏。
宏不用于循环、任意递归或嵌套。
英文:
The C standard does not specify any dynamic creation of macros. #define FACTORIAL_N(n) …
will only define a single macro with a literal N
in its name; it will not define a family of macros with different numerals in place of N
.
Macros are not intended for looping or arbitrary recursion or nesting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论