How do I share a global variable between c files?

前端 未结 7 1131
一生所求
一生所求 2020-12-07 18:28

If i define a global variable in a .c file, how can i use the value of the same variable in another .c file?

file1.c

#incl         


        
7条回答
  •  无人及你
    2020-12-07 19:27

    If you want to use global variable i of file1.c in file2.c, then below are the points to remember:

    1. main function shouldn't be there in file2.c
    2. now global variable i can be shared with file2.c by two ways:
      a) by declaring with extern keyword in file2.c i.e extern int i;
      b) by defining the variable i in a header file and including that header file in file2.c.

提交回复
热议问题