Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scope (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== C === {{main article|Linkage (software)}} In C, scope is traditionally known as '''[[Linkage (software)|linkage]]''' or '''visibility''', particularly for variables. C is a lexically scoped language with global scope (known as ''external linkage''), a form of module scope or file scope (known as ''internal linkage''), and local scope (within a function); within a function scopes can further be nested via block scope. However, standard C does not support nested functions. The lifetime and visibility of a variable are determined by its [[storage class]]. There are three types of lifetimes in C: static (program execution), automatic (block execution, allocated on the stack), and manual (allocated on the heap). Only static and automatic are supported for variables and handled by the compiler, while manually allocated memory must be tracked manually across different variables. There are three levels of visibility in C: external linkage (global), internal linkage (roughly file), and block scope (which includes functions); block scopes can be nested, and different levels of internal linkage is possible by use of includes. Internal linkage in C is visibility at the [[Translation unit (programming)|translation unit]] level, namely a source file after being processed by the [[C preprocessor]], notably including all relevant includes. C programs are compiled as separate [[object file]]s, which are then linked into an executable or library via a [[Linker (computing)|linker]]. Thus name resolution is split across the compiler, which resolves names within a translation unit (more loosely, "compilation unit", but this is properly a different concept), and the linker, which resolves names across translation units; see [[Linkage (software)|linkage]] for further discussion. In C, variables with block scope enter context when they are declared (not at the top of the block), go out of context if any (non-nested) function is called within the block, come back into context when the function returns, and go out of context at the end of the block. In the case of automatic local variables, they are also allocated on declaration and deallocated at the end of the block, while for static local variables, they are allocated at program initialization and deallocated at program termination. The following program demonstrates a variable with block scope coming into context partway through the block, then exiting context (and in fact being deallocated) when the block ends: <syntaxhighlight lang="c"> #include <stdio.h> int main(void) { char x = 'm'; printf("%c\n", x); { printf("%c\n", x); char x = 'b'; printf("%c\n", x); } printf("%c\n", x); } </syntaxhighlight> The program outputs: <pre> m m b m </pre> There are other levels of scope in C.<ref>"[http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fzexscope_c.htm Scope]", ''XL C/C++ V8.0 for Linux,'' IBM</ref> Variable names used in a function prototype have function prototype visibility, and exit context at the end of the function prototype. Since the name is not used, this is not useful for compilation, but may be useful for documentation. Label names for GOTO statement have function scope.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)