<< Chapter < Page Chapter >> Page >

Variable names are also selected according to the rules of identifiers:

  • Identifiers must begin with an uppercase or lowercase ASCII letter or an underscore (_).
  • You can use digits in an identifier, but not as the first character. You are not allowed to use special characters such as $,&, * or %.
  • Reserved words cannot be used for variable names.

Example: Some valid identifiers






Some invalid identifiers are as follows:


We should always give variables meaningful names, from which a reader might be able to make a reasonable guess at their purpose. We may use comments if further clarification is necessary.

Declaration statements

Naming a variable and specifying the data type that can be stored in it is accomplished using declaration statement . A declaration statement in C++ programs has the following syntax:

type name;

The type portion refers to the data type of the variable.

The data type determines the type of information that can be stored in the variable.


int sum;

long datenem;

double secnum;


  1. A variable must be declared before it can be used.
  2. Declaration statements can also be used to store an initial value into declared variables.


int num = 15;

float grade1 = 87.0;

Variable declarations are just the instructions that tell the compiler to allocate memory locations for the variables to be used in a program.

A variable declaration creates a memory location but it is undefined to start with, that means it's empty.



int main()


float price1 = 85.5;

float price2 = 97.0;

float total, average;

total = price1 + price2;

average = total/2.0; // divide the total by 2.0

cout<<"The average price is "<<average<<endl;

return 0;


The output of the above program:

The average price is 91.25

Let notice the two statements in the above program:

total = price1 + price2;

average = total/2.0;

Each of these statements is called an assignment statement because it tells the computer to assign (store) a value into a variable. Assignment statements always have an equal (=) sign and one variable name on the left of this sign. The value on the right of the equal sign is assigned to the variable on the left of the equal sign.

Display a variable’s address

Every variable has three major items associated with it: its data type, its actual value stored in the variable and the address of the variable. The value stored in the variable is referred to as the variable’s contents, while the address of the first memory location used for the variable constitutes its address.

To see the address of a variable, we can use address operator ,&, which means “the address of “. For example,&num means the address of num.



int main()


int a;

a = 22;

cout<<"The value stored in a is "<<a<<endl;

cout<<"The address of a = "<<&a<<endl;

return 0;


The output of the above program:

The value stored in a is 22

The address of a = 0x0065FDF4

The display of addresses is in hexadecimal notation.

Integer quantifiers

Portable languages like C++ must have flexible data type sizes. Different applications might need integers of different sizes. C++ provides long integer , short integer , and unsigned integer data types. These three additional integer data types are obtained by adding the quantifier long , short or unsigned to the normal integer declaration statements.

Questions & Answers

Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?