is it good or bad to reuse the variables?

后端 未结 9 2032
灰色年华
灰色年华 2020-12-17 15:03

I wonder if it is good or bad (or does not matter) if I reuse the variable names as much as possible? for example

int main(void){
  //...
  int x=0;

  //..
         


        
9条回答
  •  一向
    一向 (楼主)
    2020-12-17 15:18

    It is better to reuse variables in terms of memory. But be careful you don't need the value in a variable before reusing it. Other than that, you shouldn't use always the variable. It is important to keep a clean and readable code. So I advice you to choose different variable with names depending on the context so that your code don't become confusing.

    You should also take a look at dynamic memory allocation in C, which is very useful to manage memory and variables.

    https://en.wikipedia.org/wiki/C_dynamic_memory_allocation

提交回复
热议问题