r/C_Programming • u/jombrowski • Jun 05 '26
Shellbonacci
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
if(argc < 2)
return 0;
int n = atoi(argv[1]);
if(n >= 2)
{
char bfr1[1000], bfr2[1000];
sprintf(bfr1, "%s %d", argv[0], n - 1);
sprintf(bfr2, "%s %d", argv[0], n - 2);
n = system(bfr1) + system(bfr2);
}
printf("%d\n", n);
return n;
}
13
Upvotes
18
u/Puzzleheaded_Good360 Jun 05 '26
Thanks, I hate it
13
u/moocat Jun 06 '26
On most *nix systems, process return values are limited to the range [0, 256).
5
u/HernBurford Jun 05 '26
Perverse.
3
u/jontsii Jun 06 '26
Haven´t thought of shell-based recursion... ITS BEAUTIFUL
1
u/flyingron Jun 05 '26
In addition to being generally stupid, your program doesn't wrok.
Argv[0] is not guaranteed to be a string that could invoke the program.
The order of the system calls is unspecified, not necessarily left to right.
2
u/No_Development5871 Jun 06 '26
Certified Jombrowski moment