In this lesson, you will learn
In C++, tokens are the smallest units of a program that have a specific meaning to the compiler.
Tokens are categorized into several types, each playing a different role in the language. Here’s a detailed explanation of each type of token with examples.

The following are the rules for forming valid identifiers.
Examples: Valid identifiers
Mybooks, DATE_06_12, B1T019, AvgValue, result, $1_23_$a, _value, PRINTRESULT, _PRICE, _MY_JK_2, temp$, isAlphabet
Examples: Invalid identifiers
#include<iostream>
int main() {
int age = 30; // 'age' is an identifier
std::cout << "Age: " << age << std::endl;
return 0;
}
Identifiers: age, main, cout, variableName, etc.
C++ keywords are reserved words that have special meanings in the language. They cannot be used as identifiers (e.g., variable names, function names) because they are fundamental to the syntax and structure of the language.
Here’s a summary table of the C++ keywords:
| Keyword | Description |
|---|---|
alignas | Specifies alignment requirements for variables or data structures. |
alignof | Returns the alignment requirement of a type. |
and | Alternative for the && operator (logical AND). |
and_eq | Alternative for the &= operator (bitwise AND assignment). |
asm | Allows embedding assembly language in C++ code. |
auto | Automatically deduces the type of a variable from its initializer. |
bitand | Alternative for the & operator (bitwise AND). |
bitor | Alternative for the ` |
bool | Represents boolean values (true or false). |
break | Exits from the nearest enclosing loop or switch statement. |
case | Defines a branch in a switch statement. |
catch | Catches exceptions thrown by a try block. |
char | Represents character data. |
char16_t | Represents a 16-bit wide character. |
char32_t | Represents a 32-bit wide character. |
class | Defines a user-defined type (class). |
compl | Alternative for the ~ operator (bitwise NOT). |
const | Specifies that a variable’s value cannot be changed. |
constexpr | Specifies that a value or function can be evaluated at compile time. |
const_cast | Casts away the constness of a variable. |
continue | Skips the rest of the current loop iteration and continues with the next iteration. |
decltype | Determines the type of an expression. |
default | Specifies the default behavior of a function or constructor. |
delete | Deallocates memory previously allocated with new. |
do | Starts a do-while loop. |
double | Represents double-precision floating-point numbers. |
dynamic_cast | Safely casts pointers or references within an inheritance hierarchy. |
else | Specifies the block of code to execute if the if condition is false. |
enum | Defines a set of named integer constants. |
explicit | Specifies that a constructor or conversion operator should not be implicitly invoked. |
export | Allows template definitions to be separated from their declarations (rarely used). |
extern | Declares a variable or function that is defined in another file or translation unit. |
false | Represents the boolean value false. |
float | Represents single-precision floating-point numbers. |
for | Starts a for loop. |
friend | Grants access to private and protected members of a class. |
goto | Transfers control to a labeled statement. |
if | Specifies a conditional block of code. |
inline | Requests the compiler to attempt to inline a function. |
int | Represents integer data. |
long | Represents long integer data. |
mutable | Allows a member of a class to be modified even if the class object is const. |
namespace | Defines a scope that groups related identifiers. |
new | Allocates memory dynamically. |
noexcept | Specifies that a function does not throw exceptions. |
not | Alternative for the ! operator (logical NOT). |
not_eq | Alternative for the != operator (not equal). |
nullptr | Represents a null pointer constant. |
operator | Defines or overloads operators. |
or | Alternative for the ` |
or_eq | Alternative for the ` |
private | Specifies private access control within a class. |
protected | Specifies protected access control within a class. |
public | Specifies public access control within a class. |
register | Suggests to the compiler that a variable should be stored in a register (deprecated in C++11). |
reinterpret_cast | Performs a type cast between unrelated types. |
return | Exits from a function and optionally returns a value. |
short | Represents short integer data. |
signed | Specifies signed integer types. |
sizeof | Returns the size of a type or object. |
static | Specifies static storage duration or linkage. |
static_assert | Performs a compile-time assertion. |
struct | Defines a user-defined type (structure). |
switch | Defines a multi-way branch based on the value of an expression. |
template | Defines a template for functions or classes. |
this | Represents a pointer to the current object. |
throw | Throws an exception. |
true | Represents the boolean value true. |
try | Starts a block of code to handle exceptions. |
typedef | Defines a new name for an existing type. |
typeid | Retrieves type information at runtime. |
typename | Specifies a type parameter in templates. |
union | Defines a user-defined type (union). |
unsigned | Specifies unsigned integer types. |
using | Introduces a name into the current scope or creates type aliases. |
virtual | Indicates that a function can be overridden in a derived class. |
void | Represents the absence of type or a function that does not return a value. |
volatile | Specifies that a variable may be changed by external factors. |
wchar_t | Represents wide character data. |
while | Starts a while loop. |
xor | Alternative for the ^ operator (bitwise XOR). |
xor_eq | Alternative for the ^= operator (bitwise XOR assignment). |
In C++, constants and literals are values that do not change during the execution of a program. They represent fixed data that can be used directly in code.
Here’s a detailed explanation of different types of constants and literals in C++, with examples.

Integer literals represent whole numbers and can be expressed in decimal, octal, or hexadecimal format.
#include<iostream>
int main() {
int decimal = 42; // Decimal integer literal
int octal = 052; // Octal integer literal (base 8, prefixed with 0)
int hexadecimal = 0x2A; // Hexadecimal integer literal (base 16, prefixed with 0x)
std::cout << "Decimal: " << decimal << std::endl;
std::cout << "Octal: " << octal << std::endl;
std::cout << "Hexadecimal: " << hexadecimal << std::endl;
return 0;
}
Decimal: 42
Octal: 42
Hexadecimal: 42
Floating-point literals represent numbers with decimal points and can be expressed in decimal or scientific notation.
#include<iostream>
int main() {
float pi = 3.14159f; // Decimal floating-point literal (float)
double e = 2.718281828459; // Decimal floating-point literal (double)
double scientific = 1.23e4; // Scientific notation (1.23 × 10^4)
std::cout << "Pi: " << pi << std::endl;
std::cout << "E: " << e << std::endl;
std::cout << "Scientific notation: " << scientific << std::endl;
return 0;
}
Pi: 3.14159
E: 2.71828
Scientific notation: 12300
Character literals represent individual characters enclosed in single quotes.
#include<iostream>
int main() {
char letter = 'A'; // Single character literal
char digit = '7'; // Single character literal
char newline = '\n'; // Escape sequence for newline
char tab = '\t'; // Escape sequence for horizontal tab
std::cout << "Letter: " << letter << std::endl;
std::cout << "Digit: " << digit << std::endl;
std::cout << "Newline: " << newline << "Next line" << std::endl;
std::cout << "Tab: " << tab << "Tabbed text" << std::endl;
return 0;
}
Letter: A
Digit: 7
Newline:
Next line
Tab: Tabbed text
String literals represent sequences of characters enclosed in double-quotes.
#include<iostream>
int main() {
const char* greeting = "Hello, World!"; // String literal
const char* multiline = "This is a\nmultiline\nstring"; // String with newline characters
std::cout << "Greeting: " << greeting << std::endl;
std::cout << "Multiline: " << multiline << std::endl;
return 0;
}
Greeting: Hello, World!
Multiline: This is a
multiline
string
Wide character literals represent characters in wide format, enclosed in single quotes with an L prefix.
#include<iostream>
int main() {
wchar_t wideChar = L'汉'; // Wide character literal
wchar_t wideString[] = L"你好"; // Wide string literal
std::wcout << L"Wide character: " << wideChar << std::endl;
std::wcout << L"Wide string: " << wideString << std::endl;
return 0;
}
Wide character: 汉
Wide string: 你好
Boolean literals represent truth values and can be true or false.
#include<iostream>
int main() {
bool isTrue = true; // Boolean literal true
bool isFalse = false; // Boolean literal false
std::cout << "isTrue: " << isTrue << std::endl;
std::cout << "isFalse: " << isFalse << std::endl;
return 0;
}
isTrue: 1
isFalse: 0
Null pointer literals represent a null pointer constant. In modern C++, the nullptr keyword is used.
#include<iostream>
int main() {
int* ptr = nullptr; // Null pointer literal
if (ptr == nullptr) {
std::cout << "Pointer is null." << std::endl;
}
return 0;
}
Pointer is null.
42, 052, 0x2A).3.14159, 1.23e4).'A', '7', '\n')."Hello, World!").L'汉', L"你好").true, false).nullptr).These constants and literals are fundamental in C++ programming, providing fixed values that can be used throughout your code.
In C++, punctuation (also known as delimiters) are special symbols that help define the structure and organization of the code.
They are used to separate and group tokens, mark the end of statements, define blocks of code, and more.
Here’s a summary table of the punctuation (delimiters) used in C++ with examples:
| Punctuation (Delimiter) | Description | Example |
|---|---|---|
; | Terminates statements. | int x = 5; |
, | Separates elements in a list (e.g., function arguments, variable declarations). | int a = 1, b = 2; |
. | Accesses members of an object or class. | obj.member |
() | Defines function calls or group expressions. | foo(); |
{} | Defines a block of code (e.g., function bodies, loops). | if (x > 0) { std::cout << "Positive"; } |
[] | Defines arrays or accesses elements by index. | int arr[10]; arr[0] = 1; |