Write Articles A community of people who love to write

The easiest domain name (Note the .ORG) - Absolutely Free!
  

Home | Submit Articles | Login   
 
ALL Categories HEALTH EDUCATION FINANCE TECH WOMEN ENTERTAINMENT TRAVEL
 

Variables In C++ Language: Variable Declaration And Initialization

BY: MANI | Category: Education | Post Date: 2008-08-03
 



•    Read Comments

•    Print This Article



   MANI
Help others find this article:

ADD TO StumbleUpon ADD TO DEL.ICIO.US ADD TO DIGG Share with FACEBOOK
Show All Social Bookmarks


A variable is named memory location whose value can be change at the time of execution. The name of the variable cannot be changed at the time of execution. Basically variables are used to hold the value which is stored on a particular memory location. A variable must always be declared before it can used in a program. There are two main steps involved in variables:

Ø Variable Declaration
Ø Variable Initialization

VARIABLE DECLARATION:

The process in which we specify the datatype and the name of the variable is called variable declaration. If an undeclared variable is used in the program then compiler gives an error and program never executed. The general syntax for declaring a variable is:

datatype var_name;

A semicolon is used as a terminator of declaration. An example is given below to declare a variable:

int a;
float b;
char ch;

In above example the first words int, float and char are the datatypes of the variables and a, b and ch are the names of the variables. More then one variables can also be declared at once like:

int a,b,c,d; // Int is the type and a, b, c and d are variable names
float x,y,z; // Float is the type and x, y and z are variable names
char op,ad,mu,sub; // Char is the type and op, mu, as and sub are variable

Each variable creates a block in memory which is identified by its name. There are certain rules to naming the variables:

NAMING RULES FOR VARIABLES:

· First letter of the name must be in small letter not in capital.
· First letter must be an underscore ( _ ) or an alphabet not a number.
· Spaces must not be given between the variable names.
· For space you can used underscore ( _ ) like e.g var_sum.
· One name can not be assigned to two or more variables.
· You can use special characters between variables name.

VARIABLE INITIALIZATION:

The process of assigning value to variables is called variable initialization. The assignment operator (=) is used to assign value to a variable at design time. The name and datatype is given on the left side of the equal sign and value on the right side. The general syntax is:

datatype var_name=value;
or
var_name=value;

A semicolon is used as a terminator of initialization at design time. An example is given below to initialize a variable:
int a=5; // Initialization with declaration
float b=4.5; // Initialization with declaration
char ch='a'; // Initialization with declaration

or

int a; // Declaration
a=5; // Initialization
float b; // Declaration
b=4.5; // Initialization
char sum; // Declaration
sum='+'; // Initialization


In above, first we assign values to variables at the time of there declaration the we assign values to variables after declaring them. More then one variables can also be initialized at once like:

Int a,b,c; // Declaration
A=5; // Initialization
B=6;
C=10;
float x,y,z; // Declaration
x=3.3; // Initialization
y=4.7;
z=1.2;
char sum,mul,sub; // Declaration
sum='+'; // Initialization
mul='*';
sub='-';

Note:- To assign value to a char variable single quotes (‘a') are used and only one char can be assign to a char variable as its size is I byte if you assign more values (‘ab') then it prints the first one not others.

Note:- You can assign one value to more then one variables using the compound assignment statement like to assign 5 to three variable the statement is:

int a=b=c=5; // Assigning 5 to a, b and c.
float x=y=z=1.2; // Assigning 1.2 to x, y and z.
char ad1=ad2=ad3='+'; // Assigning + to ad1, ad2 and ad3.


Printing sum without third var and literals
#include
#include
void main( ){
clrscr();
int a,b;
a=10;
b=15;
cout<getch();
}


Output: 25



Printing sum with third var and literals
#include
#include
void main( ){
clrscr();
float a,b,sum;
a=b=3.3;
sum=a+b; // a+b assigning to sum
cout<<-Sum=-< getch();
}



Output: Sum=6.6

Article Source: http://www.saching.com



About Author / Additional Info: By: Imran Rashid
www.imranrashid.wetpaint.com

Additional Articles:
* Method of adding songs to your Play Station Portable(PSP)
* A Website, Then You Need To Be Blogging Too!
* The importance of English Language.
* Indian athletes - Why we fail to win medals in Olympic games.
* Reasons why movie stars have a high impact on behavior of children.

Does this article violate or infringe on your copyright ?
It is a violation of our terms for authors to submit content which they did not write and claim it as their own. If this article infringes on your copyrights, then use our Contact us form with the detailed proof of infringement along with the offending article's title, URL and writer name. If you do not hear back from us then contact us again in another 10 days. Thank you.




Comments on this article: (0 comments so far)

* Additional comments are now closed for this article *
Comment Comment By Comment Date



Article Views: 11141

Page copy protected against web site content infringement by Copyscape
Copyright © 2010 saching.com - Do not copy articles from this website.


Important Disclaimer: All articles on this website are for general information only and is not a professional or experts advice. We do not own any responsibility for correctness or authenticity of the information presented in this article, or any loss or injury resulting from it. We do not endorse these articles, we are neither affiliated with the authors of these articles nor responsible for their content. Please see our disclaimer section for complete terms.


| Home | Disclaimer | Xhtml |