The Geeky Way - C#//thegeekyway.com/2019-08-24T00:46:59+05:30MD5 Hashing using C#2019-08-24T00:46:59+05:302019-08-24T00:46:59+05:30sanyam_khuranatag:thegeekyway.com,2019-08-24:/md5-hashing-using-c/<p>Its common now a days that all the passwords are being encrypted in order to prevent data theft. So the passwords are being encrypted, the most common and most secure encryption method is MD5 hashing.<br>
<strong>MD5</strong> hashing stands for Message Digest algorithm 5. The idea of MD5 algorithm is to …</p><p>Its common now a days that all the passwords are being encrypted in order to prevent data theft. So the passwords are being encrypted, the most common and most secure encryption method is MD5 hashing.<br>
<strong>MD5</strong> hashing stands for Message Digest algorithm 5. The idea of MD5 algorithm is to convert all the string/char/integer into a fixed “32″ bit hexadecimal code. The input can be of any size, but the output will always be fixed, here is an example of MD5 hashing algorithm.</p>
<p><img alt="MD5 hashing algorithm" src="http://thegeekyway.com/uploads/images/2014/03/MD5-hashing-algorithm.png"></p>
<p>The main point is that whatever the length of the input is, the output will always be of “32″ bit. The namespace used is <strong>System.Security.Cryptography</strong> and the assembly used is <strong>mscorlib (in mscorlib.dll)</strong>.<br>
The first step in creating a MD5 encryption using C# is :- </p>
<h2>Step 1 : Include the required namespace.</h2>
<div class="highlight"><pre><span></span><code><span class="nv">using</span><span class="w"> </span><span class="nv">System</span>.<span class="nv">Security</span>.<span class="nv">Cryptography</span><span class="c1">;</span>
<span class="nv">using</span><span class="w"> </span><span class="nv">System</span>.<span class="nv">Text</span><span class="c1">;// for stringbuilder class</span>
</code></pre></div>
<h2>Step 2 : Create an object of MD5 class.</h2>
<div class="highlight"><pre><span></span><code>MD5 md5H=MD5.Create();
</code></pre></div>
<h2>Step 3 : Convert the string into byte array and compute its hash.</h2>
<div class="highlight"><pre><span></span><code>byte[]data=md5H.ComputeHash(Encoding.UTF8.GetBytes(text));
</code></pre></div>
<h2>Step 4 : Loop through each byte of hashed data and format each one as a hexadecimal string</h2>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o"><</span><span class="k">data</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="n">sBuilder</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="k">data</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">ToString</span><span class="p">(</span><span class="ss">"x2"</span><span class="p">));</span>
</code></pre></div>
<p>The whole combined C# coding is</p>
<div class="highlight"><pre><span></span><code><span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Collections</span><span class="p">.</span><span class="n">Generic</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Linq</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">.</span><span class="n">UI</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">.</span><span class="n">UI</span><span class="p">.</span><span class="n">WebControls</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Security</span><span class="p">.</span><span class="n">Cryptography</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="nc">Text</span><span class="p">;</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">partial</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nl">MD5Hashing</span><span class="p">:</span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">.</span><span class="n">UI</span><span class="p">.</span><span class="n">Page</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">MD5Hash</span><span class="p">(</span><span class="n">string</span><span class="w"> </span><span class="nc">text</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">MD5</span><span class="w"> </span><span class="n">md5H</span><span class="o">=</span><span class="n">MD5</span><span class="p">.</span><span class="k">Create</span><span class="p">();</span>
<span class="w"> </span><span class="o">//</span><span class="nf">convert</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">byte</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="k">compute</span><span class="w"> </span><span class="n">its</span><span class="w"> </span><span class="n">hash</span>
<span class="w"> </span><span class="n">byte</span><span class="err">[]</span><span class="k">data</span><span class="o">=</span><span class="n">md5H</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">Encoding</span><span class="p">.</span><span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="nc">text</span><span class="p">));</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="k">create</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">stringbuilder</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">collect</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">bytes</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="k">create</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">string</span>
<span class="w"> </span><span class="n">StringBuilder</span><span class="w"> </span><span class="n">sB</span><span class="o">=</span><span class="k">new</span><span class="w"> </span><span class="n">StringBuilder</span><span class="p">();</span>
<span class="w"> </span><span class="o">//</span><span class="n">loop</span><span class="w"> </span><span class="n">through</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">byte</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">hashed</span><span class="w"> </span><span class="k">data</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nf">format</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">one</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">hexadecimal</span><span class="w"> </span><span class="n">string</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o"><</span><span class="k">data</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">sB</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="k">data</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">ToString</span><span class="p">(</span><span class="ss">"x2"</span><span class="p">));</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="o">//</span><span class="k">return</span><span class="w"> </span><span class="n">hexadecimal</span><span class="w"> </span><span class="n">string</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">sB</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
</code></pre></div>
<p><img alt="MD5 hashing output" src="http://thegeekyway.com/uploads/images/2014/03/MD5-hashing-output.png"></p>Structures And User Defined Data Types (Part - I)2014-08-05T00:17:00+05:302014-08-05T00:17:00+05:30sanyam_khuranatag:thegeekyway.com,2014-08-05:/structures-c-programming-language-user-defined-data-types/<p>Howdy folks, hope you're doing well. We've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D Arrays and their memory organization</a> and further about <a href="http://www.thegeekyway.com/learning-2d-multidimensional-arrays/" title="Learning About 2D And Multidimensional Arrays">2D arrays and multidimensional arrays</a>. In case you missed it, read them first. Coming to this tutorial, we'll be discussing the way to develop our own data types in the C …</p><p>Howdy folks, hope you're doing well. We've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D Arrays and their memory organization</a> and further about <a href="http://www.thegeekyway.com/learning-2d-multidimensional-arrays/" title="Learning About 2D And Multidimensional Arrays">2D arrays and multidimensional arrays</a>. In case you missed it, read them first. Coming to this tutorial, we'll be discussing the way to develop our own data types in the C programming language. It is done through something known as structures, and that's what we'll explore about here.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/08/structures-and-user-defined-data-types-I.png" title="Structure and User Defined Data Types in C Programming"><img alt="Structure and User Defined Data Types in C Programming" src="//thegeekyway.com/uploads/images/2014/08/structures-and-user-defined-data-types-I.png"></a></p>
</div>
<h2>What is a structure in C ?</h2>
<p>Structure is basically a derived data type in C programming language. It is defined as a collection of variables (which may be of different data types) to be grouped together and referred to as a single entity. This gives you ability to define your own data types i.e. user defined data types.</p>
<h2>Difference between a structure and an array?</h2>
<p>The next question which may arise, would be what's the difference between array and structure. Well, an array is homogeneous set of elements i.e. array is capable of holding different values but of same data type. On the other hand, structure is combination of variables of different data types.</p>
<p>Structures can be used to track information which is common to large group of entities.</p>
<p><strong>How to declare a structure?</strong></p>
<p>Let us first see the basic semantics of declaring a structure. It is much like defining ordinary variable like:</p>
<div class="highlight"><pre><span></span><code><span class="o"><</span><span class="n">data_type</span><span class="o">></span><span class="w"> </span><span class="o"><</span><span class="n">variable_name</span><span class="o">></span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">For</span><span class="w"> </span><span class="n">variable</span>
</code></pre></div>
<p>Now for structure, it is done through keyword struct like this:</p>
<div class="highlight"><pre><span></span><code>struct <struct_name>
{
...
};
</code></pre></div>
<p>Let us take an example to track record of students in a high school. Each student would have certain set of properties/attributes which would differentiate them from one another. We'll consider a student has a roll number of type integer , name of type string and marks of type float for the time being. Now, let's define our structure named student with these properties.</p>
<div class="highlight"><pre><span></span><code>struct student
{
int rollno;
char name[20]; // Taking an array of character (string)
float marks;
// You are free to declare as many members as you want, but we're restricting our example to 3 data members (properties)
// Size of the structure should be known to the compiler
};
</code></pre></div>
<p>Now that we have our basic structure with it's data members ready, we can declare instances of it like:</p>
<div class="highlight"><pre><span></span><code>struct student George;
</code></pre></div>
<p>Now, all members of the structure can be accessed via our instance named George using membership access or dot operator like:</p>
<div class="highlight"><pre><span></span><code>George.rollno = 91;
George.marks = 78.6;
</code></pre></div>
<p>and so on...</p>
<p>It's essential to remember here that the members we declare in a structure would take the memory only when instances are declared. You're free to name your instance anything. It's just like declaring a variable whose data type would be now your own defined data type, which is struct student here.</p>
<p>Notice above how we've defined the instances of a structure, another way of declaring them can be like this:</p>
<div class="highlight"><pre><span></span><code><span class="n">struct</span><span class="w"> </span><span class="o"><</span><span class="n">struct</span><span class="o">-</span><span class="n">name</span><span class="o">></span>
<span class="p">{</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Data</span><span class="w"> </span><span class="n">Members</span>
<span class="p">}</span><span class="w"> </span><span class="o"><</span><span class="n">name</span><span class="o">-</span><span class="n">of</span><span class="o">-</span><span class="n">first</span><span class="o">-</span><span class="n">structure</span><span class="o">-</span><span class="n">variable</span><span class="o">></span><span class="w"> </span><span class="o">...</span><span class="w"> </span><span class="p">;</span>
</code></pre></div>
<p>So, for the above example, it can be done as follows:</p>
<div class="highlight"><pre><span></span><code>struct student
{
int rollno;
char name[20]; // Taking an array of character (string)
float marks;
}George,Henry,Steve; // Declare as many as you want.
</code></pre></div>
<p>Next, you can access each of the properties for a particular structure variable like :</p>
<div class="highlight"><pre><span></span><code>George.rollno = 50;
George.marks = 98.6;
strycpy(George.name, "George");
</code></pre></div>
<p>Similarly all other structure variables' data members can be defined.</p>
<p>That's solve for this tutorial, a lot more is there to know about structure, which we'll cover in the second part.</p>Learning About 2D And Multidimensional Arrays2014-07-24T13:04:00+05:302014-07-24T13:04:00+05:30sanyam_khuranatag:thegeekyway.com,2014-07-24:/learning-2d-multidimensional-arrays/<p>Well, I'm glad you reached at this point, we've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D arrays and the memory organization</a>, and in this post, we'll know about 2D arrays and Multidimensional Arrays. They are not very much different from 1 D arrays as we looked in the last tutorial. So, let's start …</p><p>Well, I'm glad you reached at this point, we've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D arrays and the memory organization</a>, and in this post, we'll know about 2D arrays and Multidimensional Arrays. They are not very much different from 1 D arrays as we looked in the last tutorial. So, let's start:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/2D_mutlidimensional_arrays.png" title="Learning about 2D and multidimensional arrays in C"><img alt="Learning about 2D and multidimensional arrays in C" src="//thegeekyway.com/uploads/images/2014/07/2D_mutlidimensional_arrays.png"></a></p>
</div>
<p>In this tutorial, we'll cover the following topics:</p>
<ul>
<li>
<p><a href="http://www.TheGeekyWay.com/learning-2d-multidimensional-arrays/#2d_arrays"><strong><em>2 Dimensional Arrays (2D Arrays)</em></strong></a></p>
<ul>
<li><a href="http://www.TheGeekyWay.com/learning-2d-multidimensional-arrays/#2d_declare"><strong><em>Declaration of 2D Arrays</em></strong></a></li>
<li><a href="#2d_input"><strong><em>Inputting Data in 2D Arrays</em></strong></a></li>
<li><a href="#2d_output"><strong><em>Traversing and Output from 2D Array</em></strong></a></li>
</ul>
</li>
<li>
<p><a href="#multidimensional_arrays"><strong><em>Multidimensional Arrays</em></strong></a></p>
<ul>
<li><a href="#multidimensional_declare"><strong><em>Declaration of Multidimensional Arrays</em></strong></a></li>
<li><a href="#multidimensional_input"><strong><em>Inputting Data in Multidimensional Arrays</em></strong></a></li>
<li><a href="#multidimensional_output"><strong><em>Traversing and output from Multidimensional Arrays</em></strong></a></li>
</ul>
</li>
</ul>
<h2 id="dimensional-arrays" style="text-align: center;"><strong>2 Dimensional Arrays</strong></h2>
<p>Without saying anything much about 2D arrays here, let's first have a look at the memory organization in 2D arrays:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/2DArrays_Representation.png" title="Memory Organization of 2D Arrays in memory"><img alt="Memory Organization of 2D Arrays in memory" src="//thegeekyway.com/uploads/images/2014/07/2DArrays_Representation.png"></a></p>
</div>
<p>Clearly, it's much like 1D arrays but this time the contiguous memory blocks have to be occupied in two directions considered as along x and y co-ordinates.</p>
<p>With this basic memory organization picture in mind, let's now see how we declare a 2D Array.</p>
<p #2d_declare></p>
<p><strong>Declaration of 2D Array:</strong></p>
<p>Let's take an example that you want to create a 3 x 3 Matrix named mat of integer data type, so we would declare as:</p>
<div class="highlight"><pre><span></span><code>int mat[3][3];
</code></pre></div>
<p>The first number in square brackets is for the number of rows and the next one is for number of columns. For instance, now say we need to create a matrix of say 4 rows and 6 columns, of float data type. it would look like this:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/mat_4x6.png" title="Matrix memory representation 4 X 6"><img alt="Matrix memory representation 4 X 6" src="//thegeekyway.com/uploads/images/2014/07/mat_4x6.png"></a></p>
</div>
<p>Now, it would be declared as follows:</p>
<div class="highlight"><pre><span></span><code>float mat[4][6];
</code></pre></div>
<p>Now let us come back to our original mat, which is a 3 x 3 matrix and see how to fill it.</p>
<p #2d_input></p>
<p><strong>Putting in data to 2D Arrays:</strong></p>
<p>Since we need to refer a location by both the row number and the column number, we need to fill in each block of memory by traversing through each row first and then each column in that row, then come to next row, traverse each column and so on...</p>
<p>Here we would need two loops, one for updating the row number and the next to update the column number. Since we are moving from left to right for each row, remember that we will update the row value less frequently than the column value.</p>
<p>So, basic syntax would be something like this:</p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="k">row</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">col</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="o">&</span><span class="n">matrix</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>Here row and col would correspond to the total number of rows and columns in the matrix. Inside the loop, we're just taking input from the user such that the value of <strong>j</strong> i.e. our column would update more frequently from left to right than the value of <strong>i</strong> i.e. our row.</p>
<p>Now, let's start accepting values from user for our for our matrix named mat which is a 3 x 3 matrix.</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">mat</span><span class="o">[</span><span class="n">3</span><span class="o">][</span><span class="n">3</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Declaring</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="n">D</span><span class="w"> </span><span class="n">matrix</span>
<span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Declaring</span><span class="w"> </span><span class="n">variables</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">used</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">loop</span>
<span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">updating</span><span class="w"> </span><span class="k">row</span><span class="w"> </span><span class="n">number</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">updating</span><span class="w"> </span><span class="n">col</span><span class="w"> </span><span class="n">number</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="o">&</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p #2d_output></p>
<p><strong>Traversing and Printing 2D Array:</strong></p>
<p>Next, we need to traverse this array, it would be done as:</p>
<p>Traversing means just processing through each element of the array and apply an operation.</p>
<div class="highlight"><pre><span></span><code><span class="o">//</span><span class="w"> </span><span class="n">We</span><span class="w"> </span><span class="n">have</span><span class="w"> </span><span class="n">assumed</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">initialized</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="n">taking</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="k">as</span>
<span class="o">//</span><span class="w"> </span><span class="n">shown</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">code</span><span class="w"> </span><span class="n">snippet</span><span class="w"> </span><span class="n">above</span><span class="p">.</span>
<span class="o">//</span><span class="w"> </span><span class="n">Traversing</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="o">//</span><span class="n">Traversing</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">applying</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">printing</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">console</span><span class="w"> </span><span class="k">operation</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<h2 id="multidimensional-array" style="text-align: center;"><strong>Multidimensional Array :</strong></h2>
<p>Extending what we've learned about 1D and 2D arrays, there can be 3D, 4D, 5D and even 100D arrays, though the complexity would multiply at each level, so we don't use beyond 2D arrays generally. In some complex application, we may use 3D arrays, but beyond that, because of the increase in the complexity, they are hardly used.</p>
<p #multidimensional_declare></p>
<p><strong>Declaration of Multidimensional Arrays:</strong></p>
<p>So, for declaring a 3 D array, you just have to define one more dimension as follows:</p>
<div class="highlight"><pre><span></span><code>int matrix [3][3][3]; //Declaring 3D Array which is 3 x 3 x 3
</code></pre></div>
<p>Similarly, for a 4 D array, declaration would be :</p>
<div class="highlight"><pre><span></span><code>int matrix [3][4][5][6]; // Declaring Matrix named matrix of type int and
// size 3 x 4 x 5 x 6
</code></pre></div>
<p>So, generally, whatever dimension you want, you can add while declaring.</p>
<p #multidimensional_input></p>
<p><strong>Putting in data to Multidimensional Array:</strong></p>
<p>Very similar to filling up a 2 D array, a 3D array can be filled up by using 3 loops (one for iterating through each dimension). Remember, the inner loop will always has a fast frequency to iterate than the loop enclosing it and so on...</p>
<p>Let's fill up a 3D array of type float here as an example:</p>
<div class="highlight"><pre><span></span><code><span class="nc">float</span><span class="w"> </span><span class="n">mat</span><span class="o">[</span><span class="n">3</span><span class="o">][</span><span class="n">4</span><span class="o">][</span><span class="n">5</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Declaring</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="n">mat</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">type</span><span class="w"> </span><span class="nc">float</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="n">dimensions</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="mi">5</span>
<span class="o">//</span><span class="w"> </span><span class="n">Filling</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="k">data</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="n">taking</span><span class="w"> </span><span class="k">values</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span>
<span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="o">//</span><span class="n">Taking</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="k">corresponding</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">dimension</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%f"</span><span class="p">,</span><span class="o">&</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">][</span><span class="n">k</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>Similarly, just increase the number of loops depending on the dimensions. Thus, if you have created an N dimension array, it would take N loops to fill it.</p>
<p #multidimensional_output></p>
<p><strong>Traversing and Output of a Multidimensional Array:</strong></p>
<p>In the same way as explained above, you can print a multidimensional array by iterating through it using the same no. of loops as the dimension of the array as:</p>
<div class="highlight"><pre><span></span><code><span class="o">//</span><span class="w"> </span><span class="n">This</span><span class="w"> </span><span class="n">snippet</span><span class="w"> </span><span class="n">assumes</span><span class="w"> </span><span class="n">that</span><span class="w"> </span><span class="n">we</span><span class="err">'</span><span class="n">ve</span><span class="w"> </span><span class="n">declared</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">filled</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">shown</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">above</span><span class="p">.</span>
<span class="o">//</span><span class="w"> </span><span class="n">Traversing</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="k">output</span>
<span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="o">//</span><span class="n">Taking</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="k">corresponding</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">dimension</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="ss">"%f"</span><span class="p">,</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">][</span><span class="n">k</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>I hope this had been a bit informative for you and would help you. Next, we'll be going to discuss about Structures in C language.</p>
<p>Have any doubts? Just leave a comment below or give me a shout on twitter \@ErSanyamKhurana</p>Learning About 1 Dimensional Arrays (1 D Arrays)2014-07-12T02:55:00+05:302014-07-12T02:55:00+05:30sanyam_khuranatag:thegeekyway.com,2014-07-12:/learning-1-dimensional-arrays/<p>You may have heard of arrays or "tuples" before, if you've learnt any programming language. If not, then don't worry, you'll learn everything about them in this tutorial and how are they used in C to handle <strong>large amounts of data of same data type</strong> effectively and easily. So, let's …</p><p>You may have heard of arrays or "tuples" before, if you've learnt any programming language. If not, then don't worry, you'll learn everything about them in this tutorial and how are they used in C to handle <strong>large amounts of data of same data type</strong> effectively and easily. So, let's know about them. In this tutorial we'll learn about 1 Dimensional Arrays and in later tutorials we'll look at 2 Dimensional Arrays and Multidimensional arrays.</p>
<h2 id="what-is-an-array" style="text-align: center;"><strong>What is an array?</strong></h2>
<p><em>An array can be defined as a way to store list of large no. of items of same data type in contiguous memory location</em>. Whenever we need to store some information about the same data type, we use arrays. For instance, consider a case when you need to store roll numbers of all students in the class. One way to do this is, I can define separately variables for all roll numbers like:</p>
<div class="highlight"><pre><span></span><code>...
int rollno1 = 1;
int rollno2 = 2;
int rollno3 = 3;
int rollno4 = 4;
.
.
.
.
.
.
.
.
.
int rollnon = n; //Here n is the last roll number
</code></pre></div>
<p>Surely, this would be an inefficient approach, and thus, a better way to do this is would be through arrays like this:</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">rollno</span><span class="o">[</span><span class="n">n</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Here</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="n">number</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">students</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">roll</span><span class="w"> </span><span class="n">numbers</span><span class="w"> </span><span class="k">are</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">stored</span><span class="p">.</span>
</code></pre></div>
<p>I know, you haven't been introduced to "How to declare arrays", which we would see in just a moment, but I just wanted to show how systematically you can use array to store and manage data efficiently. Below is an image describing how array is stored in the memory location. Note here, that each item can be accessed via index number (explained in detail later), and remember, this index value as shown would always start from 0. Many errors, newbie programmers fall trap to, is because they confuse the first index of the array to be 1, which is not the case, as the first index is always 0.</p>
<p>The contiguous memory means, the data cells of memory would be one after the other, so if we need to store 60 items, we need a location in memory with at least 60 cells one after the other. Consider contiguous as being "continuous".</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/arrays11.png" title="Array representation in memory."><img alt="Array representation in memory." src="//thegeekyway.com/uploads/images/2014/07/arrays11.png"></a></p>
</div>
<h2 id="how-to-declare-a-1-dimensional-array" style="text-align: center;">How to declare a 1 Dimensional array?</h2>
<p>Now let us see how you can declare an array :</p>
<div class="highlight"><pre><span></span><code><data type> <array name>[<size of array>];
</code></pre></div>
<p>Here data type is the actually the type of data of the elements you would store in your array, array name is simply the name you give to your array (similar to the name of a variable, in order to access it later), which would follow the general rules of naming a variable which we have discussed previously. After the name of the array, we would have a set of square brackets in which we would mention the size of the array.</p>
<p>Now, let's again quickly take a look at our rollno array :</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">rollno</span><span class="o">[</span><span class="n">n</span><span class="o">]</span><span class="p">;</span>
</code></pre></div>
<p>Technically, before declaring this statement, you should first define the value of variable n, or else you'll face an error.</p>
<p>Here, n is just use to make you first comfortable with basic declaration of array. More sophisticatedly, you can simply use a definite value of n to define an array as follows:</p>
<div class="highlight"><pre><span></span><code>int rollno[60]; //Enough space to store roll no of 60 students
</code></pre></div>
<p>Next, we need to fill in the values for our array. Traditionally, following an approach as we've seen in the beginning would not only take a lot of time to define each variable corresponding to each student, but makes it a hectic process.</p>
<p>But first, we need to explore more about what exactly the index no or indices of the array is?</p>
<p>Well, typically, the computer memory is divided into small data cells (as shown above in the picture), occupying contiguous memory location. To access any element stored in an array we need something to reference it, like a variable name. In case we followed, the very first traditional approach defining 60 variables, they would have different names, which would be difficult to remember, but we could refer a single element. Using array, makes this more efficient by providing us with index no. or indices, as each subsequent element can be addressed / referred to by it's index value, which we would see in just a moment.</p>
<h2 id="what-would-be-the-size-of-the-array" style="text-align: center;">What would be the size of the array?</h2>
<p>An array would consist of many elements and hence the size of the array would be the sum of size of all elements in the array. For instance, in our example of rollno array for 60 student, which is of type int, the memory locations are occupied such that each cell has capacity to store an integer. We know, that in C , integer takes two bytes, so total memory occupied by rollno array would be 2 * 60 = 120.</p>
<p>Thus, you can easily calculate the amount of memory an array occupies by using the following formula:</p>
<blockquote>
<p>Size of array = Size of data type * Total no. of elements in the array</p>
</blockquote>
<p>Now let's see how to fill up the array:</p>
<h2 id="putting-values-in-array" style="text-align: center;">Putting values in array</h2>
<p>Filling up an array is very simple using for loop, as we would see here :</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">rollno</span><span class="o">[</span><span class="n">60</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Declaring</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="mi">60</span><span class="w"> </span><span class="n">elements</span><span class="w"> </span><span class="n">storage</span><span class="w"> </span><span class="nf">space</span>
<span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">60</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="n">rollno</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">);</span><span class="w"> </span><span class="o">//</span><span class="n">Here</span><span class="w"> </span><span class="k">value</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="n">increments</span><span class="p">,</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">allow</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">store</span><span class="w"> </span><span class="k">next</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">very</span><span class="w"> </span><span class="k">next</span><span class="w"> </span><span class="nf">space</span><span class="w"> </span><span class="n">till</span><span class="w"> </span><span class="k">value</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">60</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="n">terminates</span><span class="p">.</span>
<span class="err">}</span>
</code></pre></div>
<p>The working is very straight forward as explained in the comments.</p>
<p>One important thing to note here is that, as previously mentioned the index of array would start from 0 not 1. You should always remember this thing while handling data with arrays and your loops should also work in the same fashion.</p>
<p><strong>NOTE</strong> : Newbies mistake, assuming array to be start from 1. Remember, it would be started from 0.</p>
<h2 id="printing-out-elements-of-array" style="text-align: center;">Printing out elements of Array</h2>
<p>Now, after filling up the array, let's see how we can access one particular element. For instance, let's say I need to look for roll no. of 11th student, so I know it would be stored as 10th element in memory (not 11th) as indices start from 0. So, let's print the value of 11th student.</p>
<div class="highlight"><pre><span></span><code>...
printf("%d",rollno[10]);
...
</code></pre></div>
<p>In case you want to print all values stored in the array, we simply apply a loop to loop through the all the values in the array from indices 0 to indices 59 (a total of 60 values in this case).</p>
<div class="highlight"><pre><span></span><code><span class="p">...</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">59</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="n">rollno</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">);</span>
<span class="err">}</span>
<span class="p">...</span>
</code></pre></div>
<p>So, now that you've learned about 1 dimensional arrays, it's time to look for two dimensional arrays.</p>
<p>Have any doubts till now? Please feel free to mention in comments section below, or give me a shout on twitter \@ErSanyamKhurana.</p>[Ultimate Guide] What are loops in programming and how are they important?2014-03-27T21:40:00+05:302014-03-27T21:40:00+05:30sanyam_khuranatag:thegeekyway.com,2014-03-27:/loops-programming-important/<p>Welcome everyone to yet another tutorial on C Programming language. In this tutorial we'll be learning about a great concept of loops. Most of the programming languages would use exactly the same syntax for loops, so understand this carefully. Loops provide with great utility and power and saves from a …</p><p>Welcome everyone to yet another tutorial on C Programming language. In this tutorial we'll be learning about a great concept of loops. Most of the programming languages would use exactly the same syntax for loops, so understand this carefully. Loops provide with great utility and power and saves from a lot of overhead which may be there without them.</p>
<p>In this tutorial, we'll be discussing about various topics which revolves around concept of loops and understand them. They are :</p>
<ul>
<li><a href="#Basic-Definition-and-Types-of-Loops" title="ASP.Net Basics : Populate checkboxlist from database using C#">Basic Definition and Types of Loops</a></li>
<li><a href="#Basic-section-of-Loop" title="[Ultimate Guide] What are loops in programming and how are they important?">Basic Section of Loop</a></li>
<li><a href="#For-While-and-do-whlie-loop">For, While and Do-While Loops</a><ul>
<li><a href="#for-loop">For Loop</a></li>
<li><a href="#while-loop">While Loop</a></li>
<li><a href="#do-while-loop">Do While loop</a></li>
</ul>
</li>
<li><a href="#Difference-between-entry-controlled-and-exit-controlled-loop" title="ASP.Net Basics : Populate checkboxlist from database using C#">Difference between Entry Controlled and exit controlled loop</a></li>
</ul>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/Ultimage-Guide-on-Loops-in-programming1.png" title="Understand about Loops in Programming, for, while, do while"><img alt="Understand about Loops in Programming, for, while, do while " src="//thegeekyway.com/uploads/images/2014/03/Ultimage-Guide-on-Loops-in-programming1.png"></a></p>
</div>
<p #Basic-Definition-and-Types-of-Loops></p>
<hr>
<h2>Basic Definition and Types of Loops</h2>
<p>The basic concept behind <strong><em>"Loop"</em></strong> is to <u><em>execute certain set of statements as far as a given condition is true, or in many cases a counter variable gets out of a specific range defined, or we use may use some Logical operators to control the flow in our loop.</em> </u>There are basically three types of loops in the C language. They are :</p>
<ul>
<li><strong>for loop</strong></li>
<li><strong>while loop</strong></li>
<li><strong>do while loop</strong></li>
</ul>
<p>Now, let's look at the syntax of each of these and how they can be used, in detail. To understand all these in a better way, let's consider a common and simple example to print numbers from 1 to let's say 500.</p>
<p>The most basic approach to carry out this task is to keep printing each line having different numbers from 1 to 500 which would look something like this :</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">void</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"1 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"2 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"3 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"4 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"5 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"6 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"7 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>.<span class="w"> </span>
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"499 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"500 n"</span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>Now, let's try to improve upon this a little bit.<br>
We can take a counter variable and after each step just update it's value, then it would be something like this :</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">void</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">counter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">; /* Carry on this </span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">; 500 times */</span>
}
</code></pre></div>
<p>In this simple approach although we used a counter variable to avoid writing different numbers and updating the value of counter is left to the computer. But, it lead to an increase in the Lines Of Code (LOC) which is considered as an important parameter to compare any software.</p>
<p>So, it would be good, if we could use this counter variable approach but reduce the LOC. We can do it by using loops, now let's carry this out using different loops.</p>
<p #Basic-section-of-Loop></p>
<h2><strong>Basic sections of the loops</strong></h2>
<p>All loops have three basic sections which are :</p>
<ul>
<li><strong>Initialization</strong> : we need to initialize our counter variable in this statement.</li>
<li><strong>Test Condition</strong> : is the condition which needs to be true for the loop to keep running, as soon as it becomes false, the loop would stop executing, and the control would be directed to the next statement.</li>
<li><strong>Updation</strong> : In this statement the value of counter variable is updated (either incremented or decremented )</li>
</ul>
<p #For-While-and-do-whlie-loop></p>
<h2>For, While and Do While Loops :</h2>
<p>Now, we'll look here how we can print numbers from 1 to 500 using for, while and do-while loops by first understanding their syntax and then understanding the flow of control of these loops.</p>
<p #for-loop></p>
<h3><u><strong>Understanding and using for loop</strong></u></h3>
<p>* <strong>Syntax</strong> :*</p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="w"> </span><span class="ss">(</span><span class="w"> </span><span class="nv">initialization</span><span class="w"> </span><span class="c1">; test condition ; updation )</span>
{
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
}
</code></pre></div>
<p>Now let's carry out the given task to print numbers from 1 to 500.</p>
<p>The for loop for this would be something like this, here we have a counter variable named i which would be incremented from 1 to 500.</p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="ss">(</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1">; i<=500 ; i++ )</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>We can also define the counter variable inside the loop, and then it would have<a href="http://www.thegeekyway.com/what-is-scope-and-lifetime-of-a-variable-in-c-language/" title="What is scope and lifetime of a variable in C language">scope of the loop as explained in tutorial on Scope and lifetime of variables.</a></p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="ss">(</span><span class="nv">int</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1">; i<=500 ; i++ )</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p><strong>Understanding flow of for loop</strong></p>
<p>First time, when the control enters the loop, value of counter i is 1. The test condition would be checked, it would be true in this case, as 1 is less than or equal to 500. Hence control enters the loop and the statement to print value of i would be executed. We don't have more statements here, so control would go to updation, and update the value of i, it would be incremented.</p>
<p>Next the test condition would be again checked, here value of i is 2. So, test condition would be true in this case. Hence, control enters the loop and again print statement would be executed. This time value 2 would be printed. Once again, the control goes to updation statement, and value of i is incremented. It becomes 3 now, and again test condition would be checked.</p>
<p>This would continue till the value reaches 499, after updation, the value of i would be 500. The control again enters the loop, and value of 500 would be printed. Next updation takes place and value of i becomes 501, the test condition here becomes false, because, 501 is not less than or equal to 500. So, the loop would terminate, and the control would go to the next immediate next statement following the loop.</p>
<h3><p #while-loop></p></h3>
<h3><u><strong>Understanding and Using while loop</strong></u></h3>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="nv">initialization</span>
<span class="k">while</span><span class="ss">(</span><span class="nv">test_condition</span><span class="ss">)</span>
{
<span class="w"> </span><span class="o">//</span><span class="nv">all</span><span class="w"> </span><span class="nv">code</span>
<span class="w"> </span><span class="nv">updation</span><span class="w"> </span>
}
</code></pre></div>
<p>In while loop, initialization is done even before control enters the loop, as shown. Updation is done as the last statement of the loop and test condition is given as shown.</p>
<p>Now let us print the numbers from 1 to 500 using while loop.</p>
<div class="highlight"><pre><span></span><code><span class="nv">int</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="c1">;</span>
<span class="k">while</span><span class="ss">(</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">500</span><span class="ss">)</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">i</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">i</span><span class="o">++</span><span class="w"> </span><span class="c1">;</span>
}
</code></pre></div>
<p>The initialization is need is just once, whatever loop it is, so in while loop, we have done initialization, before entering the while loop.</p>
<p><strong>Understanding flow of while loop</strong></p>
<p>First of all the test condition is checked, which would be true, since 1 is less than or equal to 500, so control enters loop, and the print statement would be executed and updation statement also takes place. There is no other statement to be executed as end of the loop is reached. Before the control leaves the loop, the test condition would be checked, which would be again true as 2 is less than equal to 500.</p>
<p>This would continue until value is 500. Next, the value would be printed, and value is incremented by 1. So, condition is tested again, now, 501 would not be less than or equal to 500. Hence, the test condition becomes false and the control exits the loop executing the immediate next statement following the loop.</p>
<h3><p #do-while-loop></p></h3>
<h3><u><strong>Understanding and Using do - while loop</strong></u></h3>
<p><strong>Syntax:</strong></p>
<div class="highlight"><pre><span></span><code><span class="nv">initialization</span>
<span class="k">do</span><span class="w"> </span>{
<span class="w"> </span><span class="o">//</span><span class="nv">executing</span><span class="w"> </span><span class="nv">statements</span>
<span class="w"> </span><span class="nv">updation</span>
}<span class="w"> </span><span class="k">while</span><span class="ss">(</span><span class="nv">test</span><span class="w"> </span><span class="nv">condition</span><span class="ss">)</span><span class="c1">;</span>
</code></pre></div>
<p><u>Notice that in do while loop, we have a semi-colon at the end of while statement.</u></p>
<p>Let's now print the numbers from 1 to 500 using do while loop.</p>
<div class="highlight"><pre><span></span><code><span class="nv">int</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="c1">;</span>
<span class="k">do</span><span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">i</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">i</span><span class="o">++</span><span class="c1">;</span>
}<span class="w"> </span><span class="k">while</span><span class="ss">(</span><span class="nv">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">500</span><span class="ss">)</span><span class="c1">;</span>
</code></pre></div>
<p><strong>Understanding flow of do while loop</strong></p>
<p>The do while loop has an interesting feature, which is it would <strong>execute at least once</strong> whether the condition is true or not.</p>
<p>In the above case, the control enters the loop, and execute the statements. So, the value 1 would be printed. Next updation is done in the value of i, and the condition is checked against value 2, so 2 is less than or equal to 500 and hence the control once again start executing from the first statement. Next the test condition is checked against the value of 3 and so on.</p>
<p>This would continue till value of 500, next after printing value of 500, the updation takes place, and then the condition would become false. The control would get out of the loop and be ready to execute the immediate next statement.</p>
<p #Difference-between-entry-controlled-and-exit-controlled-loop></p>
<h2><u>Difference between Entry Controlled and Exit Controlled loop</u></h2>
<p><strong>Entry Controlled</strong> loops are those in which the test condition is checked first, and then if the condition is true, the control enters the loop and all the statements inside the loops are executed. For example, <u>for</u> and <u>while loop</u> are entry controlled loop.</p>
<p><strong>Exit Controlled</strong> loops are those in which the statements inside loops are executed first, and then the condition is tested. Even if the condition is false, the loop is executed at least once. For example, <u>do-while</u> loop is an exit controlled loop and it is executed at least once, whether the condition is true of false. If the condition is true, the loop is executed one more time, and if it's false, termination occurs, as we have discussed in detail above.</p>
<p>In the next tutorial, we'll learn about structures in the C language.</p>Storage Classes in C language2014-03-17T17:55:00+05:302014-03-17T17:55:00+05:30sanyam_khuranatag:thegeekyway.com,2014-03-17:/storage-classes-c-language-2/<p>In the previous tutorials, we have discussed about the conditional statements. In this tutorial we'll be learning about something known as Storage Classes in the C language. Every variable you define in the C language is generally associated with a Storage class which basically defines the Scope and lifetime of …</p><p>In the previous tutorials, we have discussed about the conditional statements. In this tutorial we'll be learning about something known as Storage Classes in the C language. Every variable you define in the C language is generally associated with a Storage class which basically defines the Scope and lifetime of that variable.</p>
<p>Before we get into detail about this, let's first understand<a href="http://www.TheGeekyWay.com/what-is-scope-and-lifetime-of-a-variable-in-c-language">what is exactly meant by scope and lifetime of variable.</a></p>
<p>Now, let us take a look at different types of storage classes available in C programming language:</p>
<ul>
<li>auto</li>
<li>register</li>
<li>static</li>
<li>extern</li>
</ul>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/storage_class1.png" title="Storage classes in C language and keywords used to define them. auto, extern, static and register."><img alt="Storage classes in C language and keywords used to define them. auto, extern, static and register." src="//thegeekyway.com/uploads/images/2014/03/storage_class1.png"></a></p>
</div>
<p>Now, we'll understand each one of this in detail :</p>
<p><u><strong>Auto (Automatic) :</strong></u></p>
<p>The default storage class of any variable is Automatic . It is created as soon as the declaration statement is encountered and is destroyed as soon as the program control leaves the block which contains your variable.</p>
<p style="font-family: comic sans ms,sans-serif;">*<u>Keyword used - auto</u>*</p>
<p>For example, here we define two variables, first, count, which is a local variable and second, counter inside a block (local variable using auto class).</p>
<div class="highlight"><pre><span></span><code>{
int count;
auto int counter;
}
</code></pre></div>
<p><u><strong>External Storage Class (Extern) :</strong></u></p>
<p>They are the Global Variables, i.e. declared outside the function block but are accessible inside the function block as well. They are accessible by all the function blocks present in the program and hence they have lifetime for entire program.</p>
<p>It is actually used to give a reference to the global variable which can be accessed in all your files. If we need to have a global variable, then in one file, we use that particular variable or function, and in the other file we define it by keyword extern so that when it is used in our other file, the reference is used from the first file.</p>
<p style="text-decoration: underline;"> <p style="font-family: comic sans ms,sans-serif;">Keyword used - extern</p></p>
<p>For example, let's have two files, first which would have our main function (main.c) and second one having a function named extern_storage (extern.c)</p>
<p>First file : main.c</p>
<div class="highlight"><pre><span></span><code>int counter = 1 ; // Global Variable
void main()
{
extern_storage();
counter++;
extern_storage();
counter--;
extern_storage();
}
</code></pre></div>
<p>Second file : extern.c</p>
<div class="highlight"><pre><span></span><code>void extern_storage(void);
extern int counter;
void extern_storage(void)
{
printf("Current value of extern counter is %d n", &counter);
}
</code></pre></div>
<p>When the above two files are executed, the function extern_storage picks up the current value from our main.c file. So, the following output will be produced.</p>
<p style="font-family: courier new,courier;">Current value of extern counter is 1</p>
<p style="font-family: courier new,courier;">Current value of extern counter is 2</p>
<p style="font-family: courier new,courier;">Current value of extern counter is 1</p>
<p><u><strong>Static Storage Class (static) :</strong></u></p>
<p>It has visibility of a local variable but lifetime of external variable. Thus, once defined, it would not be destroyed after the function finishes execution but would retain it's value in memory and can be used further in future function calls.</p>
<p style="text-decoration: underline;">* <p style="font-family: comic sans ms,sans-serif;">Keyword used - static</p>*</p>
<p>(Self-Explanatory through post on <a href="http://www.thegeekyway.com/what-is-scope-and-lifetime-of-a-variable-in-c-language/" title="What is scope and lifetime of a variable in C language">Scope and lifetime of variables</a>)</p>
<p><u><strong>Register Storage Class (register) :</strong></u></p>
<p>It is very similar in behavior to an automatic storage class variable. The difference between the two comes from the fact that auto storage class uses main memory for storage but register storage class variables might use CPU Registers.</p>
<p><u><em><strong>NOTE:</strong> register keyword doesn't guarantee that CPU Registers would be used every time, it depends on hardware and other restrictions that may be there.</em></u></p>
<p>The use of these registers facilitates faster accessing and helps in running programs faster. Thus variables that need quick access like variables for storing counter values should be declared by using register keyword.</p>
<p>For example :</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="w"> </span><span class="n">register</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p><em>We hope the concept of storage classes is now clear ! Have any doubt or something to say? Let us know through comments section below.</em></p>What is scope and lifetime of a variable in C language2014-03-10T00:28:00+05:302014-03-10T00:28:00+05:30sanyam_khuranatag:thegeekyway.com,2014-03-10:/what-is-scope-and-lifetime-of-a-variable-in-c-language/<p>Welcome to your next tutorial, here we would be discussing about the scope and lifetime of a variable, and how exactly they are similar yet different from each other. We'll try to understand all this by using an example to understand the concepts in a better way.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png" title="Scope and life time of a variable in C programming language"><img alt="Scope and life time of a variable in C programming language" src="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png"></a></p>
</div>
<p>So, let's understand …</p><p>Welcome to your next tutorial, here we would be discussing about the scope and lifetime of a variable, and how exactly they are similar yet different from each other. We'll try to understand all this by using an example to understand the concepts in a better way.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png" title="Scope and life time of a variable in C programming language"><img alt="Scope and life time of a variable in C programming language" src="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png"></a></p>
</div>
<p>So, let's understand each of these :</p>
<p><strong>Life Time</strong> - Life time of any variable is the time for which the particular variable outlives in memory during running of the program.</p>
<p><strong>Scope</strong> - The scope of any variable is actually a subset of life time. A variable may be in the memory but may not be accessible though. So, the area of our program where we can actually access our entity (variable in this case) is the scope of that variable.</p>
<p>The scope of any variable can be broadly categorized into three categories :</p>
<ul>
<li><strong>Global scope</strong> : When variable is defined outside all functions. It is then available to all the functions of the program and all the blocks program contains.</li>
<li><strong>Local scope</strong> : When variable is defined inside a function or a block, then it is locally accessible within the block and hence it is a local variable.</li>
<li><strong>Function scope</strong> : When variable is passed as formal arguments, it is said to have function scope.</li>
</ul>
<p>Let's understand this with help of an example :</p>
<p>Consider this snippet of program :</p>
<div class="highlight"><pre><span></span><code><span class="c1">#include<stdio.h></span>
<span class="nb nb-Type">int</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">declared</span>
<span class="nb nb-Type">void</span><span class="w"> </span><span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">declared</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Global variable is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">global</span><span class="p">);</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Local variable is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">local</span><span class="p">);</span>
<span class="w"> </span><span class="n">func1</span><span class="p">();</span>
<span class="p">}</span>
<span class="nb nb-Type">void</span><span class="w"> </span><span class="n">func1</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Global inside func1 is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">global</span><span class="p">);</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Would</span><span class="w"> </span><span class="nb">print</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">successfully</span><span class="o">.</span>
<span class="p">}</span>
</code></pre></div>
<p>Here we have a variable named global which is defined outside all the functions (generally at the top of the program) , it is defined even outside main, and hence would be available to every function defined in the program , here we consider only one function func1() .</p>
<p>The output of the above program would be :</p>
<p style="font-family: arial,helvetica,sans-serif;">Global variable is 100</p>
<p style="font-family: arial,helvetica,sans-serif;">Local variable is 10</p>
<p style="font-family: arial,helvetica,sans-serif;">Global inside func1 is 100</p>
<p>Let us consider the same program again but this time, let us change the definition of our function func1() as:</p>
<div class="highlight"><pre><span></span><code><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">func1</span><span class="p">()</span><span class="w"> </span>
<span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Global inside func1 is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">global</span><span class="p">);</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Would</span><span class="w"> </span><span class="nb">print</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">successfully</span><span class="o">.</span><span class="w"> </span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Local inside func1 is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="w"> </span><span class="n">local</span><span class="p">);</span><span class="w"> </span><span class="o">/*</span><span class="w"> </span><span class="n">It</span><span class="w"> </span><span class="n">would</span><span class="w"> </span><span class="n">produce</span><span class="w"> </span><span class="n">an</span><span class="w"> </span><span class="n">error</span><span class="p">,</span><span class="w"> </span><span class="n">because</span><span class="w"> </span><span class="n">local</span>
<span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">can</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">accessed</span><span class="w"> </span>
<span class="w"> </span><span class="n">only</span><span class="w"> </span><span class="n">inside</span><span class="w"> </span><span class="n">main</span><span class="w"> </span><span class="n">function</span><span class="o">.</span><span class="w"> </span><span class="o">*/</span>
<span class="p">}</span>
</code></pre></div>
<p>Clearly, as mentioned in the comment, the printing of global variable would be done, but that of local variable would produce an error, as the variable named local is actually local to the function main() and hence can be accessed only through that function not from any other function, but for global variable, it can be accessed by all functions/ blocks a program is using.</p>
<p>Here our variable named global has global scope and lifetime, hence it outlives in memory till the program execution is completed and can be accessed by any function / block locally too. But in case of our local variable, it may outlives in memory till program execution is completed but it can be accessed from only within the function or block it is defined in.</p>
<p>The basic problem here can be, we can have variable with same name defined as a global as well as local variable, and hence, it would be ambiguos as to which variable we want to actually refer to. We can resolve this issue in C++ (Advancement of C language) by the use of a special operator known as scope resolution operator. We'll look into this, when we'll cover tutorials for C++.</p>
<p>I hope all the concepts of scope and lifetime are now clear, but still if you have any doubt/ suggestion, please let us know through the comments section below.</p>Conditional Statements : Switch Case2014-02-01T21:02:00+05:302014-02-01T21:02:00+05:30sanyam_khuranatag:thegeekyway.com,2014-02-01:/conditional-statements-switch-case/<p>In the previous tutorial, we've discussed about <a href="http://www.thegeekyway.com/conditional-statements-if-else/" title="Understanding Conditional Statements: if else">if - else and nested if - else statements</a> and how we can use them to test certain conditions and execute a block of code depending on whether the condition becomes true or false. In this tutorial, we'll be covering another Conditional Statement used in …</p><p>In the previous tutorial, we've discussed about <a href="http://www.thegeekyway.com/conditional-statements-if-else/" title="Understanding Conditional Statements: if else">if - else and nested if - else statements</a> and how we can use them to test certain conditions and execute a block of code depending on whether the condition becomes true or false. In this tutorial, we'll be covering another Conditional Statement used in C known as Switch Case. The major difference between choosing to use if-else and switch is, when we have to compare a single variable to different set of values, then Switch is preferred, and if-else if preferred otherwise.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/SwitchCase1.png" title="Conditional Statements : Switch Case C language"><img alt="Conditional Statements : Switch Case C language" src="//thegeekyway.com/uploads/images/2014/02/SwitchCase1.png"></a></p>
</div>
<p>The basic semantics of a switch block statement is as follows :</p>
<div class="highlight"><pre><span></span><code><span class="n">switch</span><span class="p">(</span><span class="n">variable</span><span class="o">-</span><span class="n">name</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="mi">1</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="mi">2</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="mi">3</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="n">default</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Here variable-name corresponds to the variable you want to compare with a set of values and inside the curly braces we have defined cases such that if value of variable is 1 then the statement 1 and statement 2 would execute, for variable having value 2, statement 3 and 4 would execute and so on. If none of the cases are true, then statement n corresponding to default would execute.</p>
<p>Let us take an example and understand how it's done. Let us consider we need to just print what number it is if the number is less than or equal to 5 else print number is not 1, 2, 3, 4 or 5. We'll consider that user has input a number which is stored in variable named num. This can be done through if - else statements as we studied earlier like this :</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 1 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 2 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">3</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 3 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 4 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 5 n"</span><span class="c1">;</span>
<span class="k">else</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is not 1, 2, 3, 4 or 5 n"</span><span class="c1">;</span>
</code></pre></div>
<p>Now in the above case, since we need to compare just one variable with a set of values, this can be done easily through switch case as follows:</p>
<div class="highlight"><pre><span></span><code><span class="nv">switch</span><span class="w"> </span><span class="ss">(</span><span class="w"> </span><span class="nv">num</span><span class="w"> </span><span class="ss">)</span>
{
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 1 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 2 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 3 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 4 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 5 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">default</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is not 1, 2, 3, 4 or 5 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
}
</code></pre></div>
<p>Notice here that I have mentioned break statement after each case , this is to terminate the the case above it if it's executed. Let's see if we didn't include break what would happen, so consider this code snippet.</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Enter a number "</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">cin</span><span class="o">>></span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">switch</span><span class="w"> </span><span class="ss">(</span><span class="w"> </span><span class="nv">num</span><span class="w"> </span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 1 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 2 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 3 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 4 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 5 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">default</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is not 1, 2, 3, 4 or 5 n"</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>Now, if we run this and input 2 is given then output would be as shown below</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Switch_Case.png" title="Switch case in C language Output Screen"><img alt="Switch case in C language Output Screen" src="//thegeekyway.com/uploads/images/2014/02/Switch_Case.png"></a></p>
</div>
<p>See, we not only got Number is 2 statement printed, but got all statements printed after that case, in a sequential manner, this is because in a switch condition we need to have conditional flow of statement using break statement.</p>
<p>Now, if you include break after each case but doesn't do it with default case, it would run fine too, this is because if the default case is true, then after executing set of statements with that case, the control would automatically be out of the switch block, so technically we do not need break after default case.</p>
<p>Now let's see how to match a character type variable. Consider a character (char) type variable named character_variable , and if we need to use switch, it can be done in this way:</p>
<div class="highlight"><pre><span></span><code><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">character_variable</span><span class="w"> </span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="s1">'a'</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"Case a executed n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="s1">'b'</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"Case b executed n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="s1">'c'</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"Case c executed n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">default</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"This is default case n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Here, notice that we have enclosed the character value between single quotes ( ' ' ).</p>
<p>Like wise, you can design your switch case for various purpose as/when you need to compare a single variable value with a set of values.</p>
<p>In next tutorials we'll know about the Storage Classes in C. If you have any problem with anything till now, feel free to let us know through comments section below, we'll be glad to help you.</p>
<p>Keep Coding !</p>Understanding Conditional Statements: if else2014-01-12T23:08:00+05:302014-01-12T23:08:00+05:30sanyam_khuranatag:thegeekyway.com,2014-01-12:/conditional-statements-if-else/<p>In this tutorial, we'll uncover something much interesting which would simplify our work as a programer and help us to check if a given condition is true or not. We'll study about the Conditional statements like <strong>if - else</strong> , <strong>if-else if-else</strong>, <strong>nested if -else</strong> and <strong>switch</strong> (continued in next tutorial)</p>
<p><u><strong>Understanding …</strong></u></p><p>In this tutorial, we'll uncover something much interesting which would simplify our work as a programer and help us to check if a given condition is true or not. We'll study about the Conditional statements like <strong>if - else</strong> , <strong>if-else if-else</strong>, <strong>nested if -else</strong> and <strong>switch</strong> (continued in next tutorial)</p>
<p><u><strong>Understanding if else statements :</strong></u></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Conditional-statements-if-else1.png" title="Conditional statements in C : if else, nested if-else"><img alt="Conditional statements in C : if else, nested if-else" src="//thegeekyway.com/uploads/images/2014/01/Conditional-statements-if-else1.png"></a></p>
</div>
<p>To make the control of the program use conditional flow we need to use conditional statements, as the name suggests, this type of flow of control depends upon a particular condition ie either if it is true or it is false etc.</p>
<p>We'll first look at an if statement by considering this example :-</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above program would simply accept a value from user, and if the value entered is equal to 5 ie of course 5, then we would get a message printed on console "Number entered is 5" , if value entered is not 5, nothing would happen.</p>
<p>Note: whenever you write an if statement, you should have an opening and closing parenthesis as shown (in case you need to have multiple statements to be executed for an if statement) else, the program with just single statement in if block should not necessarily consist a block, so the above program would work if</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>is written as</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
</code></pre></div>
<p>So, we just instructed the computer to print a message, if a given condition happens to be true (which is entered value is 5 in this case).</p>
<p>Now, consider, we wanted program to print a message if the value entered is not 5, ie, if value entered is 5, it would print the message "Number entered is 5" otherwise, it would print "Number entered is not 5" . Let's see how this can be done with the help of if-else</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>Let's say if we need to print message for if a number is 2, it should print "Number entered is 2" , if it's 5, it should print "Number entered is 5" otherwise, it should print "Number is neither 2 nor 5. Let's see how it can be done with if-else statements</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 2"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above program could include any no of if - else if - else statements (which means no. of else if statements in between if and else can be increased )</p>
<p>To understand nested if-else let's see the following program:</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is even"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 2"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not 2"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not even"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above program first checks if a number is even or not, if it's even then it checks if number is 2 or not.</p>
<p>Note : To check number is even or not, we used modulus operator as discussed in <a href="http://www.thegeekyway.com/operators-c-language/" title="Operators in the C language">Tutorial on Operators</a> . Here we checked, if the number when divided by 2 leaves remainder 0 ie, it is entirely divisible by 2, then it's even else it's not even.</p>
<p>In the next tutorial, we'll know about another conditional statement, switch statement.</p>Operators in the C language2014-01-09T22:57:00+05:302014-01-09T22:57:00+05:30sanyam_khuranatag:thegeekyway.com,2014-01-09:/operators-c-language/<p>Welcome to your next tutorial in the C language, today, we’ll discuss about operators in the C language. We have already discussed about <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">Variables in C language</a>, and <a href="http://www.thegeekyway.com/learn-c-variables/" title="Learn C – More about variables">how we can store data in variables</a> input by user, next we may need to update this data , or make various …</p><p>Welcome to your next tutorial in the C language, today, we’ll discuss about operators in the C language. We have already discussed about <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">Variables in C language</a>, and <a href="http://www.thegeekyway.com/learn-c-variables/" title="Learn C – More about variables">how we can store data in variables</a> input by user, next we may need to update this data , or make various operations on the variables to produce desired results. This can be done through Operators in the C language.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Operators_in_C1.png" title="Operators in C"><img alt="Operators_in_C" src="//thegeekyway.com/uploads/images/2014/01/Operators_in_C1.png"></a></p>
</div>
<p>Operators can be classified broadly as follows :</p>
<ul>
<li>Arithmetic Operators</li>
<li>Logical Operators</li>
<li>Relational Operators</li>
<li>Assignment Operator</li>
<li>Bitwise</li>
<li>Conditional Operator</li>
<li>Miscellaneous Operators</li>
</ul>
<p>Now we’ll discuss about each category one by one, let us assume that we have declared two variables num1 and num2 and initialized them as shown below.</p>
<div class="highlight"><pre><span></span><code>int num1 = 3;
int num2 = 6;
</code></pre></div>
<p>Let's have another variable named res for storing result, so that we can analyze the value of result after applying each operator and understand their functioning.</p>
<p>But before this, we need to understand that there are basically two kinds of <em><strong>Operators depending on the number of operands</strong> they operate upon</em>. They are :-</p>
<p><strong>Unary Operators</strong> : which operate on only one operand.</p>
<p><strong>Binary Operators</strong> : which requires two operands to be operated upon.</p>
<p>Now, we'll carry on with understanding different types of operator.</p>
<p><strong>Arithmetic Operators</strong></p>
<p>They are binary operators used to do arithmetic operations like addition , subtraction , multiplication , division etc.</p>
<p><em><u>+ used for addition</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num1 + num2;
</code></pre></div>
<p>This would add the two values ie 5 and 6 and store the result to variable res which is 9 in this case.</p>
<p><em><u>- used for subtraction</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num2 - num1;
</code></pre></div>
<p>This would subtract num1 from num2 and then store the result in variable res which is 3 in this case.</p>
<p><em><u>* used for multiplication</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num1 * num2;
</code></pre></div>
<p>This would multiply num1 and num2 and store the result in variable res which is 18 in this case.</p>
<p><em><u> / used for division</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num2 / num1;
</code></pre></div>
<p>This would divide the two values ie num2 by num1 and then store the result in variable res which is 2 in this case.</p>
<p><em><u>% (modulus operator )</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num2 % num1;
</code></pre></div>
<p>This operator will divide num2 by num1 and then gives us the <em>REMAINDER</em> which will be stored in variable res and it's value would be 0 in this case , since, when 6 is divided by 3, it would give remainder 0.</p>
<p><strong>Logical Operators</strong>:</p>
<p>They are included in logical expressions and are of following types :</p>
<ul>
<li>Logical AND (&&)</li>
<li>Logical NOT ( ! )</li>
<li>Logical OR ( || )</li>
</ul>
<p>Note that in Logical AND and Logical OR, we use the respective symbol two times as mentioned. We'll study their complete usage in the coming tutorials specially in <em><u>Loops</u></em> and <em><u>Conditional Statements</u> .</em></p>
<p><strong>Relational Operator:</strong></p>
<p>As the name suggests, they are used to depict relations among variables. Mostly they return either a true (1) value or a false (0) value. They are of the following types :</p>
<ul>
<li>Less than or equal to ( \<= )</li>
<li>More than or equal to ( >= )</li>
<li>Not equal to ( != )</li>
</ul>
<p>Again, they would be better understood when we'll discuss about Conditional Statements and Loops, so we'll discuss them in the upcoming tutorials, just remember the types of operators for now.</p>
<p><strong>Assignment Operator:</strong></p>
<p>They are used to assign values to a variables, we are already using this operator since we studied about assigning a value to our variables, so consider the following :</p>
<div class="highlight"><pre><span></span><code>int num = 5;
</code></pre></div>
<p>Here ( = ) ie assignment operator is used to assign value 5 to variable num.</p>
<p><strong>Miscellaneous Operators</strong>:</p>
<p>There are other operators too, but we have covered the most important operators here.</p>
<p>Unary + and Unary - are some of the operators you would see often. As the name suggests they operate on only one operand. To understand what they do, consider the following code snippet :</p>
<div class="highlight"><pre><span></span><code>int num = 5;
num = (+num);
printf("Value of num after Unary + is %d n",&num);
num = -num;
printf("Value of num after Unary - is %d",&num);
</code></pre></div>
<p>Including all the necessary header files and other stuff required, it would produce the output as follows:</p>
<p>Value of num after Unary + is 5</p>
<p>Value of num after Unary - is -5</p>
<p>So, you understood it right Unary + doesn't changed the sign of the value, while unary - reversed the sign of integer value, so +5 become -5 , and it would be vice-versa if we have a negative value in variable.</p>
<p>In the next post, we'll study about Conditional statements and then about Loops in C and their usage, they would help us understand the concept of operators like logical and relational operator in much more detail. If you have any doubts/question, please feel free to flood them in the comments section, we'll help you !</p>Learn C - More about variables2013-12-16T11:51:00+05:302013-12-16T11:51:00+05:30sanyam_khuranatag:thegeekyway.com,2013-12-16:/learn-c-variables/<style type="text/css"><!--<br />
P { margin-bottom: 0.08in; }<br />
--></style>
<p>Welcome to your next tutorial on the Learn C language, here we'll have a deeper look at the use of variables in C language. In previous <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">tutorial about variables in C</a>, we defined a variable sam of integer type and then initialized it with …</p><style type="text/css"><!--<br />
P { margin-bottom: 0.08in; }<br />
--></style>
<p>Welcome to your next tutorial on the Learn C language, here we'll have a deeper look at the use of variables in C language. In previous <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">tutorial about variables in C</a>, we defined a variable sam of integer type and then initialized it with a value (10), this can be done in simpler manner in just one line. If you haven't read the previous tutorial, then please read it first, and you can read the <a href="http://www.thegeekyway.com/category/programming/c/">Learn C tutorial Series</a> .</p>
<p><strong>Declaration and Initialization together :</strong></p>
<p>So, we'll have a look at how to do this :</p>
<div class="highlight"><pre><span></span><code>int sam = 10;
</code></pre></div>
<p>In the above statement, I have declared my integer type variable named sam, and then initialised it with a value.</p>
<p>So, I had two options, one to do something like mentioned above, or have declaration in one line and initialization in other as :</p>
<div class="highlight"><pre><span></span><code>int sam;
sam = 10;
</code></pre></div>
<p>Now, we'll see how to take values from our user</p>
<p><strong>Accepting values from user :</strong></p>
<p>We can store values in our variables depending on choice of end user. This is done through a function know as scanf() and is defined under header file stdio.h (Standard Input/Output). We first define a variable named value.</p>
<div class="highlight"><pre><span></span><code>int value;
</code></pre></div>
<p>Now, we'll accept input form user as :</p>
<div class="highlight"><pre><span></span><code>scanf(“%d”, &value);
</code></pre></div>
<p>Note : scanf() function is somewhat like printf function defined before, but with the function name it's necessary to have ampersand (&) before variable name. This operator is used for reference, in simple terms, this helps in passing the address of memory location that is reserved for our new variable. We'll discuss about these things in detail in later tutorial, till now, just see it's done in this way.</p>
<p>Note: scanf() function should always contain ampersand before your variable name.</p>
<p>Now, we'll write a program here which will accept an input from user and then print the same on the screen.</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">value</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span>“<span class="nv">Enter</span><span class="w"> </span><span class="nv">value</span><span class="w"> </span>:<span class="w"> </span>“<span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span>“<span class="o">%</span><span class="nv">d</span>”<span class="w"> </span>,<span class="w"> </span><span class="o">&</span><span class="nv">value</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span>“<span class="nv">Value</span><span class="w"> </span><span class="nv">entered</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="o">%</span><span class="nv">d</span>”,<span class="nv">value</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above code snippet would produce the following output :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/C_TUT_2.1.png" title="Output for learning about variables in C"><img alt="Learn C variables Program Output" src="//thegeekyway.com/uploads/images/2013/12/C_TUT_2.1.png"></a></p>
</div>
<p>Note: Output compiled in Turbo C with DosBox for Windows, rather than gcc.</p>
<p>Note here, that whatever value end user enters, the same is printed on the screen.</p>
<p><strong>Accepting user Input and displaying output for variables of different data types :</strong></p>
<p>Now, we'll see how you can accept and return different types of variables :</p>
<p>For character type variable , you can do as :</p>
<div class="highlight"><pre><span></span><code>char ch;
scanf(“%c” , &ch);
printf(“%c”, ch);
</code></pre></div>
<p>For float type variable, you can do as :</p>
<div class="highlight"><pre><span></span><code><span class="nb nb-Type">float</span><span class="w"> </span><span class="n">fvar</span><span class="p">;</span>
<span class="n">scanf</span><span class="p">(</span><span class="err">“</span><span class="o">%</span><span class="n">f</span><span class="err">”</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">fvar</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="err">“</span><span class="o">%</span><span class="n">f</span><span class="err">”</span><span class="p">,</span><span class="w"> </span><span class="n">fvar</span><span class="p">);</span>
</code></pre></div>
<p>And so on, you can accept and return the values in variables of different kinds, what you have to remember is , you have to specify the placeholders in C in order to enter and retrieve values in variables, and if you want to accept input, use scanf function with ampersand, and for output use just variable name.</p>
<p>In the next tutorial we'll learn about Operators in C, what they are used for, and then with help of operators manipulate the values stored in variable, and implement all this through a small program to understand about what all we've discussed in these tutorials.</p>
<p>If you like the posts, don't forget to share this with your friends. For any doubts, let us know through comments, we'll answer you and clear your doubts ASAP.</p>Learning about Variables in C2013-12-09T22:30:00+05:302013-12-09T22:30:00+05:30sanyam_khuranatag:thegeekyway.com,2013-12-09:/variables-in-c/<p>Welcome to your second tutorial on the C language. In this tutorial, we'll be discussing about variables, their applications, their necessity both in general terms of programming and for C language as well. Variables in C are really very important. These are the fundamentals and you need to understand how …</p><p>Welcome to your second tutorial on the C language. In this tutorial, we'll be discussing about variables, their applications, their necessity both in general terms of programming and for C language as well. Variables in C are really very important. These are the fundamentals and you need to understand how variables in C are used and data in them can be manipulated.</p>
<p>Variables are where you store your data, you can assume them to be pretty much like containers (reserved memory locations) where you can store your things ( data ). In C or any programming language, it is essential that you define that variable is of what kind, ie it's data type.</p>
<p>The data types can be divided into two broad categories namely Fundamental Data types and Derived data types. Now we'll look in detail about both of these.</p>
<p><strong>Fundamental Data Types</strong></p>
<p>These are the data types which are built-in and not derived, that's why the word “Fundamental” has been used. There are five basic kind of Data types :-</p>
<ul>
<li>int – for integer data types</li>
<li>float – for floating point numbers (those with decimals)</li>
<li>char – for character type</li>
<li>double- same as integer type but include float also and is capable of holding more numbers (range is very large)</li>
<li>void – this stands for null value</li>
</ul>
<p><strong>Derived Data Types</strong></p>
<p>These are the data types derived from fundamental data types. Some of the derived data types are :</p>
<ul>
<li>Arrays</li>
<li>Pointers</li>
<li>Enumeration</li>
<li>Structures</li>
</ul>
<p>etc.</p>
<p><strong>Defining a Variable</strong></p>
<p>The following syntax should be used while defining variable in C :</p>
<div class="highlight"><pre><span></span><code><span class="n">data_type</span><span class="w"> </span><span class="n">variable_name</span><span class="p">;</span>
</code></pre></div>
<p>Here, data_type is the type of data which variable shall hold, and variable_name is any legal name you may give to the variable to access the information stored in it.</p>
<p>Now, we'll define a variable of type integer.</p>
<div class="highlight"><pre><span></span><code>int sam;
</code></pre></div>
<p>Here, we have defined a variable named sam which is capable of holding any integer type value.<br>
Now, we'll assign a value to this variable with assignment operator (=)</p>
<div class="highlight"><pre><span></span><code>sam = 10;
</code></pre></div>
<p>Now, our variable sam contains value 10.</p>
<p>Now, we'll see it in the entire program and output the value of variable.</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="nv">int</span><span class="w"> </span><span class="nv">sam</span><span class="c1">;</span>
<span class="nv">sam</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="c1">;</span>
<span class="nv">printf</span><span class="ss">(</span>“<span class="nv">Value</span><span class="w"> </span><span class="nv">of</span><span class="w"> </span><span class="nv">sam</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="o">%</span><span class="nv">d</span>”,<span class="w"> </span><span class="nv">sam</span><span class="ss">)</span><span class="c1">;</span>
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>For the above code, we'll get the following output :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/2.png" title="Output of variable in C tutorial code"><img alt="Output of variable in C tutorial code" src="//thegeekyway.com/uploads/images/2013/12/2.png"></a></p>
</div>
<p>(Note: Program are compiled in Turbo C with DosBox instead of gcc)</p>
<p>Note we've used %d , this just means that we want to print an integer value. In C, we need to tell the compiler what type of value is coming, so we need to have this %d for integer type value. Similar to %d, we use the following placeholders to instruct the compiler of upcoming data type :</p>
<p>\%d – integer (int)<br>
%c – character (char)<br>
%s – strings</p>
<p>\%f - float<br>
etc.</p>
<p>Observe, that in printf() function, we have used , and the variable name, and when this program is compiled and executed, we get the output , such that it prints Value of sam is 10 , the %d is now replaced by the value stored in our variable sam. This is because, %d was an instruction for the compiler so that it knows what type of value is coming in, so wherever you place this %d, and output an integer value, it would be replaced by value of that variable.</p>
<p>For example, see the following code snippet :</p>
<div class="highlight"><pre><span></span><code><span class="c1">#include<stdio.h></span>
<span class="nb nb-Type">int</span><span class="w"> </span><span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="nb nb-Type">int</span><span class="w"> </span><span class="n">sam</span><span class="p">;</span>
<span class="n">sam</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="err">“</span><span class="w"> </span><span class="n">Value</span><span class="w"> </span><span class="n">stored</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="o">%</span><span class="n">d</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">sam</span><span class="err">”</span><span class="w"> </span><span class="p">,</span><span class="w"> </span><span class="n">sam</span><span class="p">);</span>
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Here is the output for the above mentioned code snippet</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/1.png" title="Outputing with variables"><img alt="Output of variable in C tutorial code" src="//thegeekyway.com/uploads/images/2013/12/1.png"></a></p>
</div>
<p>(Note: Program are compiled in Turbo C with DosBox instead of gcc)</p>
<p>Note: Return type of our main function is of int type so we are returning value 0, this can be any value, but since we don't need to return anything, we generally return 0.</p>
<p>We'll carry this topic in the next tutorial as well, till then, you can try the codes from this tutorial and play with the code.</p>C# Tutorial : Compiling C# Programmes using Notepad2013-12-05T17:13:00+05:302013-12-05T17:13:00+05:30Deep_Singhtag:thegeekyway.com,2013-12-05:/csharp-tutorial-compiling-csharp-programmes-using-notepad/<h2>About C#</h2>
<p>C# is a multi-paradigm object oriented language developed by Mircrosoft in the year 2000. C# has its major implementations in dotNet framework, Mono, DotGNU. In this tutorial we will print a simple line "TheGeekyWay.com" using a notepad and C# command line compiler.</p>
<blockquote>
<p>In order to continue with …</p></blockquote><h2>About C#</h2>
<p>C# is a multi-paradigm object oriented language developed by Mircrosoft in the year 2000. C# has its major implementations in dotNet framework, Mono, DotGNU. In this tutorial we will print a simple line "TheGeekyWay.com" using a notepad and C# command line compiler.</p>
<blockquote>
<p>In order to continue with the C# tutorial you will need the latest version of .NET Framework SDK installed on your machine. You can download latest version of .NET Framework <a href="http://www.microsoft.com/en-in/download/details.aspx?id=19988">here</a>.</p>
</blockquote>
<p>First of all you need to create a folder where you can save your C# programme, you can also save the file on Desktop but its always a good habit to keep your files organized, so I am creating a folder named TheGeekyWay in the C: drive.</p>
<!--more-->
<blockquote>
<p><strong>Remember :</strong> C# is a case sensitive language. So everything should be same while writing the below code, any syntactical changes will bring down an error in compiling.</p>
</blockquote>
<p>So here is the following C# code (don't worry the code will be explained in detail in next tutorial, for now we just focus on how to compile and run a c# code in command prompt)</p>
<div class="highlight"><pre><span></span><code>public class TheGeekyWay
{
public static void Main()
{
System.Console.WriteLine("Welcome to TheGeekyWay.com");
System.Console.ReadLine();
}
}
</code></pre></div>
<p>Next step is to go to start button and type cmd and run as administrator. The reason using cmd is that many .NET tools can be used or scripted using DOS batch files.</p>
<p>Now in cmd type the following lines in cmd and reach to the directory of Framework. The reason for showing the directory is to know the recent version of C# compiler.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/CTUT1.1.png" title="C# Tutorial : To run C# programme using Notepad (Image 1)"><img alt="C# Tutorial : To run C# programme using Notepad Image 1" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.1.png"></a></p>
</div>
<p>now next step is to go in the directory of your latest .NET version, for me its v4.0.30319, so we use the following command line to go in the latest .NET folder directory.</p>
<div class="highlight"><pre><span></span><code>cd v4.0.30319
</code></pre></div>
<p>you can also visit the following code to view all the help related to C# compiler, remember to type the code in v4.0.30319 directory (or whatever your latest version of .NET compiler is).</p>
<div class="highlight"><pre><span></span><code>csc help
</code></pre></div>
<p>now the main part of the tutorial, the compilation code, here is the compilation code to compile your c# code.</p>
<div style="text-align:center">
<p><img alt="C#TUT1.2" class="alignnone size-medium wp-image-302" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.2.png"></p>
</div>
<p>confused ! don't worry, here is snapshot of the complete code using cmd.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/CTUT1.3.png" title="C# Tutorial : To run C# programme using Notepad (Image 2)"><img alt="C#" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.3.png" title="C# Tutorial : To run C# programme using Notepad"></a></p>
</div>
<p>here is the complete explanation of the C# code</p>
<blockquote>
<ul>
<li><strong>csc :</strong> csc stands for <strong>C</strong> <strong>S</strong>harp <strong>C</strong>ompiler.</li>
<li><strong>/t:exe :</strong> It means the extension of your output, generally "exe" is used.</li>
<li><strong>\<Your output destination path>\<Your file name> :</strong> Set the destination path and the file name where you want to save the file.</li>
<li><strong>\<Your Source Code path> :</strong> Set the path where your source code is saved.</li>
</ul>
</blockquote>
<p>and here is the output</p>
<div style="text-align:center">
<p><img alt="C#TUT1.4" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.4.png" title="C# Tutorial : To run C# programme using Notepad " "C# Tutorial : To run C# programme using Notepad (Image 3)"></p>
</div>
<p>There can be various problems that you can arrive into, since C# is a case sensitive language, so did you typed the code exactly same as mentioned in the box, sometimes there can be permissions problem, sometimes there can be a problem in the compiler line.</p>
<p>That's it for now folks ! I hope you enjoyed the article on compiling C# Programmes using Notepad and command line. If any problem arises in the compilation or any sort of error comes during compilation, you can post it as a comment below and TheGeekyWay team will try to solve it ASAP.</p>
<h2>You can click the following social icons and share this article with your friends.</h2>
<hr>Getting Started With C language2013-11-19T13:26:00+05:302013-11-19T13:26:00+05:30sanyam_khuranatag:thegeekyway.com,2013-11-19:/getting-started-c-language/<p><strong>Introduction to C language</strong></p>
<p>C language is a middle level language developed by Dennis Ritchie in 1969 and the most secure OS "UNIX" is also coded in C language. It has just turned the world of Coding giving new level to programming, and introducing us to what is called "Structured …</p><p><strong>Introduction to C language</strong></p>
<p>C language is a middle level language developed by Dennis Ritchie in 1969 and the most secure OS "UNIX" is also coded in C language. It has just turned the world of Coding giving new level to programming, and introducing us to what is called "Structured Programming".</p>
<p>It is very important to note here that C is a Case Sensitive language, what this means is, TgW , tGw, TGw are all different. Don't worry if it's sound confusing, you'll be able to understand it better once we begin to learn about Variables.</p>
<p>Wait! before we start, the question arises :-</p>
<p><strong>Why learn C language?</strong></p>
<p>Well, learning C language would improve your logical skills, and many programming languages are directly or indirectly derived from C. This means, if you learn C language, you can learn other programming languages like C++, Java, JavaScript, Python, PHP, Perl and many others, much faster and it would be simple for you.</p>
<!--more-->
<p><strong>Pre Requisites for this and the coming Tutorials :-</strong></p>
<p>To compile the C language programs, you need something called compiler. It doesn't matter if you're using Linux, Windows, or Mac, you'll surely get a C compiler. If you're Windows user, get the best compiler from the start ie gcc compiler. I wouldn't recommend any other like Turbo C etc. they are not good when compared to gcc. Download gcc compiler from here :-<br>
http://gcc.gnu.org/<br>
If you're on Linux you'll probably already have gcc compiler installed. To check if you have gcc compiler already installed please type the following command in the terminal</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>g++<span class="w"> </span>-v
</code></pre></div>
<p>If you've gcc installed, you'll get something like this, as in my terminal window.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/11/C_Tut1.1.png" title="checking for gcc compiler in linux"><img alt="checking for gcc compiler in linux" src="//thegeekyway.com/uploads/images/2013/11/C_Tut1.1.png"></a></p>
</div>
<p>Much of theory, isn't it? Ok, let's get started with some code.</p>
<p><strong>First C Program</strong></p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="nv">printf</span><span class="ss">(</span><span class="s2">"Hello World"</span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>Just compile it in your gcc compiler and you'll get Hello World printed in the output console.</p>
<p>If you need help in compilation and execution, wait for one more tutorial.</p>
<p>Now, we'll just break down each and every part of the program and analyse it.<br>
``</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
</code></pre></div>
<p>``In this command, we ask the compiler to include a file (known as header file) which contains some pre defined fuctions which are needed to run the program.<br>
We, request to add file stdio which is Standard Input/Output console file, as we need to display the print on the output console.<br>
"#" (hash/pound) symbol here represents the pre-processor directive ie command started with a # sign goes to the pre-processor, means these statements are carried out first, before the program goes for compilation.</p>
<div class="highlight"><pre><span></span><code>main()
</code></pre></div>
<p>This is a function in programming language, they are just meant to divide the program into modules. The main function of any program is of uttermost importance, as the program execution starts from main function.</p>
<div class="highlight"><pre><span></span><code>printf()
</code></pre></div>
<p>This is also a function, it is the standard output function defined under stdio header file. Whatever we give in the paranthesis between the quotes is printed on the screen.</p>
<p>Note, that we have used int before main function , this means that this function would return a value of int type, and that's the reason we have given return 0 at the end.</p>
<p>Also note, that each statement in C would terminate with a semicolon. If you forget to put a semicolon, compiler would report an error.</p>
<p>You can now continue to <a href="http://www.thegeekyway.com/variables-in-c/" title="Learn about variables in C">Learn about variables in C</a></p>