Tokens are the smallest program units in C++. Similar to how punctuation and words form sentences in a language, tokens are the fundamental elements that offer a C++ program structure and meaning. The code is divided into these tokens by a C++ compiler before being subjected to additional analysis.
Table of Contents
Types of Tokens in C++
C++ tokens are broadly classified into six categories:
- Keywords
- Identifiers
- Constants
- Strings
- Operators
- Punctuators
Let’s dive deeper into each of these types.
1. Keywords
In C++, reserved words are those that have particular meanings determined by the compiler. It is not permitted to use these terms as variable names, function names, etc. Every keyword in the language has a distinct function.
Some common keywords:
int
,float
,double
– for defining data typesif
,else
,for
,while
– for control structuresreturn
– to return values from functionsclass
,struct
– for defining custom data structurespublic
,private
,protected
– for access specifiers in object-oriented programming
Example:
int main() {
if (true) {
return 0;
}
}
In this code, int
, if
, and return
are keywords.
2. Identifiers
In order to distinguish variables, functions, classes, arrays, etc. in a program, they are given names called identifiers. The rules that these names must abide by are:
- Must begin with a letter (A-Z, a-z) or an underscore (_).
- Can be followed by letters, digits (0-9), or underscores.
- C++ is case-sensitive, so
Var
andvar
are two different identifiers. - Keywords cannot be used as identifiers.
Example:
int myVariable;
void calculateSum();
In this example, myVariable
and calculateSum
are identifiers.
Learn on Youtube:
C++ Tokens
3. Constants (Literals)
Literals, often known as constants, are values that remain constant while the program runs. Among them are:
- Integer literals:
10
,-42
,1000
- Floating-point literals:
3.14
,0.001
,-2.5
- Character literals:
'a'
,'5'
,'@'
- Boolean literals:
true
,false
Example:
const int PI = 3.14;
Here, 3.14
is a floating-point constant.
4. Strings
Character sequences contained in double quotations (“”) are represented using string literals. Usually, these are used as input prompts or output messages.
Example:
std::cout << "Hello, World!";
In this example, "Hello, World!"
is a string literal.
5. Operators
Operators are symbols that instruct the compiler on particular logical or mathematical processes to be carried out. There are many operators in C++, such as:
- Arithmetic operators:
+
,-
,*
,/
,%
- Relational operators:
==
,!=
,>
,<
,>=
,<=
- Logical operators:
&&
,||
,!
- Assignment operators:
=
,+=
,-=
,*=
,/=
- Bitwise operators:
&
,|
,^
,~
,<<
,>>
- Increment/Decrement operators:
++
,--
Example:
int x = 5;
int y = x + 10; // + is an arithmetic operator
In this example, +
is an arithmetic operator, and =
is an assignment operator.
6. Punctuators
Symbols called punctuators are used to arrange the program’s structure. Several often employed punctuators include:
- Semicolon (
;
): Marks the end of a statement. - Comma (
,
): Separates values in a list. - Parentheses (
()
): Enclose function parameters or control statement conditions. - Braces (
{}
): Enclose blocks of code, like functions or loops. - Brackets (
[]
): Define array indices.
Example:
int main() {
int a, b; // Comma separates two variables
a = 10; // Semicolon ends the statement
if (a > b) { // Parentheses enclose condition
// Block of code inside braces
}
}
Here, ;
, ,
, ()
, {}
, and []
are punctuators.
Conclusion
The cornerstone of a C++ program is tokens. Tokens such as keywords, operators, constants, and identifiers are essential to understanding in order to write error-free, efficient code. You’ll be able to develop cleaner, more reliable C++ programs and understand more difficult topics if you can master these fundamentals.
You can enhance not just your coding abilities but also the general quality, efficiency, and maintainability of your software by closely examining how you use tokens in your programs. Understanding C++ tokens well is the first step towards being a good programmer, and it’s a necessary prerequisite for becoming a successful developer.
Learn More:
C++ Syntax