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
🔒 NordVPN Premium – 3 Months
hrdilshan
Updated:
Yesterday at 8:29 PM
🚀 Microsoft Office 365 Pro Plus – Lifetime Access! 🚀
hrdilshan
Updated:
Yesterday at 8:28 PM
Linkedin Premium Business / Careere /Sales Navigator - 1/2/3/6/9/12 Months - Reddem Link
hrdilshan
Updated:
Yesterday at 8:27 PM
Colombo
YEYE 3 in 1 Instant Coffee Mix 50 Sachet
Romeshka
Updated:
Wednesday at 12:16 AM
Colombo
Red Hat Certified System Administrator (RHCSA) - RHEL 10
Sanjeewani95
Updated:
Jul 3, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Help
Lesson 05: 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: 672974" data-attributes="member: 837"><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Please go through the lessons 1, 2, 3 and 4 before reading this lesson. If you didn’t read them, you might find it hard to follow this. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">OK, in this lesson I will explain about the Variables and storage in C. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">C language allows storing values in variables and each variable is identified by a variable name. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Each variable has a variable type and this type tells the computer about the ‘kind’ of data which the program is gonna store. (real, floating point, integer, character etc.)</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Variable names can start with a character (A – Z) or an underscore ( _ ) and followed by any number of digits, characters or underscores. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>KEEP IN MIND YOU CANNOT DEFINE A VARIABLE STARTING WITH A DIGIT (0 – 9). </strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>C LANGUAGE IS CASE SENSITIVE</strong>. So elakiri, ELAKIRI, ElaKiri, eLaKiRi are four different variables. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>AND YOU CANNOT HAVE SPACES IN VARIABLES. </strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">There is a set of reserved words in any programming language which you <strong>cannot</strong> use to define variables. In C language also, you cannot use variable names like int, float, if, for, while etc. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">To avoid the confusion when defining many variables, stick to a common style. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">I usually use ela_kiri style. That means I use all simple letters and separate the words with an underscore. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">But some people use Camel notation. Its like elaKiri. Every new word starts with a capital letter without spaces. It’s upto you to choose a common way for yourself to define variables. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Always use meaningful names for variables. Avoid using variable names such as x, y, z.. etc. Instead of those names use names like area, height, radius, pi, members_count.... etc. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">General form of defining a variable is:</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">type name; /* comment */</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>type</strong> – Type of the variable (Int, float etc)</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>name</strong> – a meaningful name which describes the variable</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>/*comment*/ </strong>- Comment for the programmer to remember what this variable means and maybe the purpose of it </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>INTEGERS</strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>Integers</strong> are numbers which <strong>doesn’t have a fractional part or decimal point</strong>. Numbers like 234, -666, 40000 are integers but 7.7 is not an integer because it has a decimal point (Dashama sthaana). </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">In unix integer means a 32 bit value to the computer and for windows <strong>PCs integer is a 16 bit value</strong>. So in Unix we can store values in the range of -2147483648 to 2147483647 (2^31 – 1) and <strong>in windows we can save values in the range of -32768 to 32767</strong> (2^15 – 1)</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>Ex:</strong> Following will work in a UNIX machine but will not work on a normal PC</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int total; /*Total for a set of values*/</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">...........................................</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">...........................................</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">total = 52525; </span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>So keep in mind the limitations of the integer data type. </strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>ASSIGNMENT</strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">To use a variable we have to <strong>assign</strong> a value to the variable we created.</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>Equal sign is the assignment operator used in C ( = )</strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">total = (4 + 2) * 3;</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Now in the above coding, we <strong>ASSIGN</strong> a value to the variable called <strong>total</strong>. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">In other words... the value of the expression <span style="font-family: 'Courier New'">(4+2)*3</span> is ASSIGNED to the variable named total </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">The <strong>semicolon (<img src="/styles/default/xenforo/smilies/default/wink.gif" class="smilie" loading="lazy" alt=";)" title="Wink ;)" data-shortname=";)" /> </strong>ends the statement.</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Variable declaration creates a space in the memory to store a value. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p style="text-align: center"><span style="font-family: 'Verdana'"><span style="font-size: 12px"><img src="http://img453.imageshack.us/img453/3711/pic1cfr3.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>Example: </strong> (Taken from Steketee, I. Learning C, Prentice Hall)</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">#include <stdio.h></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int term; /* term used in two expressions */</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">term = 3 * 5;</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">printf("Twice %d is %d\n", term, 2*term);</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">printf("Three times %d is %d\n", term, 3*term);</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>Explanation: </strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>#include <stdio.h></strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">We include the standard input and output header file</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>int term;</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Defines an integer type variable named term</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>int main()</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>{</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Begining of the main function</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>term = 3 * 5;</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">We assign the value of 3*5 to the variable which we defined as term</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>printf("Twice %d is %d\n", term, 2*term);</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Now this is the tricky bit... We ask the computer to print <strong>“Twice” </strong>on the screen and then we have add a place holder as <span style="font-family: 'Courier New'">%d</span> to hold the value of term, then again we ask the computer to print “is” and another place holder as <span style="font-family: 'Courier New'">%d</span> to hold the value of <span style="font-family: 'Courier New'">2*term </span>and then an 'enter' character (Newline) as <span style="font-family: 'Courier New'">\n </span>then we tell the program what are the two place holders hold... That means first <span style="font-family: 'Courier New'">%d </span>holds the value of <strong>term</strong> and the second place holder holds <strong>2*term</strong>. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Didn’t get it..?? Ok Here is a diagram... </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p style="text-align: center"><span style="font-family: 'Verdana'"><span style="font-size: 12px"><img src="http://img453.imageshack.us/img453/9605/pic2cvg0.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>Dont mis-interpret these place holders as variable storages.... These are just places we are showing by <span style="font-family: 'Courier New'"><strong>%d </strong></span>to put the value of the expression</strong></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">So <strong>term</strong> will get <strong>15</strong> as its <strong>value</strong> and </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><strong>2*term </strong>will get <strong>30</strong>.</span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">After executing the above source line it should display <span style="font-family: 'Courier New'"><strong>Twice of 15 is 30</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>printf("Three times %d is %d\n", term, 3*term);</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">This has the same concept as the above line but will multiply the term value by 3 and will display <span style="font-family: 'Courier New'"><strong>Three times 15 is 45.</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Can you see it uses 3*term to get 45. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>return (0);</strong></span>Checks whether the statements executed normally. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"><strong>}</strong></span></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Denotes the end of the program. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">OK now you know how it works... Now try to create some of your own codes and try to execute them. Refer the past lessons to see what are the mathematical operators and implement some of your own codings.... e.g. Find the area of a square and print it on the screen (Wargapalaya), find the area of a triangle.... etc. </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Next lesson will be about floating point numbers and character operations and if possible some array operations... </span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Verdana'"><span style="font-size: 12px">Cheers...!!!</span></span></p></blockquote><p></p>
[QUOTE="x-pert, post: 672974, member: 837"] [FONT="Verdana"][SIZE="3"]Please go through the lessons 1, 2, 3 and 4 before reading this lesson. If you didn’t read them, you might find it hard to follow this. OK, in this lesson I will explain about the Variables and storage in C. C language allows storing values in variables and each variable is identified by a variable name. Each variable has a variable type and this type tells the computer about the ‘kind’ of data which the program is gonna store. (real, floating point, integer, character etc.) Variable names can start with a character (A – Z) or an underscore ( _ ) and followed by any number of digits, characters or underscores. [B]KEEP IN MIND YOU CANNOT DEFINE A VARIABLE STARTING WITH A DIGIT (0 – 9). [/B] [B]C LANGUAGE IS CASE SENSITIVE[/B]. So elakiri, ELAKIRI, ElaKiri, eLaKiRi are four different variables. [B]AND YOU CANNOT HAVE SPACES IN VARIABLES. [/B] There is a set of reserved words in any programming language which you [B]cannot[/B] use to define variables. In C language also, you cannot use variable names like int, float, if, for, while etc. To avoid the confusion when defining many variables, stick to a common style. I usually use ela_kiri style. That means I use all simple letters and separate the words with an underscore. But some people use Camel notation. Its like elaKiri. Every new word starts with a capital letter without spaces. It’s upto you to choose a common way for yourself to define variables. Always use meaningful names for variables. Avoid using variable names such as x, y, z.. etc. Instead of those names use names like area, height, radius, pi, members_count.... etc. General form of defining a variable is: [FONT="Courier New"]type name; /* comment */[/FONT] [B]type[/B] – Type of the variable (Int, float etc) [B]name[/B] – a meaningful name which describes the variable [B]/*comment*/ [/B]- Comment for the programmer to remember what this variable means and maybe the purpose of it [B]INTEGERS[/B] [B]Integers[/B] are numbers which [B]doesn’t have a fractional part or decimal point[/B]. Numbers like 234, -666, 40000 are integers but 7.7 is not an integer because it has a decimal point (Dashama sthaana). In unix integer means a 32 bit value to the computer and for windows [B]PCs integer is a 16 bit value[/B]. So in Unix we can store values in the range of -2147483648 to 2147483647 (2^31 – 1) and [B]in windows we can save values in the range of -32768 to 32767[/B] (2^15 – 1) [B]Ex:[/B] Following will work in a UNIX machine but will not work on a normal PC [FONT="Courier New"]int total; /*Total for a set of values*/ ........................................... ........................................... total = 52525; [/FONT] [B]So keep in mind the limitations of the integer data type. [/B] [B]ASSIGNMENT[/B] To use a variable we have to [B]assign[/B] a value to the variable we created. [B]Equal sign is the assignment operator used in C ( = )[/B] [FONT="Courier New"]total = (4 + 2) * 3;[/FONT] Now in the above coding, we [B]ASSIGN[/B] a value to the variable called [B]total[/B]. In other words... the value of the expression [FONT="Courier New"](4+2)*3[/FONT] is ASSIGNED to the variable named total The [B]semicolon (;) [/B]ends the statement. Variable declaration creates a space in the memory to store a value. [CENTER][IMG]http://img453.imageshack.us/img453/3711/pic1cfr3.jpg[/IMG][/CENTER] [B]Example: [/B] (Taken from Steketee, I. Learning C, Prentice Hall) [FONT="Courier New"]#include <stdio.h> int term; /* term used in two expressions */ int main() { term = 3 * 5; printf("Twice %d is %d\n", term, 2*term); printf("Three times %d is %d\n", term, 3*term); return (0); }[/FONT] [B]Explanation: [/B] [FONT="Courier New"][B]#include <stdio.h>[/B][/FONT] We include the standard input and output header file [FONT="Courier New"][B]int term;[/B][/FONT] Defines an integer type variable named term [FONT="Courier New"][B]int main() {[/B][/FONT] Begining of the main function [FONT="Courier New"][B]term = 3 * 5;[/B][/FONT] We assign the value of 3*5 to the variable which we defined as term [FONT="Courier New"][B]printf("Twice %d is %d\n", term, 2*term);[/B][/FONT] Now this is the tricky bit... We ask the computer to print [B]“Twice” [/B]on the screen and then we have add a place holder as [FONT="Courier New"]%d[/FONT] to hold the value of term, then again we ask the computer to print “is” and another place holder as [FONT="Courier New"]%d[/FONT] to hold the value of [FONT="Courier New"]2*term [/FONT]and then an 'enter' character (Newline) as [FONT="Courier New"]\n [/FONT]then we tell the program what are the two place holders hold... That means first [FONT="Courier New"]%d [/FONT]holds the value of [B]term[/B] and the second place holder holds [B]2*term[/B]. Didn’t get it..?? Ok Here is a diagram... [CENTER][IMG]http://img453.imageshack.us/img453/9605/pic2cvg0.jpg[/IMG][/CENTER] [B]Dont mis-interpret these place holders as variable storages.... These are just places we are showing by [FONT="Courier New"][B]%d [/B][/FONT]to put the value of the expression[/B] So [B]term[/B] will get [B]15[/B] as its [B]value[/B] and [B]2*term [/B]will get [B]30[/B]. After executing the above source line it should display [FONT="Courier New"][B]Twice of 15 is 30[/B][/FONT] [FONT="Courier New"][B]printf("Three times %d is %d\n", term, 3*term);[/B][/FONT] This has the same concept as the above line but will multiply the term value by 3 and will display [FONT="Courier New"][B]Three times 15 is 45.[/B][/FONT] Can you see it uses 3*term to get 45. [FONT="Courier New"][B]return (0);[/B][/FONT]Checks whether the statements executed normally. [FONT="Courier New"][B]}[/B][/FONT] Denotes the end of the program. OK now you know how it works... Now try to create some of your own codes and try to execute them. Refer the past lessons to see what are the mathematical operators and implement some of your own codings.... e.g. Find the area of a square and print it on the screen (Wargapalaya), find the area of a triangle.... etc. Next lesson will be about floating point numbers and character operations and if possible some array operations... Cheers...!!![/SIZE][/FONT] [/QUOTE]
Insert quotes…
Verification
Awruddata maasa keeyada?
Post reply
Top
Bottom