Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Data Types

C++ Simple Data Types


Video Summary: https://youtu.be/yrtJOOjArUE


A data type is a set of values and a set of operations on these values. In the preceding program, we used the data type int which is an identifier for the integer data type.

We used data type int in four ways in the preceding program. int precedes main, the name of the main function in the program to indicate that the function returns the value of type int. In line 18 an int literal (zero) is returned as the result of the main function. In line 14, a variable number is declared to have the data type int. In line 16, an integer number was read from the keyboard and stored in the variable number.

The following are some fundamental/simple data types:
Integral Types
Type Typical
Byte Size
Typical
Minimum Value
Typical
Maximum Value
short 2 bytes -32,768 32,767
int
long
4 bytes -2,147,483,648 2,147,483,647
long long 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807
char 1 bytes -128 127 *
unsigned short 2 bytes 0 65,535
unsigned int
unsigned long
4 bytes 0 4,294,967,295
unsigned long long 8 bytes 0 18,446,744,073,709,551,615
unsigned char 1 bytes 0 255 *
Floating Types
Type Typical
Byte Size
Typical
Sig. Figs.
Typical
Exponenent range
(powers of 10)
float 4 bytes > 7 [-126,127]
double 8 bytes > 15 [-1022,1023]
long double Depends on CPU
Usually 10
> 16 Depends on CPU

* Note: char values from 0 to 127 represent character symbols from the ASCII table.

Variables and Constants

Variables

Variables are named memory locations that have a type, such as an integer or character, and consequently, a size, which is inherited from their type. Since variables are types, they have a set of operations that can be used to change or manipulate them.

Each variable in your program must be declared and initialized. There are two ways in which we can do this. We can declare our variables first, like this:

  char letter;                                                                  
  int x;                                                                        
  long student_id;                                                              
  float payRate;                                                                
  double pi;                                                                    
  bool valid;          

then initialize them later in the program as a separate statement.

 
  letter = 'A';                                                                 
  x = 7;
  student_id = 200201202;
  payRate = 12.85;
  pi = 3.1415926536;                                                            

  valid = true;     

The other way is to initialize them at the same time as they are declared (in one statement).

 
  char letter = 'A';                                                            
  int x = 7;                                                                    
  long student_id = 200201202;
  double pi = 3.1415926536;
  float payRate = 12.85;                                                        
  bool valid = true;  

Constants

Constants are variables with values that can't be changed throughout the program. Using constants prevents the user or program from accidentally modifying a value that should always remain the same, such as a number indicating an upper limit that must never be crossed.

We can declare our initialized variable as a constant by adding the type qualifier const before the definition. The general format for a const declaration is shown below:
const type variable-name = any value you like

Inside of a program, you will see constants written like this:

 
 const float payRate = 12.85;                      
 const double pi = 3.1415926536; 

Words and Symbols with Special Meanings

Certain words have predefined meanings within the C++ language; these are called reserved words. Examples of reserved words: int, namespace, using, include, cin, cout, and return, etc.. You cannot use them as your user defined identifier such as variable names.