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.
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...
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...!!!
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.
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...
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:




