x-pert
07-06-2007, 07:46 PM
Please read Lesson 01 (http://www.elakiri.com/forum/showthread.php?t=35788), Lesson 02 (http://www.elakiri.com/forum/showthread.php?t=35876) and Lesson 03 (http://www.elakiri.com/forum/showthread.php?t=36705) before reading this....
From now on.... its hard core programming... :P :P
Lets start....
Elements of a program
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.
1) Data (Variables)
2) Instructions (Code or functions)
Data are the basic building blocks of a program
Instructions tell the computer what to do with the data.
Basic structure of a program
Declaration
Functions
Comments
E.g.
/************************************************** **
* ...Heading comments... *
************************************************** **/
...Data declarations...
int main()
{
...Executable statements...
return (0);
}
Heading comments tell the programmer about the program
Data declaration describe the data that the program gonna use.
Our single function is main. The name main is special coz it is the first function called and if there are any other functions, they will be called within the main function.
Lets review a sample coding line by line:
#include <stdio.h>
int main()
{
/* Tell the world hello */
printf("Hello World\n");
return (0);
}
#include <stdio.h>
This line tells the program to include the standard Input Output C package to the code.
int main()
The main function
{
The beginning of the main function
/* Tell the world hello */
Comment mentioning what the next line will do
printf("Hello World\n");
This line is an executable statement instructing C to print the message "Hello World" on the screen. C uses a semicolon [;] to end a statement 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 standard function printf is used to output our message. A library routine 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.
return (0);
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.
Lets just check what are the mathematical operators in C.
* Multiply
/ Divide
+ Add
- Subtract
% modulus (bedala ithuru wena gaana... 15/2 = 7 ithuru 1 ne... So in this example, modulus=1 :))
We also use parentheses (warahan) to group the terms. And all the mathematical rules apply.
So (1+2) * 4 is not as same as 1+2*4. :)
(1+2) * 4 = 3 * 4 = 12 :yes:
1+2 * 4 = 1 + 8 = 9 :yes: ;)
Theerunaane...??? ;)
ElaKiri.. :D :cool:
Simple example:
#include <stdio.h>
int main()
{
(1 + 2) * 4;
return (0);
}
Can you figure out the error in here....???
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:
The correct coding should be:
#include <stdio.h>
int total;
int main()
{
total = (1 + 2) * 4;
printf (“Total is %d\n”, total);
return (0);
}
Int total;
We declare (create) a variable in computer’s memory called total which can store an integer value.
total = (1 + 2) * 4;
We assign the variable (total) a value ((1+2)*4)
So the answer of the expression will be stored in the variable total.
printf (“Total is %d\n”, total);
Tells the program to print the value of total.
Total is will be printed first. Then %d is a place holder for the value ‘total’.
\n 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.... :) :) :)
From now on.... its hard core programming... :P :P
Lets start....
Elements of a program
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.
1) Data (Variables)
2) Instructions (Code or functions)
Data are the basic building blocks of a program
Instructions tell the computer what to do with the data.
Basic structure of a program
Declaration
Functions
Comments
E.g.
/************************************************** **
* ...Heading comments... *
************************************************** **/
...Data declarations...
int main()
{
...Executable statements...
return (0);
}
Heading comments tell the programmer about the program
Data declaration describe the data that the program gonna use.
Our single function is main. The name main is special coz it is the first function called and if there are any other functions, they will be called within the main function.
Lets review a sample coding line by line:
#include <stdio.h>
int main()
{
/* Tell the world hello */
printf("Hello World\n");
return (0);
}
#include <stdio.h>
This line tells the program to include the standard Input Output C package to the code.
int main()
The main function
{
The beginning of the main function
/* Tell the world hello */
Comment mentioning what the next line will do
printf("Hello World\n");
This line is an executable statement instructing C to print the message "Hello World" on the screen. C uses a semicolon [;] to end a statement 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 standard function printf is used to output our message. A library routine 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.
return (0);
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.
Lets just check what are the mathematical operators in C.
* Multiply
/ Divide
+ Add
- Subtract
% modulus (bedala ithuru wena gaana... 15/2 = 7 ithuru 1 ne... So in this example, modulus=1 :))
We also use parentheses (warahan) to group the terms. And all the mathematical rules apply.
So (1+2) * 4 is not as same as 1+2*4. :)
(1+2) * 4 = 3 * 4 = 12 :yes:
1+2 * 4 = 1 + 8 = 9 :yes: ;)
Theerunaane...??? ;)
ElaKiri.. :D :cool:
Simple example:
#include <stdio.h>
int main()
{
(1 + 2) * 4;
return (0);
}
Can you figure out the error in here....???
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:
The correct coding should be:
#include <stdio.h>
int total;
int main()
{
total = (1 + 2) * 4;
printf (“Total is %d\n”, total);
return (0);
}
Int total;
We declare (create) a variable in computer’s memory called total which can store an integer value.
total = (1 + 2) * 4;
We assign the variable (total) a value ((1+2)*4)
So the answer of the expression will be stored in the variable total.
printf (“Total is %d\n”, total);
Tells the program to print the value of total.
Total is will be printed first. Then %d is a place holder for the value ‘total’.
\n 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.... :) :) :)