Wednesday 5 March 2014

What is Variables, Data Types, Declaration and Rules for Variables?

Variables, Data Types, Data Types in C++, Declaration and and Rules for Variables


Variables:


A Variables is a term used in computer programming languages that holds the value or information associated to the program. A variable is just like a container that holds a single value at a time. The value of a variable may change during program execution. A real life example of a variable is a glass containing water. In this case the glass is a variable because the value of it may change time to time. Some times it contains water, some time milk and some time it may contain some other liquids. In C++ we can assign values to a Variables by using the assignment operator ( = ). We can declare different type of variables in C++. The type of variable depends upon the nature of the program data. For Example: if the program requires integer data then the variables should be declared as integer.



Data Types:


The type of the value of a Variables is called data type. There are different types of data such as integer data,  real data, charecter or alphabetic data etc. Variables can hold different types of data according to their types declaration, e.g. if a variable is declared as an integer then it will hold only integer values, similarly if a viraible is declared as a character then it will hold only character values etc.


Data Types in C++:


COO provides the following different data types for variable declarations. These data types are availible in all C++ compilers.

Data Type:                  Description

  • int : It is used to declare integer Variables.
  • unsigned int: It is used to declare unsigned ineteger Variables. When a Variable is declared as unsigned then that Variable only can hold a positive value.
  • Short int: It is use to declare short integer Variables.
  • unsigned short int:  It is used to declare unsigned short integer variables.
  • signed short int: This data type works same as short int. 
  • long int:  It is used to declare long integer variables.
  • float:  It is used to declare floating-point variables.
  • double:  It is used to declare double floating-point variables.
  • long double: It is used to declare long double floating-point variables.
  • char:  It is used to declare charecter variable.
  • signed char: This data type works same as char.
  • unsigned char: It is used to declare unsigned char variables.
  • enum: It is used to declare enumeration.

Declaration of Variables:


In C++ as well as in many others programming languages all the variables that a program is going touse must be declared before use. When we write a program then the variables declaration starts first. When a program or a prortion of a program needs variables then they should be declared at the top of the program or preceding that portion of the program needs variables. In C++ programming variables can be declared by using the following syntax:

                 Data-type-name <Variables-list>;
                 For Example
                  Data-type-name <Variable1, Variable2, -------- Variable-n>;

To declare the integer Variables we must write the following code as:

                  int Variable-name;
                    For Example
                           int ;

Here we have declared a variable  of type integer, similarly to declare float and character Variables we must write the following code:

                 float y;
                char ch;

Here we have declared two Variables "y" of type float and "ch" of type character. If you want to declare multiple Variables of same data type then you will write the list of Variables name separated by comma as:

                 int x, y, z;
                 float xl, yl, zl;
                char ch1, ch2, ch3;

Rules for declaring of Variables:

  1. Each variable must have a name.
  2. In a single block or function two Variables don't have the same identical names.
  3. The Variable name always starts with an alphabetic letter or with an underscore and it cannot start from any numeric value for example 2ad, and 34r etc. They are illegal Variable names. After the first letter you can use numeric values and additional underscores.
  4. No special symbols other then an underscore can be allowed in Variable name.
  5. Variable name can be as short as a single character or long as 31 characters. It means that the minimum length of a variable is one character and maximum length of a Variable is 31 characters.
  6. Variable cannot have the same name as C++ commands or library functions.
  7. You should always give meaningful names to variables. For example: If you want to find average of the students then a Variable that holds the avergae must be "avg" or "average". These two names are more descriptive then using the Variable naem "yz123" etc.
  8. Capital letters are also allowed in variable names. If you give the same name for two Variables i.e, for one Variable you give a name in small letters and for second Variable you give the same name in capital letters then C++ compiler does not understand that these two Variables are same.

Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

0 comments:

 
Search Engine Submission - AddMe url submit Casino us org Seo Packages http://blogsiteslist.com