Programming

(C) How do I share variables between different .c files?

steloflute 2012. 7. 14. 21:44

http://stackoverflow.com/questions/1045501/how-do-i-share-variables-between-different-c-files


In fileA.c:

int myGlobal = 0;

In fileA.h

extern int myGlobal;

In fileB.c:

#include "fileA.h"
myGlobal
= 1;

So this is how it works:

  • the variable lives in fileA.c
  • fileA.h tells the world that it exists, and what its type is (int)
  • fileB.c includes fileA.h so that the compiler knows about myGlobal before fileB.c tries to use it.



'Programming' 카테고리의 다른 글

Olly Betts: The Art of Writing Small Programs - OSDC 2011  (0) 2012.07.19
Portable Executable 101 - a windows executable walkthrough  (0) 2012.07.18
enum Types in C++  (0) 2012.07.13
(C++) shared_ptr  (0) 2012.07.10
Online syntax highlighting  (0) 2012.07.10