<?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>variable | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/variable/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>Tue, 07 Jul 2020 16:01:11 +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>variable | 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>Do you declare variables at the top of functions</title>
		<link>https://learncgames.com/do-you-declare-variables-at-the-top-of-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=do-you-declare-variables-at-the-top-of-functions</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sat, 11 Jul 2020 23:00:26 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[C99]]></category>
		<category><![CDATA[variable]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=971</guid>

					<description><![CDATA[<p>This is for C99 and later. Prior to this, in function you had to declare all the variables that would be used in that function before any code. Since C99, you can declare them nearer to where they are used. This function is from the Asteroids game. The declaration of the two variables hx,hy could [&#8230;]</p>
The post <a href="https://learncgames.com/do-you-declare-variables-at-the-top-of-functions/">Do you declare variables at the top of functions</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;:631,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/TheDigitalArtist-202249\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1274888&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260202125618\/https:\/\/pixabay.com\/users\/thedigitalartist-202249\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 16:50:17&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-18 14:15:23&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-26 12:38:23&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-05 15:32:39&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-14 11:17:24&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-14 11:17:24&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:632,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1274888&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:24&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-18 14:15:23&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-26 12:38:23&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-05 15:32:39&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-14 11:17:22&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-14 11:17:22&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_972" aria-describedby="caption-attachment-972" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-972" src="https://learncgames.com/wp-content/uploads/2020/07/matrix-1274888_640-300x225.png" alt="Matrix code" width="300" height="225" srcset="https://learncgames.com/wp-content/uploads/2020/07/matrix-1274888_640-300x225.png 300w, https://learncgames.com/wp-content/uploads/2020/07/matrix-1274888_640.png 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-972" class="wp-caption-text">Image by <a href="https://pixabay.com/users/TheDigitalArtist-202249/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1274888">Pete Linforth</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1274888">Pixabay</a></figcaption></figure>
<p>This is for C99 and later. Prior to this, in function you had to declare all the variables that would be used in that function before any code. Since C99, you can declare them nearer to where they are used.</p>
<p>This function is from the Asteroids game. The declaration of the two variables hx,hy could have been moved to just before the do-loop. They can&#8217;t be declared inside the do-loop because they are used after it.</p>
<pre><code class="language-c" data-line="">// doHyperJump() find empty space on screen fo PlayerShip
void CheckJump() {
	int hx, hy;

	if (jumpFlag &amp;&amp; SDL_GetTicks() - jumpTimer &gt; 3000) { 
		jumpTimer = SDL_GetTicks();
		jumpFlag = 0;
		do {
			hx = 65 + Random(SCREENWIDTH - 130);
			hy = 65 + Random(SCREENHEIGHT - 130);
			if (IsEmptySpace(hx, hy)) break;
		} while (1);
		Player.x = (float)hx;
		Player.y = (float)hy;
		Player.vx = 0;
		Player.vy = 0;
	}
}</code></pre>
<p>This is one of those cases where it doesn&#8217;t really matter. But generally I will declare them close to where they are first used and I almost always declare the for-loop variable in the for-loop. Only if it&#8217;s needed afterwards do I declare it after, but that is a slightly iffy use unless you want to check the value it had when it exited the loop.</p>
<p>Remember the scope of the variable i.e. the part of the function in which the variable can be accessed starts just after it is declared. By keeping declarations near the code, you reduce the scope and this can help robustness.</p>The post <a href="https://learncgames.com/do-you-declare-variables-at-the-top-of-functions/">Do you declare variables at the top of functions</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">971</post-id>	</item>
	</channel>
</rss>
