r/C_Programming • • 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

16 comments sorted by

View all comments

Show parent comment

1

u/SPAstef Jun 06 '26

Ah right good point, I didn't even consider the printing at all, was just reasoning about the final result of the computation. Then one should introduce a sequence point to force determinism (assuming system() blocks, which it should)