<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2866512383960676099</id><updated>2011-11-27T16:54:12.429-08:00</updated><title type='text'>Basics of C Programming</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-4563448539132166131</id><published>2010-02-06T21:43:00.000-08:00</published><updated>2010-02-06T23:45:01.578-08:00</updated><title type='text'>Operators in C - Part 3</title><content type='html'>Continued from &lt;a href="http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-2.html"&gt;Part 2&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;vi) &lt;span style="font-weight: bold;text-decoration:underline;"&gt;Bitwise Operators :&lt;/span&gt; We have already said that, numbers are internally stored as 1's and 0's which are called Bits. Though we may say 8 + 6, internally these numbers are represented in binary and addition is carried out on them. C has a set of operators that allow as to perform the binary operators. These operators comes in handy when we need to speed of certain operations. For example, I need to multiply a number (say 3) by 2. The result of the operation is 6 as we know. In binary 3 is represented as (00000011). Now if we shift the bits by one position towards the left, we get (00000110), which is the equivalent of 6. So inorder to multiply a number by 2, we can left shift the binary equivalent once. If we need to multiply by 4, we have to left shift twice. Let us have a look at the binary operators.&lt;br /&gt;&lt;br /&gt;There are various other uses of these bitwise operations like the &amp; operator (Bitwise And) which comes in handy when we need to find out whether any flag is set.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;int iNum1 = 5; // Binary Equivalent - 0101&lt;br /&gt;   &lt;br /&gt;   int iNum2 = 11; // Binary Equivalent - 1010&lt;br /&gt;   &lt;br /&gt;   // Bitwise And Operation &lt;br /&gt;   printf("%d &amp; %d gives %d\n",iNum1,iNum2,(iNum1&amp;iNum2));&lt;br /&gt;   &lt;br /&gt;   // Bitwise Or Operation &lt;br /&gt;   printf("%d | %d gives %d\n",iNum1,iNum2,(iNum1|iNum2));&lt;br /&gt;   &lt;br /&gt;   // Bitwise Exclusive OR&lt;br /&gt;   printf("%d ^ %d gives %d\n",iNum1,iNum2,(iNum1^iNum2));&lt;br /&gt;   &lt;br /&gt;   // Left Shift&lt;br /&gt;   printf("%d &lt;&lt; 1 gives %d\n",iNum1,(iNum1&lt;&lt;1));&lt;br /&gt;&lt;br /&gt;   // Right Shift&lt;br /&gt;   printf("%d &gt;&gt; 1 gives %d",iNum2,(iNum2&gt;&gt;1));&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="outputheader"&gt;Output&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;5 &amp; 11 gives 1&lt;br /&gt;5 | 11 gives 15&lt;br /&gt;5 ^ 11 gives 14&lt;br /&gt;5 &lt;&lt; 1 gives 10&lt;br /&gt;11 &gt;&gt; 1 gives 5&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;So what has happened, the Bitwise And operator and Or operators followed the truth table as explained in the Relational Operators in &lt;a href="http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-2.html"&gt;this&lt;/a&gt; post. It takes one bit from iNum1 and performs an AND/OR/XOR operation with the corresponding bit in the iNum2 operation. &lt;br /&gt;&lt;br /&gt;Lets perform the And operation following the truth table.&lt;br /&gt;&lt;pre&gt;iNum1 = 5  = 0 1 0 1&lt;br /&gt;             &amp; &amp; &amp; &amp;&lt;br /&gt;iNum2 = 11 = 1 0 1 1&lt;br /&gt;AND gives  = 0 0 0 1 = which Equals to 1 (in decimal)&lt;/pre&gt;&lt;br /&gt;As shown above we perform AND operation on corresponding bits of the numbers.&lt;br /&gt;&lt;br /&gt;Similarly XOR follows the truth table&lt;br /&gt;&lt;table border=1&gt;&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;Output&lt;/td&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Hence (0101) ^ (1011) gives (1110) which is 14 in decimal. &lt;br /&gt;&lt;br /&gt;And this XOR operator can be used for SWAP two numbers without using a tempory variable. (Will explain this later).&lt;br /&gt;&lt;br /&gt;And I have already said about Left shift and right shift operators. They shift the binary digits one position towards left or right. &lt;br /&gt;&lt;br /&gt;vii) &lt;span style="font-weight:bold;text-decoration:underline;"&gt;Conditional or Ternary Operator :&lt;/span&gt; Ok I need to find which is the biggest of two numbers. how will i write the code ?&lt;br /&gt;&lt;pre&gt;if a is greater than b then &lt;br /&gt;  biggest number is a&lt;br /&gt;else &lt;br /&gt;  biggest number is b&lt;/pre&gt;&lt;br /&gt;Now that is 4 lines. Why not in a single line ? Yes for that we use Ternary Operator.  and This is how we write it &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;   int iNum1 = 5; &lt;br /&gt;   &lt;br /&gt;   int iNum2 = 11; &lt;br /&gt;   &lt;br /&gt;   // Ternary Operator - expr1?expr2:expr3&lt;br /&gt;   // expr1 is the condition to be evaluated&lt;br /&gt;   // expr2 is the statement to be executed when condition expr1 is true&lt;br /&gt;   // expr3 is the statement to be executed when condition expr1 is false&lt;br /&gt;   int iNum3= (iNum1 &gt;&gt; iNum2)?iNum1:iNum2;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Expr1 (5 &gt;&gt; 11) is false. So Expr3(iNum2) is evaluated. Thus iNum2 is assigned to iNum3. &lt;br /&gt;&lt;br /&gt;The drawback is only one statement can be used in Expr2 or Expr3. &lt;br /&gt;&lt;br /&gt;viii) &lt;span style="font-weight:bold;text-decoration:underline;"&gt;Special Operators :&lt;/span&gt; There are many special operators like &lt;br /&gt;&amp; - Pointer Operator - Address Of Operator&lt;br /&gt;* - Pointer Operator - Value at Operator&lt;br /&gt;-&gt; - Pointer Operator - Member Selection operator&lt;br /&gt;. - Member Selection operator&lt;br /&gt;, - Comma Operator&lt;br /&gt;sizeof() - SizeOf Operator&lt;br /&gt;&lt;br /&gt;Here I will explain comma(,) operator alone. As the others require detailed post and cannot be stuffed in here. &lt;br /&gt;&lt;br /&gt;Consider the following code, &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;   iNum1 = 5;&lt;br /&gt;   iNum2 = 10;&lt;br /&gt;   iNum3 = iNum1+iNum2;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;This above code can be written in single line using the comma operator like this. &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;      iNum3 = (iNum1 = 5,iNum2 = 10,iNum1+iNum2);&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The comma operator is used for linking two or more related statements. The execution starts from left to right. &lt;br /&gt;&lt;br /&gt;That is all regarding Operators, Will update with more basic information in the future posts :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-4563448539132166131?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/4563448539132166131/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/4563448539132166131'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/4563448539132166131'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-3.html' title='Operators in C - Part 3'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-5793022724217941155</id><published>2010-02-06T06:25:00.000-08:00</published><updated>2010-02-06T23:41:51.398-08:00</updated><title type='text'>Operators in C - Part 2</title><content type='html'>Continuing from &lt;a href="http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-1.html"&gt;Part 1&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;iv) &lt;span style="font-weight:bold;text-decoration:underline;"&gt;Logical Operators :&lt;/span&gt; This operator is typically used for operating on bits. Or we can say that, it operates on other relational expressions. &lt;br /&gt;&lt;br /&gt;Assume I have a scenario where I need to find the greatest of three numbers. What I will do first is, I will compare first two numbers and find out which is bigger. Then I take this bigger number and compare it with the third element. So in order to find a a greatest number, I need to make two comparisions, and in "both" the comparisions the number should be greater. Here comes the usefullness of &amp;&amp; operator (Logical And) &lt;br /&gt;&lt;br /&gt;Here is how we use it : (iNum1 &gt; iNum2) &amp;&amp; (iNum2 &gt; iNum3) : If both the condition evaluates to true, then this expression evaluates to true. If any one fails, then it evaluates to false. Logical And follows the following truth table&lt;br /&gt;&lt;br /&gt;&lt;table border=1&gt;&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;Output&lt;/td&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Here the A and B specifies the two relational expressions. and the Output is the result of Logical end between the two relational expressions.&lt;br /&gt;&lt;br /&gt;Now consider another scenario where I need to find whether I have scored above 90 in atleast one subject. What I will do, I will first compare my first subject mark with 90, if false then I will continue the same with other subjects until I get a subject that has over 90 marks. Now I will use the || operator (Logical Or).&lt;br /&gt;&lt;br /&gt;Here is an example : (Mark1 &gt;&gt; 90) || (Mark2 &gt;&gt; 90) || (Mark3 &gt;&gt; 90). If "atleast" one condition is true, then the expression evaluates to true. The Logical Or follows the following truth table.&lt;br /&gt;&lt;br /&gt;&lt;table border=1&gt;&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;Output&lt;/td&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Now the last Logical operator is the Not Operator (!). It is used for negating an value. It is a unary operator, meaning it operates on only one operand unlike other two. Logical Not follows the following truth table. &lt;br /&gt;&lt;br /&gt;&lt;table border=1&gt;&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;Output&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;v) &lt;span style="font-weight:bold;text-decoration:underline;"&gt;Increments and Decrement Operators :&lt;/span&gt; I have a scenario where I need to perform some set of operations N number of times. So what I will do, I will first assign a variable(say count) to 0. Then for each time the operation is performed, I will add one to it using the expression count=count+1 and then I will compare the value with N. Now people saw that addition/subtaction of one to a variable is more commonly used, and hence they have introduced a very handy operation called Increment and decrement operators.&lt;br /&gt;These are unary operators as they are operating on single operand. &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;   int iNum=0;&lt;br /&gt;&lt;br /&gt;   // Increment Operator&lt;br /&gt;   iNum++;&lt;br /&gt; &lt;br /&gt;   // Outputs 1     &lt;br /&gt;   printf("%d", iNum);&lt;br /&gt;   &lt;br /&gt;   // Decrement Operator&lt;br /&gt;   iNum--;&lt;br /&gt;&lt;br /&gt;   // Outputs 0   &lt;br /&gt;   printf("%d", iNum);&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Ok great, thats a nice little operator. But I need do the comparision, and I need to increment the variable. That is two statements. Why can't I do it in one single statement? C allows us to do something like (iNum++ &gt; 5). Now consider the following example... &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;   int iNum = 5;   &lt;br /&gt;   printf("%d", (iNum++ &gt; 5));&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Guessed the output ? No "1" (non zero) is a wrong answer. It will actually output 0 (false). What is the reason ? Because what we say as iNum++ is a post increment operator. The variable will be first utilised in the comparision and then the value of the variable will be incremented.  Ok If there is something post, then there should be something called preincrement(++iNum). So if I print (++iNum &gt; 5) in the above scenario it will display 1(non-zero). Similarly there is post decrement(iNum--) and pre decrement(--iNum). &lt;br /&gt;&lt;br /&gt;If these increment and decrement operators comes individually in a statement, then post and pre-decrement doesn't make any difference. If it comes alone with some other expressions or in some functions as in the printf statement, it does makes a difference. I will make another detailed post regarding this increment and decrement operator to explain it better. &lt;br /&gt;&lt;br /&gt;Lets have a break and continue this in Part 3&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-5793022724217941155?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/5793022724217941155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5793022724217941155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5793022724217941155'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-2.html' title='Operators in C - Part 2'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-7168654620727408976</id><published>2010-02-06T04:51:00.000-08:00</published><updated>2010-02-06T23:41:13.855-08:00</updated><title type='text'>Operators in C - Part 1</title><content type='html'>Now in order to make some advanced programs, we need to know about this basic thing called Operators.  I have expained earlier about micro processor programming where we use statements like 2E 23,34 for instructing the processor to perform an add operation. But it is very low level, and it is difficult for us. So better approach which C give us is Operators. &lt;br /&gt;&lt;br /&gt;In general terms, an operator is a function that specify specific actions on objects. Those objects which acts as an input to the operator is called an operand. Let us see the different types of operators used in C. The following are categorised based on the operation they perform. &lt;br /&gt;&lt;br /&gt;i) &lt;span style="font-weight:bold;text-decoration:underline;"&gt;Arithmetic Operators :&lt;/span&gt; These operators are used for performing arithmetic operations like Addition, Subtraction, Multiplication, Division, Modulus Operator. &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre id="C_lang" class="prettyprint lang-c"&gt; iNum3 = iNum1 + iNum2; // Addition operation &lt;br /&gt; iNum3 = iNum1 - iNum2; // Subtraction operation       &lt;br /&gt; iNum3 = iNum1 * iNum2; // Multiplication operation       &lt;br /&gt; iNum3 = iNum1 / iNum2; // Division operation       &lt;br /&gt; iNum3 = iNum1 % iNum2; // Modulo operation (Gives remainder of division operation - iNum1/iNum2)&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;While the first four operators can be used for floating point numbers also, modulo operators can be used only for Integers. &lt;br /&gt;&lt;br /&gt;ii) &lt;span style="font-weight:bold;text-decoration:underline;"&gt;Assignment Operators :&lt;/span&gt; If you look at the above code, you could see that we have used "=". This is called the assignment operator. What it does is, evaluates the expression on the right hand side and assigns the result of the evaluation to the operand in the left hand side. Hence the left hand operand cannot be an expression. &lt;br /&gt;&lt;br /&gt;There are also some special assignment operator called Short hand operator. &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre id="C_lang" class="prettyprint lang-c"&gt; iNum1 += iNum2; // Similar to iNum1 = iNum1 + iNum2&lt;br /&gt; iNum1 -= iNum2; // Similar to iNum1 = iNum1 - iNum2       &lt;br /&gt; iNum1 *= iNum2; // Similar to iNum1 = iNum1 * iNum2    &lt;br /&gt; iNum1 /= iNum2; // Similar to iNum1 = iNum1 / iNum2   &lt;br /&gt; iNum1 %= iNum2; // Similar to iNum1 = iNum1 % iNum2&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;This is pretty much useful while we need to store the result in one of the operands. &lt;br /&gt;&lt;br /&gt;iii) &lt;span style="font-weight:bold;text-decoration:underline;"&gt;Relational operators :&lt;/span&gt; These operators are used for comparing two operands. The result of the operation is either true(1 or non zero value) and false(0). It is used in conditional statements where we need to perform certain set of operation if a condition is true, or another set of operations if a condition is false. &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre id="C_lang" class="prettyprint lang-c"&gt;      int iNum1=10;&lt;br /&gt;      int iNum2=20;&lt;br /&gt;&lt;br /&gt;      // Checks whether iNum1 is equal to iNum2&lt;br /&gt;      printf("%d\n",iNum1 == iNum2);&lt;br /&gt;&lt;br /&gt;      // Checks whether iNum1 is greater than iNum2&lt;br /&gt;      printf("%d\n",iNum1 &gt; iNum2);&lt;br /&gt;      &lt;br /&gt;      // Checks whether iNum1 is greater than or equal to iNum2&lt;br /&gt;      printf("%d\n",iNum1 &gt;= iNum2);&lt;br /&gt;      &lt;br /&gt;      // Checks whether iNum1 is lesser than iNum2&lt;br /&gt;      printf("%d\n",iNum1 &lt; iNum2);      &lt;br /&gt;      &lt;br /&gt;      // Checks whether iNum1 is lesser than or equal to iNum2&lt;br /&gt;      printf("%d\n",iNum1 &lt;= iNum2);      &lt;br /&gt;      &lt;br /&gt;      // Checks whether iNum1 is not equal to iNum2&lt;br /&gt;      printf("%d\n",iNum1 != iNum2);&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="outputheader"&gt;Output&lt;/div&gt;&lt;pre id="C_lang" class="prettyprint lang-c"&gt;0&lt;br /&gt;0&lt;br /&gt;0&lt;br /&gt;1&lt;br /&gt;1&lt;br /&gt;1&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;This post is getting pretty big, so lets continue in my next post on operators :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-7168654620727408976?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/7168654620727408976/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7168654620727408976'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7168654620727408976'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2010/02/operators-in-c-part-1.html' title='Operators in C - Part 1'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-1525465904039482493</id><published>2010-01-03T08:16:00.000-08:00</published><updated>2010-01-03T08:38:07.261-08:00</updated><title type='text'>Getting inputs from User</title><content type='html'>Most times, we require interaction from the user, and we process the inputs given by them and display the outputs. Now have a look at the following program which does the function of adding two numbers. It takes two numbers from the user, adds it and displays the output. &lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;&lt;br /&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;      // Declaration of variables.&lt;br /&gt;      int iNum1,iNum2;&lt;br /&gt;      int iSum;&lt;br /&gt;      &lt;br /&gt;      // Getting inputs from user&lt;br /&gt;      printf("Enter the first number  : ");&lt;br /&gt;      scanf("%d",&amp;iNum1);&lt;br /&gt;      printf("Enter the second number : ");\&lt;br /&gt;      scanf("%d",&amp;iNum2);&lt;br /&gt;      &lt;br /&gt;      // Process the inputs&lt;br /&gt;      iSum = iNum1 + iNum2;&lt;br /&gt;      &lt;br /&gt;      // Display the output&lt;br /&gt;      printf("\nSum of %d and %d is %d",iNum1,iNum2,iSum);&lt;br /&gt;      &lt;br /&gt;      getch();&lt;br /&gt;      &lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="outputheader"&gt;Output&lt;/div&gt;&lt;pre class="outputcontent"&gt;Enter the first number  : 23&lt;br /&gt;Enter the second number : 12&lt;br /&gt;&lt;br /&gt;Sum of 23 and 12 is 35&lt;/pre&gt;&lt;/div&gt;The program is straight forward, we first display the message to the user to input the first number, then the inbuilt function "scanf" will get the input from the user. The program execution pauses at this point until the user presses "Enter" key. Once the input is given, you will prompted to enter second number, and after getting the number, it will add the two numbers and displays the output. &lt;br /&gt;&lt;br /&gt;Important thing to be noted here is the arguments passed in the Scanf function. First string parameter is used to define format specifier. It tells the compiler that it should treat the input from the user as specified in that format specifier. Format specifiers used in Scanf are similar to the one explained in &lt;a href="http://c-thebasics.blogspot.com/2009/11/format-specifiers-in-printf.html"&gt;this&lt;/a&gt; post. When I say %d in the scanf, it expects a integer. The next argument in the scanf is the variables where we need to store the data. We use scanf to store the value we enter in some memory location. So we need to pass the address of the variable as the argument. "&amp;" operator in the scanf statement is the address of operator. &amp;iNum1 will give the address of iNum1 variable. So the number that is got from user is stored in the address of iNum1.&lt;br /&gt;&lt;br /&gt;Instead of using two different statements for obtaining the inputs, we can also use one single statement as shown below.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;      printf("Enter two numbers  : ");&lt;br /&gt;      scanf("%d%d",&amp;iNum1,&amp;iNum2);&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="outputheader"&gt;Output&lt;/div&gt;&lt;pre class="outputcontent"&gt;Enter two numbers  : 12 23&lt;br /&gt;&lt;br /&gt;Sum of 12 and 23 is 35&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;As shown above, we need to separate the two inputs with a space/tab/new line, if we need to give the two inputs in the same scanf statement.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-1525465904039482493?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/1525465904039482493/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2010/01/getting-inputs-from-user.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/1525465904039482493'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/1525465904039482493'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2010/01/getting-inputs-from-user.html' title='Getting inputs from User'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-4612890564050901129</id><published>2009-11-08T07:42:00.000-08:00</published><updated>2010-01-03T08:08:47.641-08:00</updated><title type='text'>Printf - Format Specifiers</title><content type='html'>I have given the program and the output corresponding to it. Also I have added comments so that it will be clear. &lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;      // Declaration of Variables&lt;br /&gt;      int iNum=86;&lt;br /&gt;      char cChar='V';&lt;br /&gt;      float fNum=86.1234567890;&lt;br /&gt;      double dNum=86.1234567890;&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      // To print the integer value&lt;br /&gt;      printf("Integer - %d\n",iNum);&lt;br /&gt;      // If I use %c for a integer, it will print its ASCII equivalent character&lt;br /&gt;      printf("Printing Number as Character - %c\n",iNum);&lt;br /&gt;      // I dont have any idea why it is displaying 0. &lt;br /&gt;      printf("Printing Number as Float - %f\n",iNum);&lt;br /&gt;      // I dont have any idea why it is displaying 0. &lt;br /&gt;      printf("Printing Number as Double - %lf\n",iNum);&lt;br /&gt;      // Displays hex equivalent of a number&lt;br /&gt;      printf("Printing Number as Hex - %x\n\n",iNum);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      // Displaying a Character&lt;br /&gt;      printf("Character - %c\n",cChar);     &lt;br /&gt;      // If I use %d for a charcter, it will print its ASCII equivalent number&lt;br /&gt;      printf("Printing Character as Number - %d\n",cChar);      &lt;br /&gt;      // I dont have any idea why it is displaying 0. &lt;br /&gt;      printf("Printing Character as Float - %f\n",cChar); &lt;br /&gt;      // I dont have any idea why it is displaying 0.  &lt;br /&gt;      printf("Printing Character as Double - %lf\n",cChar);&lt;br /&gt;      // Gets the ASCII equivalent(in Hex) of character&lt;br /&gt;      printf("Printing Character as Hex - %x\n\n",cChar);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      // Displaying Float Variable - Precision is only 6 digits&lt;br /&gt;      printf("Float - %f\n",fNum);&lt;br /&gt;      // To explain the precision I am going to print 10 digits after decimal&lt;br /&gt;      printf("Float - %.10f\n",fNum);&lt;br /&gt;      // Displaying Float as a integer&lt;br /&gt;      printf("Printing float as integer - %d\n",fNum);&lt;br /&gt;      // Check the explanation for Displaying Double as Character&lt;br /&gt;      printf("Printing float as character - %c\n",fNum);&lt;br /&gt;      // Displaying Float as a Double.&lt;br /&gt;      printf("Printing float as double - %lf\n",fNum);&lt;br /&gt;      // Displays hex equivalent of a float - May be I have to understand how &lt;br /&gt;      // floats are stored internally.   &lt;br /&gt;      printf("Printing float as hex - %x\n\n",fNum);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      // Using double Variable&lt;br /&gt;      printf("Double - %lf\n",dNum);&lt;br /&gt;      // To explain the precision I am going to print 10 digits after decimal&lt;br /&gt;      // Compare this precision with corresponding printf statement given in float. &lt;br /&gt;      printf("Double - %.10f\n",dNum);&lt;br /&gt;      // May be due to how double is stored internally, Check the Hex equivalent &lt;br /&gt;      // of double below. &lt;br /&gt;      printf("Printing double as integer - %d\n",dNum); &lt;br /&gt;      // Displays 'Y' because it is the ASCII equivalent of HEX Value 59&lt;br /&gt;      // since 59 is the LSB, and Char takes only 1 byte as I have explained&lt;br /&gt;      // in the previous posts. Same explanation applies for displaying Float as Char     &lt;br /&gt;      printf("Printing double as character - %c\n",dNum);&lt;br /&gt;      // Printing a double value as float. &lt;br /&gt;      printf("Printing double as float - %f\n",dNum);&lt;br /&gt;      // Double in Hex format&lt;br /&gt;      printf("Printing double as hex - %x\n\n",dNum);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      // Printing a hex value. If we put 0x before a number, compiler treats it as&lt;br /&gt;      // a hex number. &lt;br /&gt;      printf("Printing Hex Value directly - %x\n",0x15);&lt;br /&gt;      // Printing a hex equivalent of a decimal number&lt;br /&gt;      printf("Printing Hex Value of a Number - %x\n",15);&lt;br /&gt;      // Hex value with capitalized characters A,B,C,D,E,F&lt;br /&gt;      printf("Printing Hex Value of a Number(capitalized) - %X\n\n",15);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      // Printing a Octal Value - If we put 0 before a number, compiler treats it as&lt;br /&gt;      // a Octal number&lt;br /&gt;      printf("Printing Octal Value directly - %o\n",015);&lt;br /&gt;      // Printing a octal equivalent of a decimal number&lt;br /&gt;      printf("Printing Octal Value of a number - %o\n\n",15);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      //Octal to Hex Conversion &lt;br /&gt;      printf("Printing Hex value of Octal Value - %x\n",010);&lt;br /&gt;      //Hex to Octal Conversion &lt;br /&gt;      printf("Printing Octal value of Hex Value - %o\n\n",0x10);&lt;br /&gt;      &lt;br /&gt;      &lt;br /&gt;      getch();&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="outputheader"&gt;Output&lt;/div&gt;&lt;pre class="outputcontent"&gt;Integer - 86&lt;br /&gt;Printing Number as Character - V&lt;br /&gt;Printing Number as Float - 0.000000&lt;br /&gt;Printing Number as Double - 0.000000&lt;br /&gt;Printing Number as Hex - 56&lt;br /&gt;&lt;br /&gt;Character - V&lt;br /&gt;Printing Character as Number - 86&lt;br /&gt;Printing Character as Float - 0.000000&lt;br /&gt;Printing Character as Double - 0.000000&lt;br /&gt;Printing Character as Hex - 56&lt;br /&gt;&lt;br /&gt;Float - 86.123459&lt;br /&gt;Float - 86.1234588623&lt;br /&gt;Printing float as integer - -1073741824&lt;br /&gt;Printing float as character -&lt;br /&gt;Printing float as double - 86.123459&lt;br /&gt;Printing float as hex - c0000000&lt;br /&gt;&lt;br /&gt;Double - 86.123457&lt;br /&gt;Double - 86.1234567890&lt;br /&gt;Printing double as integer - -1219637671&lt;br /&gt;Printing double as character - Y&lt;br /&gt;Printing double as float - 86.123457&lt;br /&gt;Printing double as hex - b74dce59&lt;br /&gt;&lt;br /&gt;Printing Hex Value directly - 15&lt;br /&gt;Printing Hex Value of a Number - f&lt;br /&gt;Printing Hex Value of a Number(capitalized) - F&lt;br /&gt;&lt;br /&gt;Printing Octal Value directly - 15&lt;br /&gt;Printing Octal Value of a number - 17&lt;br /&gt;&lt;br /&gt;Printing Hex value of Octal Value - 8&lt;br /&gt;Printing Octal value of Hex Value - 20&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;All questions that comes with why it is displaying like this for printf statements that are related to float, can be better understood if we know how float is stored internally. I will try to see how float how works exactly and then will try to make a post on this.&lt;br /&gt;&lt;br /&gt;Also I did not get the difference between %f, and %lf when I try to print float and double values. I will be happy to hear from someone what is the difference between the two.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-4612890564050901129?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/4612890564050901129/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/format-specifiers-in-printf.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/4612890564050901129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/4612890564050901129'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/format-specifiers-in-printf.html' title='Printf - Format Specifiers'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-7391695619861689593</id><published>2009-11-07T04:10:00.000-08:00</published><updated>2009-11-07T05:30:43.653-08:00</updated><title type='text'>Using printf to display output</title><content type='html'>Let us continue from &lt;a href="http://c-thebasics.blogspot.com/2009/10/using-datatypes-and-variables.html"&gt;here&lt;/a&gt;. I have used printf statement to display the output. From the statement and the output I have given in that post, you would have guessed what should have happened. Still I will explain what is happening. &lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;printf("Sum of %d and %d is %d\n",iNum1,iNum2,iSum);&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="outputheader"&gt;Output&lt;/div&gt;&lt;pre class="outputcontent"&gt;Sum of 23 and 33 is 56&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;If you see the printf I have used in the Basic C program, it has only one argument(parameters passed to a function) - the string "Welcome to http://c-thebasics.blogspot.com". But here the number of arguments is 4. Thus the number of arguments can vary. Also the first argument should always be a string.   &lt;br /&gt;&lt;br /&gt;If you could see the code and the output, the first %d is replaced by iNum1, and second %d by iNum2, and third %d by iSum. But what is this %d ? This is the format specifier that can used in the string. The format specifier used should be based on the data type of the variable or how it should be interpretted. Since iNum1 is of integer type, we use %d. Similarly for other data types we have other format specifiers as given in the following table.&lt;br /&gt;&lt;br /&gt;&lt;table border="1px" cellpadding="2" cellspacing="2"&gt;&lt;tr&gt;&lt;th&gt;Format Specifier&lt;/th&gt;&lt;th&gt;Used for&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%d&lt;/td&gt;&lt;td&gt;int&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%u&lt;/td&gt;&lt;td&gt;Unsigned Integer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%lu&lt;/td&gt;&lt;td&gt;Unsigned Long Integer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%lu&lt;/td&gt;&lt;td&gt;Signed Long Integer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%o&lt;/td&gt;&lt;td&gt;Octal Number&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%f&lt;/td&gt;&lt;td&gt;float&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%lf&lt;/td&gt;&lt;td&gt;double&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%c&lt;/td&gt;&lt;td&gt;char&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%s&lt;/td&gt;&lt;td&gt;string&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%x&lt;/td&gt;&lt;td&gt;Hex Format(a,b,c,d,e,f)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%X&lt;/td&gt;&lt;td&gt;Hex Format(A,B,C,D,E,F)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;%%&lt;/td&gt;&lt;td&gt;To print %&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;Apart from the above, there are something called escape sequences which is used for doing special things. Following is some of the escape characters that can be used in strings. &lt;br /&gt;&lt;br /&gt;&lt;table border="1px" cellpadding="2" cellspacing="2"&gt;&lt;tr&gt;&lt;th&gt;Escape sequences&lt;/th&gt;&lt;th&gt;Used for&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\n&lt;/td&gt;&lt;td&gt;Takes the cursor to the first column of next line&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\n&lt;/td&gt;&lt;td&gt;Takes the cursor to the first column of same line&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\b&lt;/td&gt;&lt;td&gt;Takes the cursor to the previous column in same line&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\t&lt;/td&gt;&lt;td&gt;Takes the cursor to the next tab&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\a&lt;/td&gt;&lt;td&gt;Creates a beep sound&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\\&lt;/td&gt;&lt;td&gt;to print \&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\'&lt;/td&gt;&lt;td&gt;to print '&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;\"&lt;/td&gt;&lt;td&gt;to print "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;These are not the only format specifiers and escape sequences. There are many others, we will look into them later if needed.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-7391695619861689593?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/7391695619861689593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/using-printf-to-display-output.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7391695619861689593'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7391695619861689593'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/using-printf-to-display-output.html' title='Using printf to display output'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-3004877744970149088</id><published>2009-11-07T01:19:00.000-08:00</published><updated>2009-11-07T03:37:28.597-08:00</updated><title type='text'>Best Coding Practises</title><content type='html'>Next most basic thing that one should know before getting into Programming is, some best Coding Practises. These practises are recommended to be followed for writing error free and efficient programs.&lt;br /&gt;&lt;br /&gt;1) Commenting : Whenever you write a program logic, make sure you comment about the logic. This helps you understand underlying codes and the logic in it, when you revisit the code at a later time. &lt;br /&gt;&lt;br /&gt;2) Variable Names : It is recommended to give a variable name, which is meaningful. One common mistake programmers do is, giving variable name as i,j,k etc for array indexers. &lt;br /&gt;&lt;br /&gt;3) Indentation : Make sure you properly indent blocks of codes, so that it is easily readable. When there are lots of opening and closing braces your code will become unmanageable if you didnot indent. You wont be able to see where a block of code starts and ends. &lt;br /&gt;&lt;br /&gt;4) User Inputs : Make sure that program logic will be able to handle all possible user inputs. Many times, we wont see what will be the possible inputs from the users. For eg) assume that we wrote a program to find whether a number is prime or not. Most times, we wont check for the integrity of the user inputs. If we give negative numbers, then also it will show whether the number is prime or not. Sometimes our code will say 1 as prime number, if we did not make necessary checking for the user input as 1. Take another example of finding average of 5 marks which is given by user. Most times we assume that user will give something between 0 and 100. But what if the user gives a negative number or something above 100. The program should ignore such cases/throw errors.&lt;br /&gt;&lt;br /&gt;5) Overflow/Truncation: When the result of addition or mulitplication increases beyond the actual value a datatype can hold, error occurs. And instead of floating point operation as in finding average, normal integer operation is performed error will occur in the result. So checking the data type of variables used is very important and we should make sure that user inputs corresponds to the variable of the data type&lt;br /&gt;&lt;br /&gt;6) Memory : Use variables of appropriate data types so that we use memory for what we need and we wont be wasting memory. It is not a good practise to use long int, when you are sure that values stored in the variables will be in the range which int variable can hold. Usage of arrays with very large maximum index will create wastage of memory. Decide on the maximum index, which you thing you will need. There is other options like Linked List also. &lt;br /&gt;&lt;br /&gt;7) Code Optimization : Make sure you follow code tuning techniques, so that your code will use less memory and takes less times to execute. Loops should be properly handled, and exit conditions should be available in the loops so that it wont get into infinite loop. &lt;br /&gt;&lt;br /&gt;8) Error Handling : The Program should have appropriate error handling techniques and the user should be displayed with messages which the user can understand. Logging can be done for debugging incase of any errors, so that we will be easily trace the problem and fix them.  &lt;br /&gt;&lt;br /&gt;Incase if you find any improvements that can be done in the code snippets that you find in this blog, let me know so that others will be benefited.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-3004877744970149088?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/3004877744970149088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/best-coding-practises.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/3004877744970149088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/3004877744970149088'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/best-coding-practises.html' title='Best Coding Practises'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-3952486222385019038</id><published>2009-11-06T20:42:00.000-08:00</published><updated>2009-11-06T23:06:12.859-08:00</updated><title type='text'>Compilation Process</title><content type='html'>Everytime I say compilation and execution. What is exactly happening when I say compilation and execution. We write a program in Dev CPP, save it and then compile it. What internally happens is some series of steps that will create an executable file which can be understood by the operating system so that we can run/execute it. &lt;br /&gt;&lt;br /&gt;The program we write is according to the standards/syntax that is specified for C. Only then the C Compilers can understand our programs. These standards are specified keeping in mind, how easy the developers can understand the programs. But the computer system can understand only 0s and 1s (which will be difficult for the developers to deal with). It doesnt know what is "printf" and what is "scanf" unless it is converted to someother form the computer can understand. This translation of our source program to machine understandable object codes is done by compilers. But will it blindly convert whatever we write ? definitely not. So lets look into what are all the steps that are taking place...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Step 1: Lexical Analysis or Linear Analysis or Scanner :&lt;/span&gt; All the statements in the C program are further divided into tokens. &lt;br /&gt;There are 5 types of tokens &lt;br /&gt;a) Identifiers eg) Variables&lt;br /&gt;b) Constants eg) 3,5,10 etc&lt;br /&gt;c) Operators eg) +, -, (, ) etc&lt;br /&gt;d) Keywords eg) if, else etc&lt;br /&gt;e) Delimitors/Separators eg) ;&lt;br /&gt;&lt;br /&gt;Say we have the following piece of code, &lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;if(x&lt;5)&lt;br /&gt;   x = x + 2;&lt;br /&gt;else &lt;br /&gt;   x = x + 10;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Here the tokens that will be generated are &lt;br /&gt;Keywords : if , else&lt;br /&gt;Identifier : x&lt;br /&gt;Constants : 2, 10&lt;br /&gt;Operators : +,= &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Step 2: Syntax Analysis :&lt;/span&gt; It checks for syntax of our statements. It will then generate a parse tree for our statements. Something like the following (eg. x=2+5)&lt;br /&gt;&lt;pre&gt;  =&lt;br /&gt; / \&lt;br /&gt;x   +&lt;br /&gt;   / \&lt;br /&gt;  2   5&lt;br /&gt;&lt;/pre&gt; &lt;br /&gt;In case of any errors in the syntax like x=x+; It misses one identifier/constant after '+' operator. So it will generate an error.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Step 3: Semantic Analysis :&lt;/span&gt; Checks for type mismatch. Say we have declared variable x as float. And we are trying to use Modulo operator (% - finds the reminder in division operation) on x. The type of x is float, and we cant use % on that variable. So this causes an semantic error. &lt;br /&gt;&lt;br /&gt;Now when the same program is used in different platforms/machines and compiled, the above phases are the same. The above operations output only changes when the Source Language is changed. So since the above phases are largely dependent on the source language, they are called front-end operations. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Step 4:  Intermediate Code Generation :&lt;/span&gt; The actual machine understandable code is generated after phases like Code-Optimization(step 5) and Code Generation(step 6). Since they are largely dependent on the Target Machine, they are called Back-End Operations. And these phases are independent on the Source Program used. &lt;br /&gt;&lt;br /&gt;In order to make compilation process more efficient, so that we need to have less number of backend and front end operations for compiling from programs in many source languages for many platforms, we need to introduce this step of Intermediate Code Generation. This creates a logical separation between the machine dependent phases and source dependent phases. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Step 5: Code Optimization:&lt;/span&gt; Inorder to make the execution of programs efficient, like usage of less resources and time efficient, compiler will optimize our code upto some extent. We should also remember that we should write the codes by following some code tuning techniques (will be explained later). &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Step 6: Code Generation:&lt;/span&gt; Final step is the actual generation of code for the target machine. It is dependent on the architecture of the CPU and other devices. &lt;br /&gt;&lt;br /&gt;In all the steps, compiler will make use of Hash Tables to keep track of name,type, address,size of variables. It will also make use of Error Processing module for reporting and recovery of errors that occurs in each stage of compilation. Error Reporting helps in getting the line in which error has occured and possible reason. Error Recovery will try to correct or skip lines where error has occurred and will allow the compiler proceed with compilation for the rest of the lines. &lt;br /&gt;&lt;br /&gt;Thats the basics about compilation process. Now the generated output code can be executed to give the actual output of the program.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-3952486222385019038?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/3952486222385019038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/compilation-process.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/3952486222385019038'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/3952486222385019038'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/compilation-process.html' title='Compilation Process'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-7556843975582031651</id><published>2009-11-01T00:43:00.000-07:00</published><updated>2009-11-05T09:12:36.416-08:00</updated><title type='text'>More about using Variables and Datatypes</title><content type='html'>Let us continue from my previous &lt;a href="http://c-thebasics.blogspot.com/2009/10/using-datatypes-and-variables.html"&gt;post&lt;/a&gt;. I said I have a question. Lets look into it. &lt;br /&gt;&lt;br /&gt;Memory location is mapped to variable name. But will it map all memory of all 4 bytes to Variable Name. Answer is, it actually maps only the memory location of lower most byte. By the way, Lower most byte is the least significant byte.&lt;br /&gt;&lt;br /&gt;Say I have 1023 (binary equivalent - 00000011 11111111). We assume that Most Significant Byte(00000011) is stored in memory location 2001 and 11111111 is stored in memory location 2000. Then variable name will be mapped to memory location 2000. &lt;br /&gt;&lt;br /&gt;And why does it map to 2000. Why not 2001 ? &lt;br /&gt;&lt;br /&gt;Before coming to the answer we should know some basic terms. &lt;br /&gt;1 bit - 0 or 1&lt;br /&gt;1 byte - 8 bits&lt;br /&gt;&lt;br /&gt;Every system has some basic memory storage called WORD. A word is grouping of bytes (It may be 2,4 etc. and it depends on the system.) What the system does is, it gives memory address to each WORD. Assume a system uses 4 bytes for a word, and I write a  a C program, where I declare a character variable (which takes 1 byte) and declare 2 more integer variable (which takes 4 bytes).  Usually when the program is very small with only 2 variables, during execution those variables are stored in continous memory locations. So I will have a look at the address mapped to the variable. What i see is that memory address 2000 is assigned for character variable and memory addresss 2004 and 2008 is assigned for the two integer variables. So even though the character variables takes only 1 bytes, actually the memory used up is 4 bytes (I dont know why, may be due to the basic memory storage concept). &lt;br /&gt;&lt;br /&gt;Note : I will come up with the program used for this purpose later while explaining memory allocated for data types.&lt;br /&gt;&lt;br /&gt;So memory locations 2000, 2001, 2002, 2003 where in use. But actually when we store something in the character variable, it gets stored in 2000. Hence Instead of mapping 2003 to the character variable, 2000 is mapped to the character varaible. Also while making basic operations like addition, we usually start from the Least significant digit. So it will be easy if the program fetches the LSB first and then proceed to the MSB. Inorder to avoid the overload in finding the LSB, memory location of LSB is mapped to the variable. &lt;br /&gt;&lt;br /&gt;We will better understand this concepts while we come across UNIONS (a secondary data type).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-7556843975582031651?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/7556843975582031651/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/more-about-using-variables-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7556843975582031651'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7556843975582031651'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/11/more-about-using-variables-and.html' title='More about using Variables and Datatypes'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-5214824362496090603</id><published>2009-10-31T23:33:00.001-07:00</published><updated>2009-11-06T20:26:04.471-08:00</updated><title type='text'>Using datatypes and variables</title><content type='html'>Let us write a simple program for using data types, which were explained in the previous few posts. Have a look at the simple program to add 2 numbers. &lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;  /* Declaration of Variables (Input) */&lt;br /&gt;  int iSum;&lt;br /&gt;  int iNum1=23;&lt;br /&gt;  int iNum2=33;  &lt;br /&gt;&lt;br /&gt;  /* Processing Input */&lt;br /&gt;  iSum = iNum1 + iNum2;&lt;br /&gt;&lt;br /&gt;  // Display Output &lt;br /&gt;  printf("Sum of %d and %d is %d\n",iNum1,iNum2,iSum);&lt;br /&gt;&lt;br /&gt;  /* Changing inputs */&lt;br /&gt;  iNum1 = 10;&lt;br /&gt;  iNum2 = 23;&lt;br /&gt;  &lt;br /&gt;  /* Process Again */&lt;br /&gt;  iSum = iNum1 + iNum2;&lt;br /&gt;&lt;br /&gt;  // Display Output Again&lt;br /&gt;  printf("Sum of %d and %d is %d",iNum1,iNum2,iSum);&lt;br /&gt;&lt;br /&gt;  getch();&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;The above program introduces many new things to you. First thing is Comments. To make the program look more understandable, we can use comments to describe certain blocks of code. So we use comments. Comment Lines are ignored by compiler while compiling. There are two types of comments - Single line comment and Multiline comment. Single Line comment can be made by starting the line with // and multiline comment can be made by starting the comment with /* and ending the comment with */&lt;br /&gt;&lt;br /&gt;Next thing that is introduced here is variables and data types. Recall the third paragraph in this &lt;a href="http://c-thebasics.blogspot.com/2009/10/what-is-c-programming-language.html"&gt;Post&lt;/a&gt;. We can store data/values in memories and can give those memory locations as input to the add function. So if we need to change the value for which we need to find sum, we simply change the value in those memory location and call the sum function with the same address location. &lt;br /&gt;&lt;br /&gt;But as explained earlier, C program makes things easy for us. We don't need to specify where to store our data. It simply finds a free memory location and puts our value in it. So how do we know where our value is stored ? How can we change the value in that memory location ? That is where Variables  comes into picture. We just name the memory location (whatever it might be). We use that variable name to identify the memory location. Internally C will map the variables names to the memory location using a Table maintained by it. &lt;br /&gt;&lt;br /&gt;Thus when we DECLARE a variable(int iSum;), a new memory location is allocated and the address is mapped to the variable name iSum. So even without knowing the memory location, we would be able to change the values in that memory location. Hence the name "Variables". Now when we INITIALIZE a variable(int iNum1=23;), inaddition to declaration, the program also stores the value 33 in the memory allocated. &lt;br /&gt;&lt;br /&gt;Now I know you will have a question. What will be present in the Variable which is not initialized? At any point of time, a memory location will have random combination of 0s and 1s or some value which was previously stored during execution of someother program. So it will contain some random number which we can't guess. We call those values GARBAGE VALUES. &lt;br /&gt;&lt;br /&gt;Ok I have said it will allocate memory for the variable. But how many bytes of memory will it allocate ? Now comes datatype into picture. As explained &lt;a href="http://c-thebasics.blogspot.com/2009/10/more-about-data-types.html"&gt;here&lt;/a&gt;, Int means integer and the memory size for it is 4 bytes (depending on the compiler/system). So when we specify "&lt;span style="font-weight:bold;"&gt;int&lt;/span&gt; a", 4 bytes of memory is allocated. (Now I have a question again, have a look at the next post for what it is)&lt;br /&gt;&lt;br /&gt;Now the statement for processing the input(iSum = iNum1 + iNum2;), It is something like 2E [2000] [2004]  (as explained &lt;a href="http://c-thebasics.blogspot.com/2009/10/what-is-c-programming-language.html"&gt;here&lt;/a&gt;). Instead of giving the memory address [2000],[2004] we specify the variable names. Instead of specifying 2E, we specify the operator +. Now we can also specify where to store the result through &lt;span style="font-weight:bold;"&gt;iSum =&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now the output will be something like this&lt;br /&gt;&lt;br /&gt;Sum of 23 and 33 is 56&lt;br /&gt;Sum of 10 and 23 is 33&lt;br /&gt;&lt;br /&gt;Let us see about the statement Printf in a new Post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-5214824362496090603?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/5214824362496090603/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/using-datatypes-and-variables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5214824362496090603'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5214824362496090603'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/using-datatypes-and-variables.html' title='Using datatypes and variables'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-3144257709904317721</id><published>2009-10-24T20:25:00.000-07:00</published><updated>2009-10-31T20:33:13.429-07:00</updated><title type='text'>More about Data Types</title><content type='html'>For the reasons stated &lt;a href="http://c-thebasics.blogspot.com/2009/10/internal-storage-of-data.html"&gt;here&lt;/a&gt;, there should be some data type range that every system should have. Depending on the sytem/compiler you are working on, these range of data types can vary. So don't worry if you see different results in your computer (I will give and explain a program later to see what is the range of data type that your computer can store).&lt;br /&gt;&lt;br /&gt;&lt;table border="1px" cellpadding="2" cellspacing="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Data Type&lt;/th&gt;&lt;th&gt;Size&lt;/th&gt;&lt;th&gt;Range&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;int&lt;/td&gt;&lt;td&gt;4 bytes&lt;/td&gt;&lt;td&gt;–2,147,483,648 to 2,147,483,647&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;char&lt;/td&gt;&lt;td&gt;1 byte&lt;/td&gt;&lt;td&gt;–128 to 127&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;float&lt;/td&gt;&lt;td&gt;4 bytes&lt;/td&gt;&lt;td&gt;3.4 e-38 to 3.4 e+38 (7 digits precision)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;double&lt;/td&gt;&lt;td&gt;4 bytes&lt;/td&gt;&lt;td&gt;1.7e-308 to 1.7e+308 (15 digits precision) &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;There are variations of primitive data types such as signed and unsigned char, signed and unsigned int, short and long int, signed and unsigned long int, &lt;br /&gt;Note : 1 byte = 8 bits&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-3144257709904317721?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/3144257709904317721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/more-about-data-types.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/3144257709904317721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/3144257709904317721'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/more-about-data-types.html' title='More about Data Types'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-4095850456275791532</id><published>2009-10-24T20:00:00.001-07:00</published><updated>2009-10-24T20:30:44.991-07:00</updated><title type='text'>Internal Storage of Data</title><content type='html'>Digital Computers store datas in zeroes and ones. Any data we want to represent are converted to &lt;a href="http://en.wikipedia.org/wiki/Binary_numeral_system"&gt;binary equivalent&lt;/a&gt; (zeroes and ones) and stored in the memory. Let us say we have 33, the binary equivalent of 33 is 100001. 33 will be actually stored as 100001 internally. Similarly if we have a character 'A', the &lt;a href="http://en.wikipedia.org/wiki/ASCII"&gt;ASCII&lt;/a&gt; equivalent of that character is taken and it is then converted to the binary equivalent and stored.&lt;br /&gt;&lt;br /&gt;But had a look at the variations in the number of digits in the binary equivalent ? 33 takes 6 characters (100001), but 23 takes 5 (10111), similarly the number of bits (binary digits) differ for each number. So to create uniformity and to make the system understand where a number/character starts and end, they have introduced something called padding of zeroes. We will have fixed number of bits, say 8. So every number will be stored as 8 bit binary number. That is 33 will be stored as 00100001 and 23 will be stored as 00010111. This makes the compiler easy to understand where it should start reading a number.&lt;br /&gt;&lt;br /&gt;Ok, What can we do for storing characters whose ASCII equivalent is also a number and is stored as binary the same way as a number. Wont there be a contracdiction whether it is a character or number? How can we differentiate it is a number or a character ? This is the reason we have data types. We tell the compiler whatever stored in this memory location is a charcter, and whatever we store here is a number through data types.&lt;br /&gt;&lt;br /&gt;Ok, But 8 bits can store only from 0 to 255 (from 00000000 to 11111111). What can we do for higher numbers? Ok we can increase the number of bits from 8 to 16 or 32 or 64 etc. But the problem of extending the number of bits required to store one number is, we have to append more and more zeroes thus wasting the memory. This is one of the reasons why we have variations in Primary Data types. Also we need to have something called, range of values each data types can store. If we use the correct data types, we can save memory.&lt;br /&gt;&lt;br /&gt;Next post will explain the range of values that can be used by a data type.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-4095850456275791532?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/4095850456275791532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/internal-storage-of-data.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/4095850456275791532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/4095850456275791532'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/internal-storage-of-data.html' title='Internal Storage of Data'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-5264716226191425535</id><published>2009-10-24T19:32:00.000-07:00</published><updated>2009-10-24T19:58:27.977-07:00</updated><title type='text'>Data Types in C</title><content type='html'>When we are reading something, we know that it is a alphabet, it is a number, it is a decimal number etc. For the compiler to know what type of data we are dealing with, we must specify the data type. Following are the primary data types that can be used in C.&lt;br /&gt;&lt;br /&gt;&lt;table border="1px" cellpadding="2" cellspacing="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Example&lt;/th&gt;&lt;th&gt;Data Type Name&lt;/th&gt;&lt;th&gt;Data Type&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;123, -23&lt;/td&gt;&lt;td&gt;Integers&lt;/td&gt;&lt;td&gt;int&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;12.5, 3.4E-38&lt;/td&gt;&lt;td&gt;Floating Point&lt;/td&gt;&lt;td&gt;float&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;a&lt;/td&gt;&lt;td&gt;Characters&lt;/td&gt;&lt;td&gt;char&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1.7E-308&lt;/td&gt;&lt;td&gt;Double Precision Float&lt;/td&gt;&lt;td&gt;double&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;When I say primary, there are some secondary/derived data types also. We will  look into those data types later. Also there are some variations of primary data types. To know more about, those data types, we need to know what is actually happening internally. My next post will explain how computers store data.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-5264716226191425535?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/5264716226191425535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/data-types-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5264716226191425535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5264716226191425535'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/data-types-in-c.html' title='Data Types in C'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-5060541227638517498</id><published>2009-10-21T08:11:00.001-07:00</published><updated>2009-11-07T04:17:27.963-08:00</updated><title type='text'>Basic C Program</title><content type='html'>Enough of basics, we will quickly move into programming. I will start with a very simple program and start explaining to you on how to write a C Program and compile it using Dev CPP. Whatever may be the compiler, the steps will be almost similar. Have a look at the following program. It simply displays "Welcome to http://c-thebasics.blogspot.com".&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;  printf("Welcome to http://c-thebasics.blogspot.com");&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;As I have already described in &lt;a href="http://c-thebasics.blogspot.com/2009/10/more-about-c-programming-language.html"&gt;this&lt;/a&gt; post, there are many inbuilt functions and they are grouped to form library files. These files can be included in the program and the functions in those library can be used. The first statement includes the standard input/output library. Extension of the file .h stands for Header file, it contains the definition (let us see what they are later) for input/output functions. This is called a Pre-processor directive (Processed before compilation phase)&lt;br /&gt;&lt;br /&gt;main() - Is a user defined function. Every program starts execution from this point. The opening brace { and closing brace } tells the compiler where the function starts and ends.&lt;br /&gt;&lt;br /&gt;printf - As I have said, it is a inbuilt function. Its job is to display whatever is given as input to it. Here the input is a string (group of characters).&lt;br /&gt;&lt;br /&gt;Now what to do with this piece of code ? Open your C compiler, and open a new source file in it. Type the above code and save it as Welcome.c , where .c is the file extension.&lt;br /&gt;&lt;br /&gt;If you are using Dev C++, go to Build -&gt;  Compile &amp;amp; Run (F9).&lt;br /&gt;If you are using TurboC, go to Run -&gt; Run (Ctrl + F9)&lt;br /&gt;&lt;br /&gt;Ok, but what happened ? Nothing seemed to come up? Yes, because the output window has already came and closed in Dev C++. In TurboC, Press Alt+F5, to get to the outscreen.&lt;br /&gt;&lt;br /&gt;In DevC++, the window has already closed. So we need a little work around to see the output.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="codeheader"&gt;Code&lt;/div&gt;&lt;pre class="prettyprint lang-c" id="C_lang"&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;  printf("Welcome to http://c-thebasics.blogspot.com");&lt;br /&gt;  getch();&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;So what have we added extra ? getch(); What it does ? It is again a inbuilt function, and it is used for accepting a Single Character from our Keyboard, so until we press any key, the cursor stays there, Hence we will see the output. So usually I used to put this getch(); as the last line in a program.&lt;br /&gt;&lt;br /&gt;Did you note something ? we have a semi colon at the end of every statement. This is how we tell the compiler that its an end of a line/statement.&lt;br /&gt;&lt;br /&gt;Another point to be noted here is that, the above program still runs fine in Dev C++, without errors even if the stdio.h header file is not included. Possible reason is that, it is automatically included by DevC++ compiler when compiling &amp;amp; executing the program. In TurboC, it will show error.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-5060541227638517498?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/5060541227638517498/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/basic-c-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5060541227638517498'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/5060541227638517498'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/basic-c-program.html' title='Basic C Program'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-7617629382853313158</id><published>2009-10-19T07:11:00.000-07:00</published><updated>2009-10-21T07:41:07.371-07:00</updated><title type='text'>What you need ?</title><content type='html'>You should have an operating system installed in your machine :d. And you will require one C Programming complier like Turbo C, Borland C etc for your operating system. Just google for the names you will find the place to download them. Install that compiler into your system.&lt;br /&gt;&lt;br /&gt;I use Dev CPP. It is a C++ compiler, but it can compile C source also. I prefer using that because I feel it is easy for me to edit and compile the programs. Syntax highlighting also helps me a lot. Also if there is any run time errors, or infinite loops it will be easy for me to quit the program, without damaging the source code files that are open.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-7617629382853313158?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/7617629382853313158/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/what-you-need.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7617629382853313158'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7617629382853313158'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/what-you-need.html' title='What you need ?'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-8455448220170937185</id><published>2009-10-19T07:02:00.002-07:00</published><updated>2009-10-21T08:28:46.926-07:00</updated><title type='text'>More About C Programming Language</title><content type='html'>Let us think of a scenario. I need to find answer for 2 to the power 5. My algorithm will be something like this. I place 5 in a memory location 1. Place 2 in a memory location 2. Multiply value in memory location 2 with 2, and everytime I multiply, I will decrement the value in memory location 1 until it reaches 0. We don't need to specify where we need to store the values. Those jobs are taken care by the compiler. It reads the programs, separates them into smaller parts called tokens and checks them against predefined syntax. It then converts the program into codes that machines can understand. More like a conversion from Human readable form to Machine Readable form.&lt;br /&gt;&lt;br /&gt;Now, since we will be requiring to find Power of certain numbers many times in a program, we can puts these program into a block called functions. In order to find the power, we just need to call these functions and pass our arguments/inputs (say 2 and 5). Similar to this power function, there will be many other function that are predefined when we install compilers like Turbo C, Borland C etc. These are standards that are set and every compiler for C Programming will follow those standards. &lt;br /&gt;&lt;br /&gt;In order to make it more organised, those Predefined functions called INBUILT functions are grouped and put into a package called library files. So all math related functions will be put into one package. All string related functions will be put under one package etc. When we need to make use of these inbuilt functions, we simply include those files into our program.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-8455448220170937185?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/8455448220170937185/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/more-about-c-programming-language.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/8455448220170937185'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/8455448220170937185'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/more-about-c-programming-language.html' title='More About C Programming Language'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-9209680776581373555</id><published>2009-10-19T07:02:00.001-07:00</published><updated>2009-10-19T07:02:57.361-07:00</updated><title type='text'>What is C Programming Language ?</title><content type='html'>Before one should learn something, they must know what they are learning. Let us say, we have a Computer. In the simplest form a computer consists of a Input device (like our keyboard), a Output device (like our monitor), a CPU (for processing the input we give), a memory device (like RAM, Hard Disk etc for storing the programs and datas we supply). &lt;br /&gt;&lt;br /&gt;Now, how does computer function with all these ? Computer doesn't have intelligence. It simply does what we ask it do through Programs. Every CPU has it own set of operations that it can perform like Addition, Subtraction, Multiplication, Division, comparision etc. We can make use of these operations to occamplish complicated tasks, Say finding the n'th power of a number. For that we need to specify a series of instructions (Program) to the CPU. The data is present here, and the program is present here. The CPU then starts executing the program and sends the output to the output device as we have requested. &lt;br /&gt;&lt;br /&gt;Earlier days, we used to write Machine Codes. Machine codes are codes which corresponds to each basic CPU operation. Say we have a code 2E for Addition. (2E - Hexadecimal number - Just an example). So to add two numbers we need to send something like 2E 34 23 to the CPU. Now the CPU understands that it needs to perform addition operation on 34 and 23. As the requirement for dynamic data increased, we place the data in the memory location and send the instruction like 2E [2000] [2004]. Now the CPU knows that It needs to take data from memory locations (address given to memory) 2000 and 2004 and add them. &lt;br /&gt;&lt;br /&gt;As it was very difficult for us to remember each and every instruction code, we started developing something call Mnemonics. Where we simply say Add 34,23 and there will be a compiler which will understand Add 34,23 and sends the ouput as 2E,34,23 to the CPU. CPU then processes this. This compiler itself is a program written with the instruction codes. Still mnemonics are very hard, because we still need to specify the address where the data should be stored and where the CPU needs to put the output. Also it is not easy to handle loops, control statements etc without actually knowing the instruction sets of CPU.&lt;br /&gt;&lt;br /&gt;The result of all our expectations for faster and efficient programming is C Programming Language. C is a general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. Though there are many languages that were developed prior to C, it is one of the basic languages for understanding many other Programming langugages. Knowing C, we can learn other programming languages very easily.&lt;br /&gt;&lt;br /&gt;I am quite sure that if you understand how C Programming works, you will enjoy programming.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-9209680776581373555?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/9209680776581373555/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/what-is-c-programming-language.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/9209680776581373555'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/9209680776581373555'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/what-is-c-programming-language.html' title='What is C Programming Language ?'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2866512383960676099.post-7547446439352736837</id><published>2009-10-18T10:05:00.000-07:00</published><updated>2009-10-18T10:34:14.154-07:00</updated><title type='text'>About this blog</title><content type='html'>This blog is all about Basics in C Programming. Though there are lots of newer, advanced and much efficient languages developed these days, I believe that Language C is one of the root which one should know to easily understand other programming languages. Similar to HTML, which is very much required for web designing, I believe that one should know C. So I am going to explain to you what I have understood from my graduation and other programming expertise. The contents posted here are from my understandings, and if you see any contradiction from what you have learnt, I will be glad to learn from you. Suggestions, Comments, Feedbacks are all welcome. You can drop a mail to talktotrix[at]ymail[dot]com or can comment here.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2866512383960676099-7547446439352736837?l=c-thebasics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-thebasics.blogspot.com/feeds/7547446439352736837/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/about-this-blog.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7547446439352736837'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2866512383960676099/posts/default/7547446439352736837'/><link rel='alternate' type='text/html' href='http://c-thebasics.blogspot.com/2009/10/about-this-blog.html' title='About this blog'/><author><name>Trix</name><uri>http://www.blogger.com/profile/17864546236713218409</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
