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

<channel>
	<title>Geek Files &#187; C Tutorials</title>
	<atom:link href="http://www.sunilb.com/category/c-tutorials/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sunilb.com</link>
	<description>Question Everything - that&#039;s the only way to learn</description>
	<lastBuildDate>Mon, 30 Jan 2012 04:59:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>C Tutorial &#8211; Operators in C</title>
		<link>http://www.sunilb.com/c-programming/c-tutorial-operators-in-c</link>
		<comments>http://www.sunilb.com/c-programming/c-tutorial-operators-in-c#comments</comments>
		<pubDate>Sun, 25 May 2008 08:43:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C Programming]]></category>
		<category><![CDATA[C Tutorials]]></category>
		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/c-tutorial-operators-in-c</guid>
		<description><![CDATA[An operator is a symbol that operates on a certain data type and produces the output as the result of the operation. In C, you can combine various operators of similar or different categories and perform an operation. In this case the C compiler tries to solve the expression as per the rules of precedence. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fc-tutorial-operators-in-c"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fc-tutorial-operators-in-c&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>An operator is a symbol that operates on a certain data type and produces the output as the result of the operation. In C, you can combine various operators of similar or different categories and perform an operation. In this case the C compiler tries to solve the expression as per the rules of precedence.</p>
<p><span id="more-89"></span></p>
<h3>Category of operators</h3>
<p><b>Unary Operators</b><br />
A unary operator is an operator, which operates on one operand.</p>
<p><b>Binary</b><br />
A binary operator is an operator, which operates on two operands</p>
<p><b>Ternary</b><br />
A ternary operator is an operator, which operates on three operands.</p>
<h3>Following are the different types of Operators</h3>
<p></b>Arithmetic Operator</b><br />
The arithmetic operator is a binary operator, which requires two operands to perform its operation of arithmetic. Following are the arithmetic operators that are available</p>
<table border="1">
<tr>
<td>Operator</td>
<td>Description</td>
</tr>
<tr>
<td>+</td>
<td>Addition</td>
</tr>
<tr>
<td>-</td>
<td>Subtraction</td>
</tr>
<tr>
<td>/</td>
<td>Division</td>
</tr>
<tr>
<td>*</td>
<td>Multiplication</td>
</tr>
<tr>
<td>%</td>
<td>Modulo or remainder</td>
</tr>
</table>
<p>At the time of using the â€˜/â€™ division operator on two integers, the result will also be an integer. E.g. 100/8 will result in 12 and not 12.50.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Unary Operators</h3>
<p>A unary operator is an operator, which operates on one operand, i.e. it operates on itself.</p>
<p>Following are the unary operators available under C</p>
<p>!&nbsp;&nbsp;&amp;&nbsp;&nbsp;*&nbsp;&nbsp;-&nbsp;&nbsp;+&nbsp;&nbsp;~</p>
<p><b>Increment and Decrement Operators</b></p>
<p>These operators also fall under the broad category or unary operators but are quite distinct than unary minus.</p>
<p>The increment and decrement operators are very useful in C language. They are extensively used in for and while loops. The syntax of these operators is given below.</p>
<p>++<variable-name><br />
<variable-name>++<br />
&#8211;<variable-name><br />
<variable-name>&#8211;</p>
<p>The ++ operator increments the value of the variable by one, whereas the â€“ operator decrements the value of the variable by one.</p>
<p>These operators can be used in either the postfix or prefix notation as follows:</p>
<p>Postfix: a++ or a&#8211;<br />
Prefix: ++a or â€“a</p>
<p><b>Postfix notation</b></p>
<p>In the postfix notation, the value of the operand is used first and then the operation of increment or decrement takes place, e.g. consider the statements below:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">int</span> a<span style="color: #339933;">,</span>b<span style="color: #339933;">;</span>
	a <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
	b <span style="color: #339933;">=</span> a<span style="color: #339933;">++;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>â€œ<span style="color: #339933;">%</span>d\nâ€<span style="color: #339933;">,</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>â€œ<span style="color: #339933;">%</span>d\nâ€<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Program Output</b><br />
10<br />
11</p>
<p><b>Prefix notation</b></p>
<p>In the prefix notation, the operation of increment or decrement takes place first after which the new value of the variable is used.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">int</span> a<span style="color: #339933;">,</span>b<span style="color: #339933;">;</span>
	a <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
	b <span style="color: #339933;">=</span> <span style="color: #339933;">++</span>a<span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>â€œ<span style="color: #339933;">%</span>d\nâ€<span style="color: #339933;">,</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>â€œ<span style="color: #339933;">%</span>d\nâ€<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Program Output</b><br />
11<br />
11</p>
<h3>Relational Operators</h3>
<p>Relational operators compare between two operands and return in terms of true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. Relational operators are used in conjunction with logical operators and conditional &#038; looping statements.</p>
<p>Following are the various relational operators</p>
<table border="1">
<tr>
<td>&lt;</td>
<td>Less than</td>
</tr>
<tr>
<td>&lt;=</td>
<td>Less than or equal to</td>
</tr>
<tr>
<td>&gt;</td>
<td>Greater than</td>
</tr>
<tr>
<td>&gt;=</td>
<td>Greater than or equal to</td>
</tr>
</table>
<p>Closely related to the relational operators is the equality operator as follows:</p>
<table border="1">
</tr>
<td>==</td>
<td>Equal to</td>
</tr>
</tr>
<td>!=</td>
<td>Not equal to</td>
</tr>
</table>
<p><b>Example:</b></p>
<p>Suppose we have three integer variables a, b and c with values 1,2 and 3 respectively.</p>
<table border="1">
<tr>
<td><b>Expression</b></td>
<td><b>Returns</b></td>
<td><b>Meaning</b></td>
</tr>
<tr>
<td>A&lt;b</td>
<td>1</td>
<td>True</td>
</tr>
<tr>
<td>(a+b)&gt;=c</td>
<td>1</td>
<td>True</td>
</tr>
<tr>
<td>(b+c)&gt;(a+5)</td>
<td>0</td>
<td>False</td>
</tr>
<tr>
<td>(c!=3)</td>
<td>0</td>
<td>False</td>
</tr>
<tr>
<td>B==2</td>
<td>1</td>
<td>True</td>
</tr>
</table>
<h3>Logical Operators</h3>
<p>A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available in the C language.</p>
<table border="1">
<tr>
<td><b>Operator</b></td>
<td><b>Meaning</b></td>
</tr>
<tr>
<td>&#038;&#038;</td>
<td>Logical AND</td>
</tr>
<tr>
<td>||</td>
<td>Logical OR</td>
</tr>
<tr>
<td>!</td>
<td>Logical NOT</td>
</tr>
</table>
<p>Truth table for Logical AND</p>
<table border="1">
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<p>Truth table for Logical OR</p>
<table border="1">
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<p><b>Example</b></p>
<p>Let us take three variables a, b, and c and assume a = 5,b=10 and c=7</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>b <span style="color: #339933;">&amp;&amp;</span> a<span style="color: #339933;">+</span>b<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>c</pre></div></div>

<p>Here a&gt;b will return false hence 0 and a+b&gt;c will return true hence 1. Therefore, looking at the truth table 0 AND 1 will be 0. From this we can understand, that this expression will evaluate to false.</p>
<p><b>Let us look at the same example using Logical OR (||)</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>b <span style="color: #339933;">||</span> a<span style="color: #339933;">+</span>b<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>c</pre></div></div>

<p>Here a&gt;b will return false hence 0 and a+b&gt;c will return true hence 1. Therefore, looking at the truth table 0 OR 1 will be 1. From this we can understand, that this expression will evaluate to true.</p>
<h3>The Logical NOT operator (!)</h3>
<p>The ! (Logical NOT) operator is a unary operator. This operator inverses a logical result.</p>
<p>E.g. if a = 5 and b = 7, then</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">==</span> b<span style="color: #009900;">&#41;</span></pre></div></div>

<p>will evaluate to be true. This expression works as follows, a==b evaluates to be false, now because of the ! (NOT) operator, the false is inverted to be true.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Assignment Operator</h3>
<p>An assignment operator (=) is used to assign a constant or a value of one variable to another.</p>
<p><b>Example:</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a <span style="color: #339933;">=</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">;</span>
b <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
interestrate <span style="color: #339933;">=</span> <span style="color:#800080;">10.5</span>
result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">/</span>b<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Important Note:</b></p>
<p>Remember that there is a remarkable difference between the equality operator (==) and the assignment operator (=). The equality operator is used to compare the two operands for equality (same value), whereas the assignment operator is used for the purposes of assignment.</p>
<p><b>Multiple assignments:</b></p>
<p>You can use the assignment for multiple assignments as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a <span style="color: #339933;">=</span> b<span style="color: #339933;">=</span> c <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span></pre></div></div>

<p>At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.</p>
<p><b>Arithmetic Assignment Operators</b></p>
<p>Arithmetic Assignment operators are a combination of arithmetic and the assignment operator. With this operator, the arithmetic operation happens first and then the result of the operation is assigned.</p>
<table border="1">
<b>Operators</b></td>
<td><b>Example</b></td>
<td><b>Expands as</b><br />
<tr>
<td></td>
</tr>
<p>+=</td>
<td>Sum+=3</td>
<td>Sum = sum + 3<br />
<tr>
<td></td>
</tr>
<p>-=</td>
<td>Count-=4</td>
<td>Count -= 4<br />
<tr>
<td></td>
</tr>
<p>*=</td>
<td>Factorial*=num</td>
<td>Factorial = factorial * num<br />
<tr>
<td></td>
</tr>
<p>/=</td>
<td>Num/=10</td>
<td>Num = num /10<br />
<tr>
<td></td>
</tr>
<p>%=</td>
<td>A%=3</td>
<td>A = a % 3<br />
<tr>
<td></td>
</tr>
</table>
<p><b>Conditional Operator</b></p>
<p>A conditional operator checks for an expression, which returns either a true or a false value. If the condition evaluated is true, it returns the value of the true section of the operator, otherwise it returns the value of the false section of the operator.</p>
<p>Its general structure is as follows:</p>
<blockquote><p>
	Expression1 ? expression 2 (True Section): expression3 (False Section)
</p></blockquote>
<p><b>Example:</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span>b<span style="color: #339933;">=</span><span style="color: #0000dd;">5</span><span style="color: #339933;">,</span>c<span style="color: #339933;">;</span>
&nbsp;
c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>b<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> a<span style="color: #339933;">+</span>b <span style="color: #339933;">:</span> b<span style="color: #339933;">-</span>a<span style="color: #339933;">;</span></pre></div></div>

<p>The variable c will have the value 2, because when the expression (a&gt;b) is checked, it is evaluated as false. Now because the evaluation is false, the expression b-a is executed and the result is returned to c using the assignment operator.</p>
<h3>Precedence of Operators</h3>
<table border="1">
<tr>
<td><b>Operator</b></td>
<td><b>Associavity</b></td>
</tr>
<tr>
<td>Unary</td>
<td>Right to Left</td>
</tr>
<tr>
<td>Arithmetic Operators</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Relational Operator</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Equality Operator</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Logical Operator</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Conditional Operator</td>
<td>Right to Left</td>
</tr>
<tr>
<td>Assignment Operator</td>
<td>Right to Left</td>
</tr>
<tr>
<td>Comma operator</td>
<td>Right to Left</td>
</tr>
</table>
<h3>Type Conversion</h3>
<p>When an operatorâ€™s operands are of different data types, C automatically converts them to the same data type. This can affect the results of mathematical expressions. This is called type conversion. In C type conversion can be done by two ways, viz.</p>
<p><b>Automatic Conversion (Arithmetic Promotion)</b><br />
When a value is converted to a higher type, it is said to be promoted. Letâ€™s look at the specific rules that govern the evaluation of mathematical expressions.</p>
<p><b>Rule 1:</b> chars, shorts, unsigned shorts are automatically promoted to int.<br />
<b>Rule 2:</b> When an operator works with two values of different data types, the lower data type is promoted to a higher data type.</p>
<p><b>Type Casting</b><br />
Type casting means to convert a higher data type to a lower data type. For the purpose of type casting we require to use the type case operator, which lets you manually promote or demote a value. It is a unary operator which appears as the data type name followed by the operand inside a set of parentheses. E.g.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	val <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>number<span style="color: #339933;">;</span></pre></div></div>

<p>This was about Operators in C, in the next <b>C Tutorial</b> you will learn about input and output in C.</p>
<p>Subscribe now to receive updates when a new <b>C tutorial</b> is released.</p>
<p><!--subscribe2--></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/c-tutorial-operators-in-c/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>C Tutorial &#8211; Constants, Variables and Data Type Modifiers</title>
		<link>http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers</link>
		<comments>http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers#comments</comments>
		<pubDate>Sun, 25 May 2008 07:46:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C Programming]]></category>
		<category><![CDATA[C Tutorials]]></category>
		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers</guid>
		<description><![CDATA[Constant A constant can be defined as â€œa quantity that does not change during the execution of a programâ€. #include &#60;stdio.h&#62; &#160; void main&#40;void&#41; &#123; int a = 10; printf&#40;â€œ%dâ€,a&#41;; &#125; Program Output 10 Here we are declaring a variable a with its initial value 10. 10 here is an integer constant and every time [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fc-tutorial-constants-variables-and-data-type-modifiers"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fc-tutorial-constants-variables-and-data-type-modifiers&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h3>Constant</h3>
<p>A constant can be defined as â€œa quantity that does not change during the execution of a programâ€.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> a <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>â€œ<span style="color: #339933;">%</span>dâ€<span style="color: #339933;">,</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Program Output<br />
10</p>
<blockquote><p>
Here we are declaring a variable a with its initial value 10. 10 here is an integer constant and every time you try to execute this program, the output will always be 10.
</p></blockquote>
<p><span id="more-88"></span></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Types of Constants</h3>
<p><b>Integer Constant</b><br />
An integer constant must have atleast one digit and should not have decimal point. It could either be positive or negative.</p>
<p><b>Real Constant</b><br />
A real constant must have atleast one digit and must have a decimal point.</p>
<p><b>Character Constant</b><br />
A character constant should be only one character and must be enclosed in single quotes e.g. â€˜Aâ€™, â€˜bâ€™, etc.</p>
<h3>Variables</h3>
<p>A variable is a name give to the space in the memory for holding data such as integers, characters, floating point numbers, strings, etc. Using a variable name is more convenient than using the memory locations.</p>
<p>There are certain rules for naming variables for which you need to refer to the rules for identifiers.</p>
<p><b>Declaration of variables</b><br />
To declare a variable means to create a memory space for the variable depending on the data type used and associate the memory location with the variable name. In C all the variables should be declared before it can be used. Also you cannot re-declare a variable once it has been already declared.</p>
<p>The general pattern for declaring a variable is as follows:</p>
<p><data-type> <variable-name-1>, <variable-name-2>, <variable-name-n>;</p>
<p>e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">int</span> age<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> age<span style="color: #339933;">,</span> count<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span>j<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> amount<span style="color: #339933;">,</span> rateofinterest<span style="color: #339933;">,</span> totalbalance<span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> ch<span style="color: #339933;">;</span></pre></div></div>

<p><b>Constant Variables</b></p>
<p>A constant variable is also called a named constant. A named constant is really a variable whose content is read-only and cannot be changed while the program is running. Here is a declaration of a named constant.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> pi <span style="color: #339933;">=</span> <span style="color:#800080;">3.14</span><span style="color: #339933;">;</span></pre></div></div>

<p>It looks just like a regular variable declaration except that the word const appears before the data type name. const is a qualifier that tells the compiler to make the variable read only. Its value will remain constant throughout the programs execution.</p>
<p>An initialization value must be given when declaring a variable with the const qualifier, or an error will result when the program is compiled. A compiler error will also result if there are any statements in the program that attempt to change the contents of a named constant.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Data type modifiers</h3>
<p>We have so far learned about the fundamental data types, why they are used, their default sizes and values. Now let us understand the need for data type modifiers and the way they can be used.</p>
<p>Let us consider an example of a program, which will accept an age from the user to do some processing. Because the age is represented in numbers, so we will have to use the integer data type int. We all know that even under exceptional case an age of a person cannot exceed more than 150. Now, that we are using an integer data type it occupies 2 Bytes of memory, which would not be required to represent the value 150. Instead the value 150 could easily be saved in an integer of 1 Byte in size, but the default size of an integer is 2 Bytes. So we have a problem here.</p>
<p>Well, not that we canâ€™t solve. To override the default nature of a data type, C has provided us with data type modifiers as follows:</p>
<p><b>signed</b><br />
By default all data types are declared as signed. Signed means that the data type is capable of storing negative values.</p>
<p><b>unsigned</b><br />
To modify a data typeâ€™s behavior so that it can only store positive values, we require to use the data type unsigned.</p>
<p>For example, if we were to declare a variable age, we know that an age cannot be represented by negative values and hence, we can modify the default behavior of the int data type as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> age<span style="color: #339933;">;</span></pre></div></div>

<p>This declaration allows the age variable to store only positive values. An immediate effect is that the range changes from (-32768 to 32767) to  (0 to 65536)</p>
<p><b>long</b><br />
Many times in our programs we would want to store values beyond the storage capacity of the basic data types. In such cases we use the data type modifier long. This doubles the storage capacity of the data type being used. E.g. long int annualsalary will make the storage capacity of variable annualsalary to 4 bytes.</p>
<p>The exception to this is long double, which modifies the size of the double data type to 10 bytes. Please note that in some compilers this has no effect.</p>
<p><b>short</b><br />
If long data type modifier doubles the size, short on the other hand reduces the size of the data type to half. Please refer to the example of age variable to explain the concept of data type modifiers. The same will be achieved by providing the declaration of age as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">short</span> <span style="color: #993333;">int</span> age<span style="color: #339933;">;</span></pre></div></div>

<p>This declaration above, will provide the variable age with only 1 byte and its data range will be from -128 to 127.</p>
<p>This was about constants, variables and data type modifiers. In the subsequent C Tutorials you will learn about C Operators.</p>
<p>Subscribe now to receive updates when a new <b>C tutorial</b> is released.</p>
<p><!--subscribe2--></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>C Tutorial &#8211; Fundamental Data Types</title>
		<link>http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types</link>
		<comments>http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types#comments</comments>
		<pubDate>Thu, 22 May 2008 04:08:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C Programming]]></category>
		<category><![CDATA[C Tutorials]]></category>
		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types</guid>
		<description><![CDATA[In this C Tutorial we will learn what Fundamental Data Types are and how to use them. We develop programs to accept an input from the user and then run the input through a series of processes and produce the output on screen or on the printer, etc. INPUT > PROCESS > OUTPUT From the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fc-tutorial-fundamental-data-types"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fc-tutorial-fundamental-data-types&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this <b>C Tutorial</b> we will learn what Fundamental Data Types are and how to use them.</p>
<p>We develop programs to accept an input from the user and then run the input through a series of processes and produce the output on screen or on the printer, etc.</p>
<p><span id="more-87"></span></p>
<p>INPUT > PROCESS > OUTPUT</p>
<p>From the userâ€™s point of view, the input supplied is data, which requires to be processed. Depending on the input provided by the user i.e. a number, fractional number, a name or an address, etc., the program must be designed to hold all these inputs. C provides the programmer with four basic or fundamental types of container to hold data, which are as follows:</p>
<table border="1">
<tr>
<td>Data Type</td>
<td>Bytes</td>
<td>Default Range</td>
</tr>
<tr>
<td>signed char</td>
<td>1</td>
<td>-128 to 127</td>
</tr>
<tr>
<td>Unsigned char</td>
<td>1</td>
<td>0 to 255</td>
</tr>
<tr>
<td>short signed int</td>
<td>2</td>
<td>-32768 to 32767</td>
</tr>
<tr>
<td>short unsigned int</td>
<td>2</td>
<td>0 to 65535</td>
</tr>
<tr>
<td>long signed int</td>
<td>4</td>
<td>-2147483648 to 2147483647</td>
</tr>
<tr>
<td>long unsigned int</td>
<td>4</td>
<td>0 to 4294967295</td>
</tr>
<tr>
<td>Float</td>
<td>4</td>
<td>-3.4e38 to +3.4e38</td>
</tr>
<tr>
<td>Double</td>
<td>8</td>
<td>-1.7e308 to +1.7e308</td>
</tr>
<tr>
<td>long double</td>
<td>10</td>
<td>-1.7e4932 to +1.7e4932</td>
</tr>
</table>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p><b>Following is the explanation of data types:</b></p>
<p><b>Character data type (char)</b></p>
<p>The character data type is only 1 Byte in size. It is used for only storing characters.</p>
<p><b>Integer Data type (int)</b></p>
<p>The integer data type is used to store whole numbers only. Fractional numbers are not allowed in an int data type.</p>
<p><b>Data type to store fractional numbers</b></p>
<p><b>float</b></p>
<p>The float data type is used to store floating point numbers or fractional numbers. This data type is 4 Bytes in size. Floating point numbers can also be expressed in the scientific notation e.g. 1.7e4 represents the number 1.7 x 10<sup>4</sup>.</p>
<p><b>double</b></p>
<p>The double data type is also used to store floating point numbers or a fractional numbers. This data type is 8 Bytes in size. This data type can hold a large value.</p>
<p>This was about fundamental <b>data types in c</b>. In the subsequent C Tutorials we will learn how to use these data types by creating variables.</p>
<p>Subscribe now to receive updates when a new <b>c tutorial</b> is released.</p>
<p><!--subscribe2--></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to C Programming</title>
		<link>http://www.sunilb.com/c-programming/introduction-to-c-programming</link>
		<comments>http://www.sunilb.com/c-programming/introduction-to-c-programming#comments</comments>
		<pubDate>Sun, 18 May 2008 14:53:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C Programming]]></category>
		<category><![CDATA[C Tutorials]]></category>
		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/introduction-to-c-programming</guid>
		<description><![CDATA[As a programming language, C is rather like Pascal or Fortran. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fintroduction-to-c-programming"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fc-programming%2Fintroduction-to-c-programming&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>As a programming language, C is rather like Pascal or Fortran. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays or structures.</p>
<p>Of the three languages, C allows the most precise control of input and output. C is also rather more terse than Fortran or Pascal. This can result in short efficient programs, where the programmer has made wise use of C&#8217;s range of powerful operators. It also allows the programmer to produce programs which are impossible to understand.</p>
<p>Programmers who are familiar with the use of pointers (or indirect addressing, to use the correct term) will welcome the ease of use compared with some other languages. Undisciplined use of pointers can lead to errors which are very hard to trace. This course only deals with the simplest applications of pointers.</p>
<p>It is hoped that newcomers will find C a useful and friendly language. Care must be taken in using C. Many of the extra facilities which it offers can lead to extra types of programming error. You will have to learn to deal with these to successfully make the transition to being a C programmer.</p>
<p>The course aims to introduce programmers to the C language. Previous programming experience is assumed, so we can quickly progress to looking at the features of C and their uses. Students with little programming experience will need to do some homework in order to keep up with the lectures.</p>
<p><span id="more-86"></span></p>
<h3>Structure of a C Program</h3>
<p><b>Example</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*A simple C example program*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>â€Hello Worldâ€<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The output of the program is shown below. This is what appears on the screen when the program runs.</p>
<blockquote><p>Hello World</p></blockquote>
<p>Letâ€™s examine the program line by line. Hereâ€™s the first line.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*A simple C example program*/</span></pre></div></div>

<p>Other than the two forward slash marks that begin the line, it looks pretty much like ordinary English. The /* marks the beginning of a comment. The compiler ignores everything from the double slash to the end of the line. That means you can type anything you want on that line and the complier will never complain. To end the comment you must close it with */</p>
<p>Although comments are not required, they are very important to programmers. Real programs are much more complicated than the example above, and comments help explain whatâ€™s going on.</p>
<p>The next line looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span></pre></div></div>

<p>Because this line starts with a #, it is called a preprocessor directive. The preprocessor reads your program before it is complied and only executes those lines beginning with a # symbol. The word inside the angular brackets <> is a header file name, which is required to do the work of Input / Output. The contents of stdio.h are included at the point where the #include statement appears. The preprocessor directives should not end with a semicolon because preprocessors are messages to the compiler.</p>
<p>The next line reads:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This starts the beginning of a function. A function can be thought of as a group of one or more programming statements that collectively has a name. The name of this function is main, and the set of parentheses enclosing the word void indicate that it is a function.</p>
<p>Although most C programs have more than one function, every C program must have a function called main. It is the starting point of the program. If you are reading someone elseâ€™s C program and want to find where it starts, just look for the function called main.</p>
<p>The next line is a</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">     <span style="color: #009900;">&#123;</span></pre></div></div>

<p>This is called the left brace and marks the opening or beginning of a function, in our case main. All statements of the main are enclosed in a set of braces.</p>
<p>After the opening brace you see the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>â€œHello Worldâ€<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This statement displays the string Hello World on screen. We will discuss more about the printf()later on in this chapter.</p>
<p>The last line is a</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This marks the end of the function main. Since main is the only function in the program it also marks the end of our program.</p>
<p><b>A special mention of the semicolon</b></p>
<p>A semicolon ; is used to mark the end of a statement in C. This allows the complier to understand that the end of a line has been reached. Usually a common mistake is that programmers omit the semicolon and try to compile the program.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>The C Character Set</h3>
<p>A character denotes any alphabet, digit or special symbol used to represent information. The C character set can be defined as a set of characters permissible under the C language to represent information. Under C, the upper case letters A-Z, the lower case letters a-z, the digits 0-9 and certain special symbols are all valid characters that can be used as building blocks to construct basic program elements.</p>
<p>Following is a table of character sets that are available under C</p>
<p><b>Alphabets</b><br />
A, B, C, â€¦â€¦â€¦â€¦â€¦â€¦â€¦ Z<br />
a, b, c, â€¦â€¦â€¦â€¦â€¦â€¦â€¦ z</p>
<p><b>Digits</b><br />
0, 1, 2, 3, 4, 5, 6, 7, 8, 9</p>
<p><b>Special Characters or Symbols</b><br />
+ &#8211; * &#038; ^ % $ # @ ! ? > < { } [ ] ( ) | \ / â€œ â€˜ : ; . , _</p>
<h3>Indentifiers in C</h3>
<p>In a C program every word is either classified as an identifier or a keyword.</p>
<p>Identifiers are used to identify or name various program-elements such as variables, symbolic constants, functions, etc.</p>
<p><b>Rules for Identifiers</b></p>
<ol>
<li>An identifier can contain letters or letters in any order except that the first character must begin with a letter.</li>
<li>C is case-sensitive, therefore upper-case identifiers are different from lower-case characters. E.g. name is different from NAME.</li>
<li>An identifier cannot contain any special character. The only exception is the under score character (_), which can also be taken as the first character of an identifier.</li>
<li>An identifier cannot be a keyword.</li>
</ol>
<p><b>Example of valid identifiers</b></p>
<p>name, rate1, rate_2, _myname, NAME</p>
<p><b>Example of invalid identifiers</b></p>
<p>1stname, my-name, rate 2</p>
<h3>Keywords</h3>
<p>Keywords are words, which have special meaning for the C compiler. Keywords cannot be used as identifiers because the programmer would then change the meaning of its implementation. Keywords are also sometimes known as reserved words.</p>
<p>Following is a list of keywords in C</p>
<table border="0">
<tr>
<td>auto</td>
<td>break</td>
<td>case</td>
<td>char</td>
<td>const</td>
</tr>
<tr>
<td>continue</td>
<td>default</td>
<td>do</td>
<td>double</td>
<td>else</td>
</tr>
<tr>
<td>enum</td>
<td>extern</td>
<td>float</td>
<td>for</td>
<td>goto</td>
</tr>
<tr>
<td>if</td>
<td>int</td>
<td>long</td>
<td>register</td>
<td>return</td>
</tr>
<tr>
<td>short</td>
<td>signed</td>
<td>sizeof</td>
<td>static</td>
<td>struct</td>
</tr>
<tr>
<td>switch</td>
<td>typedef</td>
<td>union</td>
<td>unsigned</td>
<td>virtual</td>
</tr>
<tr>
<td>void</td>
<td>volatile</td>
<td>while</td>
</tr>
</table>
<p>In the next series of tutorials, you will learn Data Types, Constants and Data type modifiers.</p>
<p>Subscribe now to receive updates when a new <b>c tutorial</b> is released.</p>
<p><!--subscribe2--></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/introduction-to-c-programming/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

