Skip to content Skip to navigation

OpenStax_CNX

You are here: Home » Content » Variables and Declaration Statements

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • VOCW

    This module is included inLens: Vietnam OpenCourseWare's Lens
    By: Vietnam OpenCourseWare

    Click the "VOCW" link to see all content affiliated with them.

Recently Viewed

This feature requires Javascript to be enabled.
 

Variables and Declaration Statements

Module by: Dr Duong Tuan Anh. E-mail the author

One of the most important aspects of programming is storing and manipulating the values stored in variables. A variable is simply a name chosen by the programmer that is used to refer to computer storage locations. The term variable is used because the value stored in the variable can change, or vary.

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.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks