Is declaration of variables expensive?

前端 未结 12 966
长情又很酷
长情又很酷 2021-02-01 00:39

While coding in C, I came across the below situation.

int function ()
{
  if (!somecondition) return false;

  internalStructure  *str1;
  internalStructure *str         


        
12条回答
  •  [愿得一人]
    2021-02-01 00:45

    The best practice is to adapt a lazy approach, i.e., declare them only when you really need them ;) (and not before). It results in the following benefit:

    Code is more readable if those variables are declared as near to the place of usage as possible.

提交回复
热议问题