Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
🚀 Google AI PRO – 18 Months | Rs. 850 Only
lkkolla
Updated:
Yesterday at 4:56 PM
🔒 NordVPN Premium – 3 Months
hrdilshan
Updated:
Thursday at 8:29 PM
🚀 Microsoft Office 365 Pro Plus – Lifetime Access! 🚀
hrdilshan
Updated:
Thursday at 8:28 PM
Linkedin Premium Business / Careere /Sales Navigator - 1/2/3/6/9/12 Months - Reddem Link
hrdilshan
Updated:
Thursday at 8:27 PM
Colombo
YEYE 3 in 1 Instant Coffee Mix 50 Sachet
Romeshka
Updated:
Wednesday at 12:16 AM
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Help
Lesson 06: C Programming
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="x-pert" data-source="post: 677252" data-attributes="member: 837"><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Please go through </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><a href="http://www.elakiri.com/forum/showthread.php?t=35788" target="_blank">Lesson 01</a></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><a href="http://www.elakiri.com/forum/showthread.php?t=35876" target="_blank">Lesson 02</a></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><a href="http://www.elakiri.com/forum/showthread.php?t=36705" target="_blank">Lesson 03</a></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><a href="http://www.elakiri.com/forum/showthread.php?t=36710" target="_blank">Lesson 04</a> and </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><a href="http://www.elakiri.com/forum/showthread.php?t=37535" target="_blank">Lesson 05</a> before reading this lesson. Actually this is a continuation of <a href="http://www.elakiri.com/forum/showthread.php?t=37535" target="_blank">Lesson 05</a>. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>Floating point numbers</strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">C language uses a <strong>Decimal point (Dashama thitha)</strong> to distinguish between integers and floating point numbers. Floating point numbers always contain a decimal point. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Numbers such as 5.0, 34.56, 23.5, 122.25 can be considered as floating point numbers. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>KEEP IN MIND</strong> numbers such as 12, 3 and 666 are integers and 12.0, 3.0 and 666.0 are floating point numbers. (If you add a decimal point to the digit, it will become a float value.) </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>A floating point zero should be written as 0.0</strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">And you can store <strong>exponential numbers </strong>in floating point type as well. You have to use <strong>e</strong> to denote an exponential number. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">7.5e27 means 7.5 X 1027</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">The general form of defining a floating point variable is almost the same as defining an inger, but except int key word, you have to use <strong>float</strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">float variable_name; /*comment*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Like integers, floating point numbers also have limitations. We will discuss it in another lesson. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>e.g. find the answer of the expression 1.0 / 3.0 </strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Simply we can do it by</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">#include <stdio.h></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">printf("The answer is %f\n", 1.0/3.0);</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">DID YOU NOTICE WE HAVE USED <span style="font-family: 'Courier New'"><strong>%f</strong></span> as the place holder this time....</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Earlier for Integers we used <span style="font-family: 'Courier New'">%d </span>and for floating point numbers we have to use <span style="font-family: 'Courier New'"><strong>%f</strong></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>Division operator is a special operator ( / ) </strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Remember that there is a difference between integer division and float division. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">In integer division, <strong>the fraction part will be discarded</strong>. So you won’t get the fraction part in the result. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">17/10 in integer division will give you the result as <strong>1 </strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>.7</strong> will get discarded. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>But on the other hand you will get the correct result as 1.7 if you divide float values 17.0 / 10.0</strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">If either of the <strong>Denominator</strong> (haraya) or <strong>numerator</strong> (lawaya) is a float value, you will get the result as a float. So 17.0/10 , 17/10.0 or 17.0/10.0 will give you a correct floating point value. <strong>BUT NOT 17/10</strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">e.g. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">1 + 2 = 3</span> (Integer)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">10/10 = 1</span> (Integer)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">10.0/10 = 1.0 </span>(float)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">23/10 = 2</span> (Int) In here, since both the denominator and numerator are integers, the .3 will be discarded. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">23.0/10.0 = 2.3</span> (float)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">C language allows automatic conversion of integer to floating point numbers:</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">And it is possible to convert floating point numbers to integers. But the fraction part will be discarded. (Part after the dashama thitha will be discarded)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">#include <stdio.h></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">int any_integer; /*integer type variable named integer*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">float any_float; /*floating point type variable named float*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">int main</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">any_float = 2.0 / 4.0; /*this will assign 0.5 to any_float*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">integer = 2/5; /*this will assign 0 to integer variable*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">any_float = (2/5) + (2/5); /*this will assign 0 to any_float....... Why did this happen...?? the correct answer should be 0.8.... But why it is 0.....??? */ </span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">/* You have to figure out why this happens..... even though the any_float variable is a floating point number, we divide two integers (2/5) and not two floating point numbers like 2.0/5.0 </span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">So 2/5 is 0 when consider integer division and 0 + 0 is still 0. </span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">That’s why you got the answer as 0 in the above example code line */</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">any_float = 6.0/4.0 /*assigns 1.5 to the any_float variable*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">integer = any_float </span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">/* now we are asking the program to assign the value of <strong>any_float </strong>to <strong>integer</strong> variable. <strong>any_float has the value 1.5</strong> coz of the line above... (6.0/4.0). <strong>But integer cannot contain a fraction part</strong>. So integer variable will now assigned 1.... just 1... And it has to discard the <strong>.5 </strong> */</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Just for an experiment.... If you like to print each and every result on the screen, just add a printf() line after each expression and see whether what I’m telling is true.. <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /> <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /> <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">If you followed the lesson correctly, this is not a big deal... But still if you need any help just let me know.... <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /> <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><strong>Characters</strong></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Character type variables are denoted by <strong>char</strong>. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">General form of defining a character type variable is </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">char variable_name /*comment*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Characters are enclosed in single quotes like <strong>‘A’.</strong> ‘A’ , ‘a’, ‘!’ are character constants. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Backslash character is called the escape character ( \ )</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\b : Backspace (Moves the cursor to the left by one character)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\t : Tab (moves the cursor to the right by one tab stop and usually there are 8 tab spaces in a standard screen)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\n : new line (Moves the cursor to the beginning of next line)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\f : From feed (moves the cursor to the top of a new page)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\r : return (moves cursor to the beginning of the current line) </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\’ : Apostrophe character ( ‘ )</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\” : Double quote ( “ )</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\\ : Backslash ( \ )</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">\nnn : character number in octal (ASCII)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Characters are enclosed in single quotes (‘a’) and strings are enclosed in double quotes (“string”)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">When using the printf function to print a character on the string. You have to use the %c place holder. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Example (Taken from the source book)</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">#include <stdio.h></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">char char1; /* first character */</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">char char2; /* second character */</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">char char3; /* third character */</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">char1 = 'A'; /* we are assigning A to variable char1*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">char2 = 'B'; /* assigns B to variable char2*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">char3 = 'C'; /*assignes C to variable char3*/</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">printf("%c%c%c reversed is %c%c%c\n",char1, char2, char3,char3, char2, char1);</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">First three <span style="font-family: 'Courier New'">%c</span> place holders will get A B and C and the second set of three <span style="font-family: 'Courier New'">%c</span> place holders will get C B and A accordingly. Hope you can understand this coding. If you didn’t, please see the last lesson where I have used a diagram to show this. </span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">To explain it further...........</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p style="text-align: center"><span style="font-size: 12px"><span style="font-family: 'Verdana'"><img src="http://img367.imageshack.us/img367/6560/pic3cqw0.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Next lesson will be on arrays and capturing inputs from the key board.</span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'"></span></span></p><p><span style="font-size: 12px"><span style="font-family: 'Verdana'">Cheers...!!!</span></span></p></blockquote><p></p>
[QUOTE="x-pert, post: 677252, member: 837"] [SIZE="3"][FONT="Verdana"] Please go through [URL="http://www.elakiri.com/forum/showthread.php?t=35788"]Lesson 01[/URL] [URL="http://www.elakiri.com/forum/showthread.php?t=35876"]Lesson 02[/URL] [URL="http://www.elakiri.com/forum/showthread.php?t=36705"]Lesson 03[/URL] [URL="http://www.elakiri.com/forum/showthread.php?t=36710"]Lesson 04[/URL] and [URL="http://www.elakiri.com/forum/showthread.php?t=37535"]Lesson 05[/URL] before reading this lesson. Actually this is a continuation of [URL="http://www.elakiri.com/forum/showthread.php?t=37535"]Lesson 05[/URL]. [B]Floating point numbers[/B] C language uses a [B]Decimal point (Dashama thitha)[/B] to distinguish between integers and floating point numbers. Floating point numbers always contain a decimal point. Numbers such as 5.0, 34.56, 23.5, 122.25 can be considered as floating point numbers. [B]KEEP IN MIND[/B] numbers such as 12, 3 and 666 are integers and 12.0, 3.0 and 666.0 are floating point numbers. (If you add a decimal point to the digit, it will become a float value.) [B]A floating point zero should be written as 0.0[/B] And you can store [B]exponential numbers [/B]in floating point type as well. You have to use [B]e[/B] to denote an exponential number. [FONT="Courier New"]7.5e27 means 7.5 X 1027[/FONT] The general form of defining a floating point variable is almost the same as defining an inger, but except int key word, you have to use [B]float[/B] [FONT="Courier New"]float variable_name; /*comment*/[/FONT] Like integers, floating point numbers also have limitations. We will discuss it in another lesson. [B]e.g. find the answer of the expression 1.0 / 3.0 [/B] Simply we can do it by [FONT="Courier New"]#include <stdio.h> int main() { printf("The answer is %f\n", 1.0/3.0); return (0); }[/FONT] DID YOU NOTICE WE HAVE USED [FONT="Courier New"][B]%f[/B][/FONT] as the place holder this time.... Earlier for Integers we used [FONT="Courier New"]%d [/FONT]and for floating point numbers we have to use [FONT="Courier New"][B]%f[/B][/FONT] [B]Division operator is a special operator ( / ) [/B] Remember that there is a difference between integer division and float division. In integer division, [B]the fraction part will be discarded[/B]. So you won’t get the fraction part in the result. 17/10 in integer division will give you the result as [B]1 [/B] [B].7[/B] will get discarded. [B]But on the other hand you will get the correct result as 1.7 if you divide float values 17.0 / 10.0[/B] If either of the [B]Denominator[/B] (haraya) or [B]numerator[/B] (lawaya) is a float value, you will get the result as a float. So 17.0/10 , 17/10.0 or 17.0/10.0 will give you a correct floating point value. [B]BUT NOT 17/10[/B] e.g. [FONT="Courier New"]1 + 2 = 3[/FONT] (Integer) [FONT="Courier New"]10/10 = 1[/FONT] (Integer) [FONT="Courier New"]10.0/10 = 1.0 [/FONT](float) [FONT="Courier New"]23/10 = 2[/FONT] (Int) In here, since both the denominator and numerator are integers, the .3 will be discarded. [FONT="Courier New"]23.0/10.0 = 2.3[/FONT] (float) C language allows automatic conversion of integer to floating point numbers: And it is possible to convert floating point numbers to integers. But the fraction part will be discarded. (Part after the dashama thitha will be discarded) [FONT="Courier New"] #include <stdio.h> int any_integer; /*integer type variable named integer*/ float any_float; /*floating point type variable named float*/ int main { any_float = 2.0 / 4.0; /*this will assign 0.5 to any_float*/ integer = 2/5; /*this will assign 0 to integer variable*/ any_float = (2/5) + (2/5); /*this will assign 0 to any_float....... Why did this happen...?? the correct answer should be 0.8.... But why it is 0.....??? */ /* You have to figure out why this happens..... even though the any_float variable is a floating point number, we divide two integers (2/5) and not two floating point numbers like 2.0/5.0 So 2/5 is 0 when consider integer division and 0 + 0 is still 0. That’s why you got the answer as 0 in the above example code line */ any_float = 6.0/4.0 /*assigns 1.5 to the any_float variable*/ integer = any_float /* now we are asking the program to assign the value of [B]any_float [/B]to [B]integer[/B] variable. [B]any_float has the value 1.5[/B] coz of the line above... (6.0/4.0). [B]But integer cannot contain a fraction part[/B]. So integer variable will now assigned 1.... just 1... And it has to discard the [B].5 [/B] */ return (0); }[/FONT] Just for an experiment.... If you like to print each and every result on the screen, just add a printf() line after each expression and see whether what I’m telling is true.. :) :) :) If you followed the lesson correctly, this is not a big deal... But still if you need any help just let me know.... :) :) [B]Characters[/B] Character type variables are denoted by [B]char[/B]. General form of defining a character type variable is [FONT="Courier New"]char variable_name /*comment*/[/FONT] Characters are enclosed in single quotes like [B]‘A’.[/B] ‘A’ , ‘a’, ‘!’ are character constants. Backslash character is called the escape character ( \ ) \b : Backspace (Moves the cursor to the left by one character) \t : Tab (moves the cursor to the right by one tab stop and usually there are 8 tab spaces in a standard screen) \n : new line (Moves the cursor to the beginning of next line) \f : From feed (moves the cursor to the top of a new page) \r : return (moves cursor to the beginning of the current line) \’ : Apostrophe character ( ‘ ) \” : Double quote ( “ ) \\ : Backslash ( \ ) \nnn : character number in octal (ASCII) Characters are enclosed in single quotes (‘a’) and strings are enclosed in double quotes (“string”) When using the printf function to print a character on the string. You have to use the %c place holder. Example (Taken from the source book) [FONT="Courier New"]#include <stdio.h> char char1; /* first character */ char char2; /* second character */ char char3; /* third character */ int main() { char1 = 'A'; /* we are assigning A to variable char1*/ char2 = 'B'; /* assigns B to variable char2*/ char3 = 'C'; /*assignes C to variable char3*/ printf("%c%c%c reversed is %c%c%c\n",char1, char2, char3,char3, char2, char1); return (0); }[/FONT] First three [FONT="Courier New"]%c[/FONT] place holders will get A B and C and the second set of three [FONT="Courier New"]%c[/FONT] place holders will get C B and A accordingly. Hope you can understand this coding. If you didn’t, please see the last lesson where I have used a diagram to show this. To explain it further........... [CENTER][IMG]http://img367.imageshack.us/img367/6560/pic3cqw0.jpg[/IMG][/CENTER] Next lesson will be on arrays and capturing inputs from the key board. Cheers...!!![/FONT][/SIZE] [/QUOTE]
Insert quotes…
Verification
Nawa warak dahaya keeyada? (Namaya wadi kireema dahaya)
Post reply
Top
Bottom