The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

C++ Tokens

Tokens are the smallest building blocks of any programming language. Variables, Data Types, Constants, Operators, Function names are examples of tokens. A combination of these tokens & whitespaces form a C++ statement and a combination of statements form a C++ program. Every statement ends with a semicolon ( ; ) and follows a syntax.

Variables and Data Types
Consider the following C++ statement :

int num = 100 ; // initializing a variable num of data type integer with value 100

Above statement can be divided into tokens ( int, num, =, 100 )
int is a keyword which refers to integer Data type.
Keywords are predefined identifiers ( names of variables, functions, classes etc. ) that are reserved and have special meaning for a language. They cannot be used as variable, class or function names.
Variables are reserved memory locations to store values.
Data Types specify what kind of values a variable can store ( In the above statement, num can store only integer type values since its data type is int ).
Based on the Data Type of a variable, Operating System allocates required memory for that variable.

C++ Data Types can be categorized into 3 classes :-
Built-in Type [ bool, int, char, float, double, void ]
User-defined Type [ enum, structure, union, class ]
Derived Type [ pointer, array, reference ]

Following table lists down the C++ built-in data types and other attributes.

Data TypeKeywordBit widthRange
Characterchar1 Byte-127 to 127
Integerint4 Bytes-2147483648 to 2147483647
Floating Pointfloat4 Bytes3.4e-38 to 3.4e+38
Double Floating Pointdouble8 Bytes1.7e-308 to 1.7e+308
Booleanbool1 ByteTrue (1) or False (0)

void represents the absence of a data type. Uses of void are as follows :-
1) To specifies return type of a function when it is not returning any value.
2) To indicate empty argument list to a function.
3) To declare generic pointers.
* These points will become clear as we move along.

Data Type Modifiers
Modifiers can be used to alter the meaning of some built-in data types in C++.
Following table explains the use of Type modifiers :-

Data Type ModifierUseExplanation
unsignedunsigned int num ;
unsigned num ;
unsigned char c ;
The keyword unsigned puts the constraint that the variable can take only non-negative integers or chars. So, the range of values that int can take changes to 0-4294967295. For char it's 0-255. Similarly the range changes for other data types. A shorthand notation is allowed for integer types ( "unsigned num" can be used instead of "unsigned int num" ).
signedsigned int num ;
signed char c ;
By default, int and char are signed (i.e " signed int " is same as " int "). Variable of type signed can take positive, negative or zero values.
shortshort int num ;
unsigned short int num ;
short int takes 2 bytes of storage. Range of "short int" is -32768 to 32767 & "unsigned short int" is 0-65535.
longlong int num ;
long num ;
unsigned long num ;
long double x ;
A long int is atleast 4 bytes but it can be longer depending on the implementation & system architecture. Similar argument is valid for long double.

The sizes of the variables depending on their data type can be found using sizeof operator.
Following program shows the use of sizeof operator:

 * This program demonstrates the use of sizeof operator
 * to find the memory allocated for different data types.

using namespace std;

int main() {
   int x; // declaring a variable 'x' of data type integer
   char ch; // declaring a variable 'ch' of character data type
   cout << "Size of integer :: "<<sizeof(x) << endl;
   cout << "Size of character :: "<<sizeof(ch) << endl;

   // we can directly use the data type to get it's size
   cout << "Size of float :: "<<sizeof(float) << endl;
   cout << "Size of double :: "<<sizeof(double) << endl;
   cout << "Size of short int :: "<<sizeof(short) << endl;
   cout << "Size of long int :: "<<sizeof(long) << endl;
   return 0;

endl inserts a new-line character. New line character can also be inserted using ' \n ' .
Similarly a tab space can be inserted using ' \t ' .

typedef is either used to create a new name for an existing data type or create a new data type.
Consider the following declaration :-

typedef int number ; // a new data type number is created using existing data type int

Now, int x; can be written as number x;

Enumerated Data Type
The keyword enum is used to create enumerated data type which is a way of referring to numbers using names.
Consider the following example :-

enum car { Maruti, Honda, Tata };
car c = Tata;

We have defined a data type car which can take values in the set { Maruti, Honda, Tata }.
Maruti, Honda & Tata are enumerators in this case.
Next, we declare a variable c of type car and initialized it with value Tata.
By default, value of the first name is 0, second name is 1 & so on.
Thus, in the above example, Maruti has value 0, Honda has value 1 & Tata has value 2. We can override the default values by explicitly assigning values to the enumerators.
For e.g, the following declaration assigns value 7 to Honda :

enum car { Maruti, Honda = 7, Tata } ;

Now, Tata will have value 8 because enumerators have values larger by 1 than their predecessor.
Let's look at a simple program which illustrates the use of enum :

 * This program illustrates the use of enum data type

using namespace std;

int main() {
   // switch can have 2 states ON & OFF
   enum switch_state { OFF, ON };
   switch_state s; // declared a variable 's' of type switch_state
   s = OFF; // 's' initialized to 0 (OFF);
   cout << "Switch State is " << s << endl; // prints switch state as 0
   s = ON;
   cout << "Switch State is " << s << endl; // prints switch state as 1
   return 0;

We will explore other user-defined & derived data types as we proceed further with the tutorial.

Symbolic Constants
Symbolic Constants refer to fixed values that cannot be altered in the program.
If a variable is declared of type const, the value of that variable cannot be modified once defined.
Consider the following declaration :-

const int num = 100 ;

num is a constant & cannot be assigned any other value in the program.
A statement like num = 200; is illegal.
We can also declare symbolic constant using #define preprocessor directive.
Following statement declares a constant NUM and assigns a value 100 to it :

#define NUM 100

' volatile ' Type Qualifier
We use volatile qualifier when a variable can be modified outside the control of the program. For e.g, a variable which is updated by a system clock. The qualifier volatile prevents compiler from doing any kind of optimizations to the code referring to the variable and ensures that every access to the variable is a memory access. In usual case, variables are stored in cache or CPU register for faster access and the updated variable is later stored in memory. In case of volatile , all updations must be done in the memory itself.
An example of volatile variable is shown below :

volatile int num ;

Reference Variables
A reference variable provides an alias for a previously defined variable.
Consider the following example :

int val = 100 ;
int & num = val ;

val is a previously defined variable & num is the reference to the variable val.
Both the variables ( val & num ) refer to the same data object in memory i.e both val & num contains value 100.
The statement
          num = 10;
will change the value of both the variables to 10.
A reference variable must be initialized at the time of declaration & once it is initialized to a data object, it cannot be changed to refer to another object.

Back | Next