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 04: 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: 657714" data-attributes="member: 837"><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Please read <a href="http://www.elakiri.com/forum/showthread.php?t=35788" target="_blank">Lesson 01</a>, <a href="http://www.elakiri.com/forum/showthread.php?t=35876" target="_blank">Lesson 02</a> and <a href="http://www.elakiri.com/forum/showthread.php?t=36705" target="_blank">Lesson 03</a> before reading this.... </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">From now on.... its hard core programming... <img src="/styles/default/xenforo/smilies/default/P.gif" class="smilie" loading="lazy" alt=":P" title=":P :P" data-shortname=":P" /> <img src="/styles/default/xenforo/smilies/default/P.gif" class="smilie" loading="lazy" alt=":P" title=":P :P" data-shortname=":P" /></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Lets start.... </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Elements of a program</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">When you are constructing a building, you need basically two things. Bricks and a blue print which tells you how to put those bricks together. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Likewise in programming also you need 2 things. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>1) Data (Variables)</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>2) Instructions (Code or functions)</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Data</strong> are the basic building blocks of a program</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Instructions</strong> tell the computer what to do with the data. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Basic structure of a program</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Declaration</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Functions</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Comments</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">E.g. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">/****************************************************</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">* ...Heading comments... *</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">****************************************************/</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">...Data declarations...</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">...Executable statements...</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Heading comments</strong> tell the programmer about the program</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Data declaration</strong> describe the data that the program gonna use. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Our single <strong>function</strong> is <span style="font-family: 'Courier New'">main</span>. The name <span style="font-family: 'Courier New'">main</span> is special coz it is the first function called and if there are any other functions, they will be called <strong>within the main function</strong>. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Lets review a sample coding line by line:</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">#include <stdio.h></span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">/* Tell the world hello */</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">printf("Hello World\n");</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">#include <stdio.h> </span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">This line tells the program to include the standard Input Output C package to the code. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">The main function</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">The beginning of the main function</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">/* Tell the world hello */</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Comment mentioning what the next line will do</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">printf("Hello World\n");</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">This line is an <strong>executable statement </strong>instructing C to print the message "Hello World" on the screen. <strong>C uses a semicolon [<strong>;</strong>] to end a statement </strong>in much the same way we use a period (.) to end a sentence. Unlike line-oriented languages such as BASIC, an end-of-line does not end a statement. So keep in mind about this semi colon ; <img src="/styles/default/xenforo/smilies/default/yes.gif" class="smilie" loading="lazy" alt=":yes:" title="Yes :yes:" data-shortname=":yes:" /></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">The <strong>standard function</strong> <span style="font-family: 'Courier New'">printf</span> is used to output our message. <strong>A library routine </strong>is a C procedure or function that has been written and put into a library or collection of useful functions. Such routines perform sorting, input, output, mathematical functions, and file manipulation. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">This line is used by the Operating system to check whether the program exited normally (status=0) if the value is non-zero.... then there is an error. We will get to know these things soon... </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Ok.. This simple hello world program is the simplest program. It contains no computations and it just sends a single signal to the screen... It is a starting point indeed. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Lets just check what are the mathematical operators in C. </strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>* Multiply</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>/ Divide</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>+ Add</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>- Subtract</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>% modulus</strong> (bedala ithuru wena gaana... 15/2 = 7 ithuru 1 ne... So in this example, modulus=1 <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-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">We also use <strong>parentheses</strong> (warahan) to group the terms. And all the mathematical rules apply. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">So (1+2) * 4 is <strong>not as same as </strong>1+2*4. <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-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">(1+2) * 4 = 3 * 4 = 12 <img src="/styles/default/xenforo/smilies/default/yes.gif" class="smilie" loading="lazy" alt=":yes:" title="Yes :yes:" data-shortname=":yes:" /> </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">1+2 * 4 = 1 + 8 = 9 <img src="/styles/default/xenforo/smilies/default/yes.gif" class="smilie" loading="lazy" alt=":yes:" title="Yes :yes:" data-shortname=":yes:" /> <img src="/styles/default/xenforo/smilies/default/wink.gif" class="smilie" loading="lazy" alt=";)" title="Wink ;)" data-shortname=";)" /> </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Theerunaane...??? <img src="/styles/default/xenforo/smilies/default/wink.gif" class="smilie" loading="lazy" alt=";)" title="Wink ;)" data-shortname=";)" /> </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">ElaKiri.. <img src="/styles/default/xenforo/smilies/default/D.gif" class="smilie" loading="lazy" alt=":D" title="Big grin :D" data-shortname=":D" /> <img src="/styles/default/xenforo/smilies/default/cool.gif" class="smilie" loading="lazy" alt=":cool:" title="Cool :cool:" data-shortname=":cool:" /> </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p> <span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Simple example: </strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">#include <stdio.h> </span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">(1 + 2) * 4;</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Can you figure out the error in here....???</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">This will give you a NULL effect warning... Program is well written but utterly useless like our wizkido’s fu** talks....</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Now in here we calculate the answer but we don’t tell the computer program to show or display the answer.... How can we do that... <img src="/styles/default/xenforo/smilies/default/confused.gif" class="smilie" loading="lazy" alt=":confused:" title="Confused :confused:" data-shortname=":confused:" /></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>The correct coding should be:</strong></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">#include <stdio.h> </span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int total;</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'"></span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">int main()</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">{</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">total = (1 + 2) * 4;</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">printf (“Total is %d\n”, total);</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">return (0);</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">}</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">Int total;</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">We declare (create) a variable in computer’s memory called total which can store an integer value. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">total = (1 + 2) * 4;</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">We assign the <strong>variable</strong> (total) a <strong>value</strong> ((1+2)*4)</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">So the answer of the expression will be stored in the variable <strong>total</strong>. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><span style="font-family: 'Courier New'">printf (“Total is %d\n”, total);</span></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Tells the program to print the value of total. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>Total is</strong> will be printed first. Then <strong>%d </strong>is a place holder for the value <strong>‘total’</strong>. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"><strong>\n</strong> tells the program to move the cursor one row. (Same as pressing the enter key)</span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Dont worry if you did not get a clear idea of this coding now.. Next time I will go through the concepts of this coding thoroughly.. </span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px"></span></span></p><p><span style="font-family: 'Arial'"><span style="font-size: 12px">Cheers.... <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></blockquote><p></p>
[QUOTE="x-pert, post: 657714, member: 837"] [FONT="Arial"][SIZE="3"] Please read [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] and [URL="http://www.elakiri.com/forum/showthread.php?t=36705"]Lesson 03[/URL] before reading this.... From now on.... its hard core programming... :P :P Lets start.... [B]Elements of a program[/B] When you are constructing a building, you need basically two things. Bricks and a blue print which tells you how to put those bricks together. Likewise in programming also you need 2 things. [B]1) Data (Variables) 2) Instructions (Code or functions)[/B] [B]Data[/B] are the basic building blocks of a program [B]Instructions[/B] tell the computer what to do with the data. [B]Basic structure of a program[/B] Declaration Functions Comments E.g. [FONT="Courier New"]/**************************************************** * ...Heading comments... * ****************************************************/ ...Data declarations... int main() { ...Executable statements... return (0); }[/FONT] [B]Heading comments[/B] tell the programmer about the program [B]Data declaration[/B] describe the data that the program gonna use. Our single [B]function[/B] is [FONT="Courier New"]main[/FONT]. The name [FONT="Courier New"]main[/FONT] is special coz it is the first function called and if there are any other functions, they will be called [B]within the main function[/B]. [B]Lets review a sample coding line by line:[/B] [FONT="Courier New"]#include <stdio.h> int main() { /* Tell the world hello */ printf("Hello World\n"); return (0); }[/FONT] [FONT="Courier New"]#include <stdio.h> [/FONT] This line tells the program to include the standard Input Output C package to the code. [FONT="Courier New"]int main()[/FONT] The main function [FONT="Courier New"]{[/FONT] The beginning of the main function [FONT="Courier New"]/* Tell the world hello */[/FONT] Comment mentioning what the next line will do [FONT="Courier New"]printf("Hello World\n");[/FONT] This line is an [B]executable statement [/B]instructing C to print the message "Hello World" on the screen. [B]C uses a semicolon [[B];[/B]] to end a statement [/B]in much the same way we use a period (.) to end a sentence. Unlike line-oriented languages such as BASIC, an end-of-line does not end a statement. So keep in mind about this semi colon ; :yes: The [B]standard function[/B] [FONT="Courier New"]printf[/FONT] is used to output our message. [B]A library routine [/B]is a C procedure or function that has been written and put into a library or collection of useful functions. Such routines perform sorting, input, output, mathematical functions, and file manipulation. [FONT="Courier New"]return (0);[/FONT] This line is used by the Operating system to check whether the program exited normally (status=0) if the value is non-zero.... then there is an error. We will get to know these things soon... Ok.. This simple hello world program is the simplest program. It contains no computations and it just sends a single signal to the screen... It is a starting point indeed. [B]Lets just check what are the mathematical operators in C. [/B] [B]* Multiply / Divide + Add - Subtract % modulus[/B] (bedala ithuru wena gaana... 15/2 = 7 ithuru 1 ne... So in this example, modulus=1 :)) We also use [B]parentheses[/B] (warahan) to group the terms. And all the mathematical rules apply. So (1+2) * 4 is [B]not as same as [/B]1+2*4. :) (1+2) * 4 = 3 * 4 = 12 :yes: 1+2 * 4 = 1 + 8 = 9 :yes: ;) Theerunaane...??? ;) ElaKiri.. :D :cool: [B]Simple example: [/B] [FONT="Courier New"]#include <stdio.h> int main() { (1 + 2) * 4; return (0); }[/FONT] [B]Can you figure out the error in here....???[/B] This will give you a NULL effect warning... Program is well written but utterly useless like our wizkido’s fu** talks.... Now in here we calculate the answer but we don’t tell the computer program to show or display the answer.... How can we do that... :confused: [B]The correct coding should be:[/B] [FONT="Courier New"]#include <stdio.h> int total; int main() { total = (1 + 2) * 4; printf (“Total is %d\n”, total); return (0); }[/FONT] [FONT="Courier New"]Int total;[/FONT] We declare (create) a variable in computer’s memory called total which can store an integer value. [FONT="Courier New"]total = (1 + 2) * 4;[/FONT] We assign the [B]variable[/B] (total) a [B]value[/B] ((1+2)*4) So the answer of the expression will be stored in the variable [B]total[/B]. [FONT="Courier New"]printf (“Total is %d\n”, total);[/FONT] Tells the program to print the value of total. [B]Total is[/B] will be printed first. Then [B]%d [/B]is a place holder for the value [B]‘total’[/B]. [B]\n[/B] tells the program to move the cursor one row. (Same as pressing the enter key) Dont worry if you did not get a clear idea of this coding now.. Next time I will go through the concepts of this coding thoroughly.. Cheers.... :) :) :) [/SIZE][/FONT] [/QUOTE]
Insert quotes…
Verification
Winadiyakata thappara keeyak tibeda?
Post reply
Top
Bottom