In C, how do I restrict the scope of a global variable to the file in which it's declared? -


I'm new to C I have a book that tells C's "File Scope", which includes sample code. But the code simply declares and initiates the scanned file - it does not verify the scope of the variable, trying to access it illegally. Hence! In the spirit of science, I created an experiment

file bar.c :.

  Fixed four fileScopedVariable [] = "asdf";  

File foo.c :

  #include & lt; Stdio.h & gt; #include "bar.c" main () {printf ("% s \ n", fileScopedVariable); }  

According to my book, and Google should call printf () - but it is not. Outputs foo.exe string "asdf" and ends normally. I would love to use file scoping, what am I missing?

You have #included bar.c, where the preprocessor should actually compile the contents of cc before the compiler touches it Make a copy.

Try to get rid of it, but ask your compiler to compile both files (like gcc foo.c bar. ) and seeing it Complain.

EDIT: I think the rules of language are between the primary illusion compiler and the preprocessor, and it is implemented by the compiler. Preprocessor runs before the compiler, and works on the commands that # prefixels. All preprocessors manipulate plain text does not parse this code or attempt to interpret the meaning of the code in any way. The "#include" instruction is very literal - it tells the preprocessor that "insert the contents of this file here" That's why you usually include # on .h (header) files only, and you have the function prototype in header files and External variable delivering only place. Otherwise, you'll compile similar functions, or define a single variable, many times, which is not legal.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -