Data Types, Variables, Constants, and Identifiers

(BICS 265 Notes.1)

 

Learn how to maintain data in C++...

 

 
To process data, a program must have a place to store it and an identifier for referencing it.

Like other programming languages, C++ provides a method for declaring data areas used by a program. C++ also supports a variety of data types...

 
 

Standard C++ Data Types

 
char

A single alphabetic character, numeric digit, or punctuation symbol
 
 

Examples

y

7

$

 

int

A whole number (with no decimal places)
 
 

Examples

231

-15

 

float

A real number (with decimal places)
 
 

Examples

7.304

-5.716E25

 

 
While some languages provide a string data type for storing words or phrases (such as a customer name or street address), C++ has no string data type...
 
 

Strings in C++

 
 

Represented by an array of characters

Each letter or digit in the word or phrase occupies an adjacent character location
 
 

Example

An array of characters

 
1 0 9   M A P L E
 
 
While the standard data types are fairly simple, they have many variations.

The variations support different programming requirements on different kinds of computers...

 
 

Common Variations on Standard Data Types

 
 

signed char

char

 

Default for character data

One byte in size

Each character has a decimal value from -128 to 127

Positive values support 7-bit ASCII code (128 characters)
 
 

Examples

y has a decimal value of 121

7 has a decimal value of 55

$ has a decimal value of 36

 

 

unsigned char

 

One byte in size

Each character has a decimal value from 0 to 255

Expands on the ASCII character set (256 characters)

 

 

short int

short

 

Two bytes in size on PCs

Number may be from -32,768 to 32,767

 

 

unsigned short int

unsigned short

 

Two bytes in size on PCs

Number may be from 0 to 65,535

 

 

long int

int

long

 

Default for integer data

Four bytes in size on PCs

Number may be from ­2,147,483,648 to 2,147,483,647

 

 

unsigned long int

unsigned long

unsigned

 

Four bytes in size on PCs

Number may be from 0 to 4,294,967,295

 

 

float

 

Four bytes in size on PCs

Always signed

Number may be from -3.4E38 to 3.4E38

WARNING: Some significant digits may be truncated
 
 

Example

1234567.89 would be stored as 1.23457E6

 

 

double

 


 
 

Eight bytes in size on PCs

Always signed

Number may be from -1.7E308 to 1.7E308

Stores twice as many significant digits as type float

 

"Select the proper data type based on the magnitude of the data to be stored..."

 

 
Every data area your program uses must be declared to the compiler. This is done by specifying its data type and identifier name. It is optional to provide an initial value...
 
 

Declaring Data Areas in C++

 
 

General syntax:           datatype identifiername;


 
 
Where datatype is a valid data type (such as int, float, char, etc...) and identifiername consists of up to 32 alphanumeric characters or underscores with no embedded blanks. The first character of the identifier must not be a numeric digit.

The following are some valid identifier names:

Amount_Due

Total_for_1998

_1998_Sales

CustCreditLimit

 

Sample Data Declarations (uninitialized)

short int Number_of_Employees;

unsigned char KeyStroke;

float BalanceDue;

unsigned long int Really_Big_Number;

char Address[30];

Notes

    When declaring a character array, specify the number of characters within square brackets [ ] after the identifier name.

    The value of an uninitialized data area is generally unpredictable when the program begins execution.

 

Sample Data Declarations (initialized)

          unsigned short int Count = 0;

          char Again = 'Y';

          float TotalSales = 3127.94;

          float TempF = -12.4;

          char CustName[30] = "Jane Doe";
 
 

Notes

    Avoid punctuation (dollar signs and commas) when coding numeric values.

    Enclose character values in single quotes (apostrophes).

    Enclose string values within double quotes.

 
 
It is possible to declare multiple data areas of the same type in a single statement...
 
 

Declaring Multiple Data Areas of the Same Type

 
 

List identifiers after data type

Individual data areas may be initialized

 
 

Examples

     float Length, Width;

     int X = 3, Y = -5 , Z;

     char TransCode = '1',

          KeyValue;

 

"Because the compiler ignores spaces, tabs, and returns (whitespace characters), you can continue a C++ statement on the next line (after indenting for clarity)."

 

 
Not all data changes during program execution. For example, if a program calculates the area of several circles based on their radius (PI times the square of the radius), the value of PI would never change.

Unlike most programming languages, C++ differentiates between constants and variables...

 
 

Declaring Constants in C++

 
 

Code const before data type

Compiler will flag statements that attempt to modify the value of the data area

 
 

Examples

     const char Yes = 'Y';

     const float PI = 3.14156;

 

"Data areas that are not constant are automatically variable..."

 

Sample Program
 
 

Programming Exercise