Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
------------------------------------------------------
Computer Programming - Variables
Contact Number: Pak (+92)-321-59-95-634
------------------------------------------------------
Computer Programming - Variables
This session will teach you another most important concept of computer programming which is calledvariables. Actually, variables are the names you give to computer memory locations which are used to store values in a computer program.
For example, assume you want to store two values 10 and 20 in your program and at later stage you want to use these two values. Let's see how you will do it, here are the following three simple steps:
-
Create variables with appropriate names.
-
Store your values in those two variables.
-
Retrieve and use stored values from the variables.
Create variables with appropriate names.
Store your values in those two variables.
Retrieve and use stored values from the variables.
Creating variables
Creating variables is also called declaring variables in C programming language. Different programming languages have different ways of creating variables inside your program. For example, C programming language has the following simple way of creating variables:
#include
main()
{
int a;
int b;
}
Above program creates two variables, i.e., reserves two memory locations with names a and b. We created these variables using int keyword to specify variable data type which means we want to store integer values in these two variables. Similar way, you can create variables to store long, float, char or any other data type. For example:
/* variable to store long value */
long a;
/* variable to store float value */
float b;
You can create variables of similar type by putting them in a single line but separated by comma as follows:
#include
main()
{
int a, b;
}
Following are few important points to remember about variables:
-
A variable name can hold a single type of value. For example, if variable a has been defined inttype, then it can store only integer.
-
C programming language requires a variable creation, i.e., declaration before its usage in your program. You can not use a variable name in your program without creating it, though programming language like Python allows you to use a variable name without creating it.
-
You can use a variable name only once inside your program. For example, if a variable a has been defined to store an integer value, then you can not define a again to store any other type of value.
-
There are programming languages like Python, PHP, Perl, etc., which do not want you to specify data type at the time of creating variables. So you can store integer, float or long without specifying their data type.
-
You can give any name to a variable like age, sex, salary, year1990 or anything else you like to give, but most of the programming languages allow to use only limited characters in their variables names. For now, I will suggest to use only a....z, A....Z, 0....9 in your variable names and start their names using alphabets only instead of digit.
-
Almost none of the programming languages allow to start their variable names with a digit, so1990year will not be a valid variable name where as year1990 or ye1990ar are valid variable names.
Every programming language provides more rules related to variables and you will learn them when you will go in further detail of that programming language. But for now above rules are enough to proceed and let's see next section, which will teach you how to store values in defined variables.
A variable name can hold a single type of value. For example, if variable a has been defined inttype, then it can store only integer.
C programming language requires a variable creation, i.e., declaration before its usage in your program. You can not use a variable name in your program without creating it, though programming language like Python allows you to use a variable name without creating it.
You can use a variable name only once inside your program. For example, if a variable a has been defined to store an integer value, then you can not define a again to store any other type of value.
There are programming languages like Python, PHP, Perl, etc., which do not want you to specify data type at the time of creating variables. So you can store integer, float or long without specifying their data type.
You can give any name to a variable like age, sex, salary, year1990 or anything else you like to give, but most of the programming languages allow to use only limited characters in their variables names. For now, I will suggest to use only a....z, A....Z, 0....9 in your variable names and start their names using alphabets only instead of digit.
Almost none of the programming languages allow to start their variable names with a digit, so1990year will not be a valid variable name where as year1990 or ye1990ar are valid variable names.
0 comments:
Post a Comment