Lesson 05: C Programming

x-pert

Member
Jun 13, 2006
20,952
77
0
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.


KEEP IN MIND YOU CANNOT DEFINE A VARIABLE STARTING WITH A DIGIT (0 – 9).


C LANGUAGE IS CASE SENSITIVE. So elakiri, ELAKIRI, ElaKiri, eLaKiRi are four different variables.


AND YOU CANNOT HAVE SPACES IN VARIABLES.


There is a set of reserved words in any programming language which you cannot 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:
type name; /* comment */


type – Type of the variable (Int, float etc)
name – a meaningful name which describes the variable
/*comment*/ - Comment for the programmer to remember what this variable means and maybe the purpose of it


INTEGERS

Integers are numbers which doesn’t have a fractional part or decimal point. 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 PCs integer is a 16 bit value. So in Unix we can store values in the range of -2147483648 to 2147483647 (2^31 – 1) and in windows we can save values in the range of -32768 to 32767 (2^15 – 1)


Ex: Following will work in a UNIX machine but will not work on a normal PC


int total; /*Total for a set of values*/
...........................................
...........................................
total = 52525;


So keep in mind the limitations of the integer data type.


ASSIGNMENT

To use a variable we have to assign a value to the variable we created.
Equal sign is the assignment operator used in C ( = )


total = (4 + 2) * 3;


Now in the above coding, we ASSIGN a value to the variable called total.
In other words... the value of the expression (4+2)*3 is ASSIGNED to the variable named total
The semicolon (;) ends the statement.


Variable declaration creates a space in the memory to store a value.



pic1cfr3.jpg





Example: (Taken from Steketee, I. Learning C, Prentice Hall)


#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);

}




Explanation:


#include <stdio.h>
We include the standard input and output header file


int term;
Defines an integer type variable named term


int main()
{

Begining of the main function


term = 3 * 5;
We assign the value of 3*5 to the variable which we defined as term


printf("Twice %d is %d\n", term, 2*term);
Now this is the tricky bit... We ask the computer to print “Twice” on the screen and then we have add a place holder as %d to hold the value of term, then again we ask the computer to print “is” and another place holder as %d to hold the value of 2*term and then an 'enter' character (Newline) as \n then we tell the program what are the two place holders hold... That means first %d holds the value of term and the second place holder holds 2*term.
Didn’t get it..?? Ok Here is a diagram...


pic2cvg0.jpg


Dont mis-interpret these place holders as variable storages.... These are just places we are showing by %d to put the value of the expression


So term will get 15 as its value and
2*term will get 30.


After executing the above source line it should display Twice of 15 is 30


printf("Three times %d is %d\n", term, 3*term);
This has the same concept as the above line but will multiply the term value by 3 and will display Three times 15 is 45.


Can you see it uses 3*term to get 45.


return (0);Checks whether the statements executed normally.


}
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...!!!
 
Last edited:

MCLINER

Member
Aug 9, 2006
11,314
9
0
UNDERGROUND
x-pert said:
hehe.. Ubalanam meewa dannawa athi matath wada hodata.. hehe :)

Thanks for the reply machang....
ubata fissuda....I dun knw even a single command


im nt a C programmer ne....Im a student and u r a X-PERT so kohomada

Ubalanam meewa dannawa athi matath wada hodata
wenne


keep it up machan !!! :):):)

u r really helpful and cool person :cool:
 

madurax86

Member
Jun 29, 2006
4,385
88
0
ela keep it up; i wont b able to learn nything othr dan skool stuf for now -- but aftr AL i'll!! for now i sick with my 6 yr old love VB6!!
 

x-pert

Member
Jun 13, 2006
20,952
77
0
ICE said:
ammatasiri.. ela kollek ne ban....

niyamai....siravata.........:yes:

:lol::lol::lol::lol::lol::lol:


ha ha.. ubatath ithin kiyanna thiyanne MCliner ta kiyapu ekama thamai... hehe ;) ;)

Thanks macho..
 

ICE

Active member
  • Mar 26, 2007
    6,152
    1
    38
    41
    x-pert said:
    ha ha.. ubatath ithin kiyanna thiyanne MCliner ta kiyapu ekama thamai... hehe ;) ;)

    Thanks macho..

    mama danna magulak nah ban..

    aurudata 1k program 1kak vath gahanne nah... :lol:

    umba a vageda :lol::lol::lol::lol:
     

    x-pert

    Member
    Jun 13, 2006
    20,952
    77
    0
    madurax86 said:
    ela keep it up; i wont b able to learn nything othr dan skool stuf for now -- but aftr AL i'll!! for now i sick with my 6 yr old love VB6!!


    ha ha.. good good...

    Change your love after a while... or else life gets boring... (Not the real love I mean) ;)
     

    x-pert

    Member
    Jun 13, 2006
    20,952
    77
    0
    Lich said:
    karala iwarai.. he he.. ude free una gaman kala.. he he.

    Ah ehemada... ehenam ElaKiri...

    Machang mata hetin passe msn enna wenne naha sathiyakata dekakata wage...
    So monawa hari probs thiyanawanam PM karanna machang....
     

    x-pert

    Member
    Jun 13, 2006
    20,952
    77
    0
    nAPs2JAAN said:
    i misd 3 4 :( :( :( :( :(
    wait ill copy 3 n 4:)


    Podi search paarak diyan machang... me widihatama thamai mama name kare...

    mata meeke hyperlink karanna amathaka una bung... aayith karanna kammalee wage... hehe :) :) :)