'problem with trying to exit a program using a function in C

i have this function:

void Win_Event(int player, int x, int y, int mat[][MAX]) {
    int choice, i, j;
    for (i = 0; i < y; i++) {
        for (j = 0; j < x; j++) {
            printf("\033[1;31m");
            if (mat[i][j] == -1)
                printf("\033[0;32m");
            else if (mat[i][j] == 0)
                printf("\033[0;35m");
            printf("%3d |", mat[i][j]);
        }
        printf("\n");
    }
    printf("\033[1;31m");
    printf("\n\033[0;36mPlayer %d Won!\nWould you like to replay? (1 -> yes, 2-> no):", player);
    scanf_s("%d", &choice);
    main(choice);
}

it basically checks if the player won then asks him if he wants to replay the game and sends that input back to the main function. the main function looks like this:

void main(int choice) {
    printf("\033[0;33m");
    printf("---Welcome To Pente---\n");
    if (choice == 1)
        SetBoardSize();
    else
        return 0;
}

the program just ignored the choice parameter completely and runs the SetBoardSize function straight away is there something i am missing?

c


Solution 1:[1]

main() is a special kind of function in C, with special rules.

It always the compiler and never the programmer that decides what forms of main() that are supported.

If your compiler does not support void main(int choice) (which it likely does not), then you cannot use that form, period. If you wish to deviate from the two standardized forms int main (void) and int main (int argc, char* argv[]), then you must read the compiler's documentation about implementation-defined forms to see what it does support (if any). You can't just cook up something yourself.

In addition, calling main() manually is allowed in C, but it is a very bad idea and there exists no sensible scenario where you should do that. It will cause you to stack a bunch of extra overhead memory recursively.

Solution 2:[2]

First I suggest you not to recur on main(), because it isn't a good coding practice and you don't have any reason to do that (move the recursive part of your program in an another function). Second if you really want to use the same code design of now and you need to exit when return 0 is called, you should replace it with:

exit(EXIT_SUCCESS);

from stdlib.h

Moreover, remember that recursion can lead to an stack overflow, take care of it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lundin
Solution 2