Does anyone know the reason why the variables have to be defined at the top of function

前端 未结 5 766
遥遥无期
遥遥无期 2021-02-09 14:18

I have a question, does anyone know why the variables have to be defined initialized at the beginning of a function? Why can\'t you initialize or define variables in the

5条回答
  •  滥情空心
    2021-02-09 15:09

    That is a left-over from early C. C99 allows variables to be defined anywhere in the function, including in loop structures.

    for (int i = 0; i < 10; ++i) { int j; }
    

    The throwback is from when compilers needed to know the size of the stack for the function before they instantiated the function's code. As compilers became better the requirement just became annoying.

提交回复
热议问题