Why are we not allowed to have assignment statements in the file scope in C?

前端 未结 2 1465
失恋的感觉
失恋的感觉 2020-12-02 01:33

Why are we only allowed to declare and define variables in global section?Why not include assignment in global section? Example:

#include
int         


        
2条回答
  •  广开言路
    2020-12-02 02:14

    For both questions the answer is the same, in file scope there is no execution of statements or evaluation of expressions, all is done at compile time.

    Other languages (C++ is an example) have a model for dynamic initialization at program startup. This is a complicated issue, e.g because initializers that come from different compilation units don't have a natural ordering among them, but might implicitly depend on each other. SO is an excelent source of information for this question, too.

    C tries to stay simple, simple to use for a programmer and simple to implement for compiler builders.

提交回复
热议问题