vendredi 31 juillet 2015

Using a variable in its own declaration in recursion

Using a local variable in its own initialization usually has no effect but doing it with recursion causes strange values.

#include <stdio.h>

void recurse(int count);

int main()
{
    int j = j + 1; // a weird thing to do but its just 1
    printf("%d\n", j);
    j = j + 1;
    printf("%d\n", j);
    j = j + 1;
    printf("%d\n", j);

    recurse(1);

    printf("\n");
}

void recurse(int count){
    int i = i + 1; // the really weird part
    printf("%d ", i);

    if(count < 10)
        recurse(count + 1);
    return;
}

Output:

1
2
3
32737 1 32737 1 1 1 1 1 1 1

The large numbers are not always the same for each execution.

Aucun commentaire:

Enregistrer un commentaire