where to declare structures, inside main() or outside main()?

前端 未结 3 1947
春和景丽
春和景丽 2021-02-04 12:39

Case 1: structure declared outside main() working fine

#include
#include
struct prod
{
    int price,         


        
3条回答
  •  天涯浪人
    2021-02-04 13:02

    where to declare structures, inside main() or outside main()?

    1. First thing, I think you meant "define", not "declare".

    2. Second, there is no rule as such, You can define wherever you want. It is all about the scope of the definition.

      • If you define the structure inside main(), the scope is limited to main() only. Any other function cannot see that definition and hence, cannot make use of that structure definition.

      • If you define the structure in a global scope, (i.e., outside main() or any other function, for that matter), that definition is available globally and all the functions can see and make use of the structure definition.

提交回复
热议问题