I found this in the help files when I looked up 'extern'. What does it mean?
The blue bits are the bits that I don't understand.Quote:
Declarations of variables and functions at file scope are external by default.
Thx,
Philipp
Printable View
I found this in the help files when I looked up 'extern'. What does it mean?
The blue bits are the bits that I don't understand.Quote:
Declarations of variables and functions at file scope are external by default.
Thx,
Philipp
File-scope means that the scope of the variable (where the variable can be used/seen) is the entire file - this means "global" variables in a file. Variables can have limited scope, e.g. be within a function or even a single block inside a function, eg.
Edit: external means that the function/variable can be seen in another file, eg. you have "main.c" and "func.c", and declared x in main.c as above, then you can tell the compiler that there is an [code]extern int x;[/code} in func.c and it becomes the same variable as main.c. Functions don't need the extern keyword to make the function usable in another file, you just have to tell the compiler the prototype (what function name is and what the return type and parameters are) to use it.Code:int x; // file scope
int func1(int y) // function scope parameter.
{
int z; // function scope local variable.
if (x == 1)
{
int w; // Block scope
}
// w doesn't exist here.
}
--
Mats
Just means that if you declare variables and functions in one compilation unit ( source file ) they are accessible from other compilation units by default.
If you dont want them to be acessible from other compilation units, declare as static.
Kurt
Thank you. Explains why there was no error when I used a function defined in another file... The declaration was in a header though which I included in the main file where I used the function.
Philipp