Initialise a variable to its own undefined value

前端 未结 4 672
你的背包
你的背包 2021-02-04 01:50

In C, does initialising a variable to it\'s own value make sense? If yes, what for?

Allow me to elaborate. In Git sources there are some examples of initialising a varia

4条回答
  •  遇见更好的自我
    2021-02-04 02:31

    For me the only reason of such self-assigning initialization is to avoid a warning.

    In the case of your transport.c, I don't even understand why it is useful. I would have left cmp uninitialized.

    My own habit (at least in C) is to initialize all the variables, usually to 0. The compiler will optimize unneeded initialization, and having all variables initialized makes debugging easier.

    There is a case when I want a variable to remain uninitialized, and I might self-assign it: random seeds:

     unsigned myseed = myseed;
     srand(myseed);
    

提交回复
热议问题