<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>static | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/static/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Wed, 08 Jul 2020 14:49:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>static | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>How to stop access to Global variables in C</title>
		<link>https://learncgames.com/how-to-stop-access-to-global-variables-in-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-stop-access-to-global-variables-in-c</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Mon, 13 Jul 2020 23:00:29 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[global]]></category>
		<category><![CDATA[robust]]></category>
		<category><![CDATA[static]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=979</guid>

					<description><![CDATA[<p>If you have an application made up of multiple source and header files, it&#8217;s possible that you use global variables in those source files. Remember, these are variables declared outside of any function. Now you may not know it, but by default, those variables are visible to other files through what is known as external [&#8230;]</p>
The post <a href="https://learncgames.com/how-to-stop-access-to-global-variables-in-c/">How to stop access to Global variables in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:626,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/PIRO4D-2707530\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2364830&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260114060822\/https:\/\/pixabay.com\/users\/piro4d-2707530\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 16:50:06&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-01 10:14:22&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-29 01:53:57&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-03 12:51:31&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-07 22:09:05&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 19:42:50&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-21 22:20:27&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-21 22:20:27&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:627,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2364830&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 16:50:08&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-01 10:14:22&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-29 01:53:57&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-03 12:51:32&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-07 22:09:05&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 19:42:51&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-21 22:20:27&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-21 22:20:27&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_980" aria-describedby="caption-attachment-980" style="width: 300px" class="wp-caption alignleft"><img decoding="async" class="size-medium wp-image-980" src="https://learncgames.com/wp-content/uploads/2020/07/chain-2364830_640-300x157.jpg" alt="Links" width="300" height="157" srcset="https://learncgames.com/wp-content/uploads/2020/07/chain-2364830_640-300x157.jpg 300w, https://learncgames.com/wp-content/uploads/2020/07/chain-2364830_640.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-980" class="wp-caption-text">Image by <a href="https://pixabay.com/users/PIRO4D-2707530/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2364830">PIRO4D</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2364830">Pixabay</a></figcaption></figure>
<p>If you have an application made up of multiple source and header files, it&#8217;s possible that you use global variables in those source files. Remember, these are variables declared outside of any function.</p>
<p>Now you may not know it, but by default, those variables are visible to other files through what is known as <strong>external linkage</strong>.</p>
<p>Slap <em>extern</em> on the definition and the compiler and linker will happily use it. For instance in the Asteroids game, there is an extern int variable used in the file <strong>asteroids.c</strong>.</p>
<pre><code class="language-c" data-line="">// external variables
extern int errorCount;
</code></pre>
<p>This is declared in the source file<strong> lib.c</strong> and when all the modules are linked together the linker sees all the compiled symbols and figures out which refers to which.</p>
<h2>Making your program robust</h2>
<p>If you don&#8217;t want global variables in one file to be accessed from another, just add the keyword <em>static</em> in front of the declaration like this.</p>
<p><code class="language-c" data-line="">static int errorCount = 0; </code><br />
Now then, even though you&#8217;ve got the extern, when you compile it you&#8217;ll get an error. This is what Visual C++ says.</p>
<pre>Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol _errorCount asteroids D:\writing\Amazon EBooks\GamesProgrammer\publishing\Learn C Games Programming\Windows\Code\asteroids_ch48\asteroids\asteroids.obj 1 
Error LNK1120 1 unresolved externals asteroids D:\writing\Amazon EBooks\GamesProgrammer\publishing\Learn C Games Programming\Windows\Code\asteroids_ch48\asteroids\Debug\asteroids.exe 1</pre>
<p>So obviously only do this with global variables that are not going top be referred to. In this case we do refer to <em>errorCount</em> in <strong>Asteroids.</strong>c so adding the static would make no sense.</p>The post <a href="https://learncgames.com/how-to-stop-access-to-global-variables-in-c/">How to stop access to Global variables in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">979</post-id>	</item>
	</channel>
</rss>
