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
Computer program
(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==== [[C programming language]] (1973) got its name because the language [[BCPL]] was replaced with [[B (programming language)|B]], and [[AT&T Bell Labs]] called the next version "C". Its purpose was to write the [[UNIX]] [[operating system]].<ref name="cpl_3rd-ch2-37"/> C is a relatively small language, making it easy to write compilers. Its growth mirrored the hardware growth in the 1980s.<ref name="cpl_3rd-ch2-37"/> Its growth also was because it has the facilities of [[assembly language]], but uses a [[High-level programming language|high-level syntax]]. It added advanced features like: * [[inline assembler]]. * arithmetic on pointers. * pointers to functions. * bit operations. * freely combining complex [[Operators in C and C++|operators]].<ref name="cpl_3rd-ch2-37"/> [[File:Computer-memory-map.png|thumb|right|Computer memory map]] ''C'' allows the programmer to control which region of memory data is to be stored. [[Global variable]]s and [[static variable]]s require the fewest [[clock cycle]]s to store. The [[call stack|stack]] is automatically used for the standard variable [[Declaration (computer programming)|declarations]]. [[Manual memory management|Heap]] memory is returned to a [[pointer variable]] from the [[C dynamic memory allocation|<code>malloc()</code>]] function. * The ''global and static data'' region is located just above the ''program'' region. (The program region is technically called the ''text'' region. It is where machine instructions are stored.) :* The global and static data region is technically two regions.<ref name="geeksforgeeks">{{cite web | url = https://www.geeksforgeeks.org/memory-layout-of-c-program/ | title = Memory Layout of C Programs | date = 12 September 2011 | access-date = 6 November 2021 | archive-date = 6 November 2021 | archive-url = https://web.archive.org/web/20211106175644/https://www.geeksforgeeks.org/memory-layout-of-c-program/ | url-status = live }}</ref> One region is called the ''initialized [[data segment]]'', where variables declared with default values are stored. The other region is called the ''[[.bss|block started by segment]]'', where variables declared without default values are stored. :* Variables stored in the ''global and static data'' region have their [[Memory address|addresses]] set at compile time. They retain their values throughout the life of the process. :* The global and static region stores the ''global variables'' that are declared on top of (outside) the <code>main()</code> function.<ref name="cpl-ch1-p31">{{cite book |title=The C Programming Language Second Edition |last1=Kernighan |first1=Brian W. |last2=Ritchie |first2=Dennis M. |publisher=Prentice Hall |year=1988 |isbn=0-13-110362-8 |page=31}}</ref> Global variables are visible to <code>main()</code> and every other function in the source code. : On the other hand, variable declarations inside of <code>main()</code>, other functions, or within <code>{</code> <code>}</code> [[Block (programming)|block delimiters]] are ''local variables''. Local variables also include ''[[formal parameter]] variables''. Parameter variables are enclosed within the parenthesis of a function definition.<ref name="cpl_3rd-ch6-128">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 128 | isbn = 0-201-71012-9 }}</ref> Parameters provide an [[Interface (computing)|interface]] to the function. :* ''Local variables'' declared using the <code>static</code> prefix are also stored in the ''global and static data'' region.<ref name="geeksforgeeks"/> Unlike global variables, static variables are only visible within the function or block. Static variables always retain their value. An example usage would be the function <code>int increment_counter(){static int counter = 0; counter++; return counter;}</code>{{efn|This function could be written more concisely as <code>int increment_counter(){ static int counter; return ++counter;}</code>. 1) Static variables are automatically initialized to zero. 2) <code>++counter</code> is a prefix [[increment operator]].}} * The [[call stack|stack]] region is a contiguous block of memory located near the top memory address.<ref name="lpi-ch6-p121">{{cite book |title=The Linux Programming Interface |last=Kerrisk |first=Michael |publisher=No Starch Press |year=2010 |isbn=978-1-59327-220-3 |page=121}}</ref> Variables placed in the stack are populated from top to bottom.{{efn|This is despite the metaphor of a ''stack,'' which normally grows from bottom to top.}}<ref name="lpi-ch6-p121"/> A [[Call stack#STACK-POINTER|stack pointer]] is a special-purpose [[processor register|register]] that keeps track of the last memory address populated.<ref name="lpi-ch6-p121"/> Variables are placed into the stack via the ''assembly language'' PUSH instruction. Therefore, the addresses of these variables are set during [[Runtime (program lifecycle phase)|runtime]]. The method for stack variables to lose their [[Scope (computer science)|scope]] is via the POP instruction. :* ''Local variables'' declared without the <code>static</code> prefix, including formal parameter variables,<ref name="lpi-ch6-p122">{{cite book |title=The Linux Programming Interface |last=Kerrisk |first=Michael |publisher=No Starch Press |year=2010 |isbn=978-1-59327-220-3 |page=122}}</ref> are called ''automatic variables''<ref name="cpl-ch1-p31"/> and are stored in the stack.<ref name="geeksforgeeks"/> They are visible inside the function or block and lose their scope upon exiting the function or block. * The [[Manual memory management|heap]] region is located below the stack.<ref name="geeksforgeeks"/> It is populated from the bottom to the top. The [[operating system]] manages the heap using a ''heap pointer'' and a list of allocated memory blocks.<ref name="cpl-ch1-p185">{{cite book |title=The C Programming Language Second Edition |last1=Kernighan |first1=Brian W. |last2=Ritchie |first2=Dennis M. |publisher=Prentice Hall |year=1988 |isbn=0-13-110362-8 |page=185}}</ref> Like the stack, the addresses of heap variables are set during runtime. An [[out of memory]] error occurs when the heap pointer and the stack pointer meet. :* ''C'' provides the <code>malloc()</code> library function to [[C dynamic memory allocation|allocate]] heap memory.{{efn|''C'' also provides the <code>calloc()</code> function to allocate heap memory. It provides two additional services: 1) It allows the programmer to create an [[Array (data structure)|array]] of arbitrary size. 2) It sets each [[Memory cell (computing)|memory cell]] to zero.}}<ref name="cpl-ch8-p187">{{cite book |title=The C Programming Language Second Edition |last1=Kernighan |first1=Brian W. |last2=Ritchie |first2=Dennis M. |publisher=Prentice Hall |year=1988 |isbn=0-13-110362-8 |page=187}}</ref> Populating the heap with data is an additional copy function.{{efn|For [[String (computer science)|string]] variables, ''C'' provides the <code>strdup()</code> function. It executes both the allocation function and the copy function.}} Variables stored in the heap are economically passed to functions using pointers. Without pointers, the entire block of data would have to be passed to the function via the stack.
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)