C89, Mixing Variable Declarations and Code

后端 未结 4 879
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 03:26

I\'m very curious to know why exactly C89 compilers will dump on you when you try to mix variable declarations and code, like this for example:

rutski@imac:~         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 04:09

    It is much easier to write a compiler for language which requires all variables to be declared at the start of function. Some languages even require variables to be declared in specific clause outside of function code (Pascal and Smalltalk come to mind).

    The reason is it's easier to map this variables to stack (or registers if your compiler is smart enough) if they are known and don't change.

    Any other statements (esp. function calls) may modify stack/registers, making variable mapping more complex.

提交回复
热议问题