C++ Simple Data Types

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 main. In line 14, a variable number is declared to have the data type int. In line 16, a integer number was read from the keyboard and stored in the storage of variable number.

The following are some fundamental/simple data types:

	Integral Types
short int long char
unsigned short unsigned unsigned long unsigned char
Floating Types
float double long double

Variables and Constants


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;                                                            

However, a variable can be used only after its value is set.


We can also declare our initialized variable as a constant, by adding the type qualifier const before the defintion. 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 cannnot use them as your user defined identifier such as variable names.

Copyright: Department of Computer Science, University of Regina.