Home > Back-end >  Does calling main() inside main() create duplicate variables?
Does calling main() inside main() create duplicate variables?

Time:12-31

#include <stdio.h>

int main(){
    int num1, num2;
    char op;
    float answer;
    
    printf("This is a simple calculator. Input '0q0' to quit.\n");
    printf("Enter the arithmatic operation ('num1''op''num2'): ");
    scanf("%d%c%d", &num1, &op, &num2);
    
    switch(op){
        case ' ' : answer = num1   num2;
            break;
        case '-' : answer = num1 - num2;
            break;
        case '*' : answer = num1 * num2;
            break;
        case '/' : answer = (float)num1 / num2;
            break;
        case 'q' : return 0;
            break;
        default: printf("Invalid Operand");
            break;
    }
    
    printf("Answer is : %.2f", answer);
    fflush(stdin);
    main();
}

This is my code. Instead of using a while loop, I called main() function again at the end to make the program loop. Is this a good practice or a bad practice?

Once the program executes it's first round, main() is called again. So when it happens, will it use the old variables 'num1', 'num2', 'op' or does the program create new variables for the next round while the old variables are still there?

I wanted to use this method for one of my college projects but if duplicate variables are created every loop, it'll be a disaster. Cause I need to use about 200 - 500 structures with 13 variables in each.

CodePudding user response:

Is this a good practice or a bad practice?

Using recursion for something that can be easily solved with a loop is bad. If your implementation uses a stack (most do), you'll put more on the stack for each call and because the stack has a limited size, there's a limit to how deep the recursion can go before it runs out of stack space and most probably crashes.

will it use the old variables 'num1', 'num2', 'op' or does the program create new variables for the next round while the old variables are still there?

It will create a new set of variables for each call and the old ones are still there so to speak.

I wanted to use this method for one of my college projects but if duplicate variables are created every loop, it'll be a disaster.

Then, it's a disaster. Use a loop:

int main(void) {
    for(;;) {
        // your current code except the call to `main()`
    }
}

Notes:

  • fflush(stdin); is only defined to work in some implementations. In other implementations it'll have undefined behavior.
  • Always check the result of your scanfs.
    if (scanf("%d%c%d", &num1, &op, &num2) == 3) {/* ok input */}
    else {/* erroneous input */}
    

CodePudding user response:

It is bad practice, as it is not recommended to do it this way. Try and place your code in a function on it's own and call the function from main()
With recursion, usually, a lot of memory get used since variables will get allocated for each function call, and they will get released only when the function ends it's execution.

  • Related