Unit#1 Basic Elements of a C++ Program
Course Outline Return to Unit

Constants and Variables

 


The following numbers represent various constants that can be used in C++ programs:

17
8.3406
-12.01
3.86e+05
7.41e-003

The last two numbers are written in scientific notation. The last number corresponds to 0.00741

If you read your book carefully and if you went over the material presented in the previous subsection you must know by now that  statements like:

 int scoresNumber;

 double width;

 float   a2;

 are called declarations of variables. The names of the variables are ScoresNumber, width, a2, and their types are int (for integer), double, float.

 A declaration of a variable notifies the compiler to set aside space in memory where a value will be stored later.  The name of the variable serves as the address of this memory space, the type of the variable defines the size of the memory and also the storage method.

 Rules about names: You can use letters, digits and the underscore character.  The name cannot start with a digit.  Lower case letters are different from upper case letters ( Examine the ASCII code table in the appendix of your book).

Sample Valid Names
Sample Invalid Names
rowA
row.A
score2
2score
score_2
score-2
USA
PA,12

 

 Data types  :

Type

used for

memory needed

int

Integer quantities ( 1, 6, -5 )

2 bytes or 4 depending on the compiler ( 16 bits or 32)

char

Single character ( a, b, +, ….)

1 byte

float

Real fractional number ( 2.345, -4.5,…)

4 bytes

double

Real fractional number with more precision and higher range.

8 bytes

There are many more types and we suggest that you look at your book for details.  Also read your book and explain what is meant by precision and range.  All data has to be first translated into binary representation and then stored in the form of  a sequence of zeros and ones ( the bits).  Since the number of bits is finite precision and range compete for space.  Try to understand this issue well.

 Another issue you must understand is the representational error and other numeric errors such as arithmetic overflow, arithmetic underflow, and cancellation error.

 
Jacob Y. Kazakia © 2001 All rights reserved