<< 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

my_variable

Temperature

x1

x2

_my_variable

Some invalid identifiers are as follows:

%x1%my_var@x2

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.

Example:

int sum;

long datenem;

double secnum;

Note:

  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.

Example:

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.

Example

#include<iostream.h>

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.

Example

#include<iostream.h>

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

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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?

Ask