1

I need to find the output of this code. I know that this is recursion but i do not exactly know how this code actually works. I thought that this would be an infinite code but it is not. Can someone please explain me?

 #include <stdio.h>
 char *str = "kre";
 void main(void) {
     int i;
     static int j = 3;
     for (i = 0; i < j; i++) {
         printf("%d ", j--);
         main();
     }
     printf("%sn%c ", str, 'i');
 }
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
Petar
  • 13
  • 2
  • suggestion: single-step through your program with a debugger. – pmg May 22 '20 at 07:51
  • 1
    suggestion 2: use an [indentation style](https://en.m.wikipedia.org/wiki/Indentation_style) other than "everything left-aligned" – pmg May 22 '20 at 07:52

1 Answers1

0

I thought that this would be an infinite code but it is not

It's not an infinite code because, you declare static for j (see What does “static” mean in C?):

static int j = 3;

The value of j will be updated when you call main function. You can see the output of your program:

3 2 1 kreni kreni kreni kreni

the value of j decrement from 3 to 1, then when j = 0, you can not access the for loop because the condition i < j is not accepted, so the main() is not called again.

If you want an infinite code, you remove static from declaration of j, your program will never be stopped.

Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • Thanks for explanation, but i still do not understand why it prints "kreni" 4 times when the loop is finished. It is maybe a stupid question but this really confuses me. – Petar May 22 '20 at 08:19
  • @Petar because, in the last call of `main()`, `j=0`, so you cannot access the `for` loop to print the value of `j` (`printf("%d ", j--);` is called 3 times), but when `j=0` you can still print `kreni`. this is reason why you print `j` 3 times but 4 times for `kreni` – Hitokiri May 22 '20 at 08:23
  • I thought that output would be "3 kreni 2 kreni 1 kreni" and it confuses me how it prints "3 2 1" in the loop but then prints "kreni" 4 times. – Petar May 22 '20 at 08:38
  • The function is called when `i=0`, `i=1` and `i=2`. However, don't forget that the first time `main` is called by starting the program. – Cheatah May 22 '20 at 08:42
  • @Petar each time you call main, your program back to the first line of main, until `j = 0`, 4 called times of `main` will can reach the line `printf("%sn%c ", str, 'i');`. You can print the value of `j` in the last `printf`: `printf("%sn%c, j = %d ", str, 'i',j);` to see – Hitokiri May 22 '20 at 08:46
  • Thanks for your answers. I understand how it works, but I think that when it prints "3 2 1" it still does not reach to the last line, so I think the output should be "3 2 1 kreni" because the code would reach to the last printf only when it can not enter the for loop. – Petar May 22 '20 at 09:00
  • @Petar `main` is called 4 times (1 in starting program, then 3 in for loop). So `printf("%sn%c ", str, 'i');` is reached in 4 times. – Hitokiri May 22 '20 at 09:04
  • @Petar Why would it not reach the last line? Which condition would stop the program from continuing after the `main` call or exiting the `for` loop? – Cheatah May 22 '20 at 09:05
  • @Cheatah Yes, do attention with `i = 0` in `for` loop. The value of `i` maybe indeterminate before `for` loop, but at starting of `for` loop, `i = 0` – Hitokiri May 22 '20 at 09:06