Token in CPP
Token in CPP
Topperworld.in
Token
❖ Types of Tokens
1. Keyword
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators
1. Keywords :
• In C++, keywords are reserved words that have a specific meaning in the
language and cannot be used as identifiers.
©Topperworld
C++ Programming
• Keywords are an essential part of the C++ language and are used to
perform specific tasks or operations.
default(1) delete(1) do
©Topperworld
C++ Programming
©Topperworld
C++ Programming
If you try to use a reserved keyword as an identifier, the compiler will produce
an error because it will not know what to do with the keyword.
Example:
#include <iostream>
int main() {
int class = 5; // Attempt to use "class" as an identifier
std::cout << class << std::endl;
return 0;
}
Output:
error: expected unqualified
2. Identifiers :
In C++, an identifier is a name given to a variable, function, or another object in
the code.
Identifiers are used to refer to these entities in the program, and they can consist
of letters, digits, and underscores.
©Topperworld
C++ Programming
⚫ Valid:
my_variable
student_name
balance_due
⚫ Invalid:
Example:
#include <iostream>
int main() {
int age = 25; // "age" is a valid identifier
std::cout << "My age is: " << age << std::endl;
return 0;
}
Output:
My age is: 25
©Topperworld
C++ Programming
3. Constants :
• In C++, a constant is a value that cannot be changed during the execution
of the program.
• Constants often represent fixed values used frequently in the code, such
as the value of pi or the maximum size of an array.
Example:
#include <iostream>
int main() {
const int integerConstant = 42;
const float floatConstant = 3.14f;
const char charConstant = 'A';
const bool boolConstant = true;
const double doubleConstant = 2.71828;
std::cout << "Integer Constant: " << integerConstant << std::endl;
std::cout << "Float Constant: " << floatConstant << std::endl;
std::cout << "Character Constant: " << charConstant << std::endl;
std::cout << "Boolean Constant: " << boolConstant << std::endl;
std::cout << "Double Constant: " << doubleConstant << std::endl;
return 0;
}
0utput:
Integer Constant: 42
Float Constant: 3.14
Character Constant: A
Boolean Constant: 1
Double Constant: 2.71828
©Topperworld
C++ Programming
4. String :
• In C++, a string is a sequence of characters that represents text.
• Strings are commonly used in C++ programs to store and manipulate text
data.
To use strings in C++, you must include the <string> header file at the beginning
of your program. This will provide access to the string class, which is used to
create and manipulate strings in C++.
Example:
#include <iostream>
#include <string>
int main() {
std::string message = "Hello, world!"; // String token
Output:
©Topperworld
C++ Programming
5. Special symbols :
In C++, several special symbols are used for various purposes. The ampersand
(&) is used to represent the address of a variable, while the tilde (~) is used to
represent the bitwise NOT operator. Some of the most common special symbols
include the asterisk (*), used as the multiplication and pointer operators.
In addition to these symbols, C++ also has a number of other special characters
that are used for a variety of purposes. For example, the backslash (/) is used to
escape special characters, the double quotes (") are used to enclose string
literals, and the single quotes (') are used to enclose character literals.
Overall, the special symbols in C++ are an important part of the language and
are used in a variety of different contexts to perform a wide range of operations.
Example:
#include <iostream>
int main() {
int a = 5;
int b = 10;
int sum = a + b;
std::cout << "The sum of " << a << " and " << b << " is: " <<
sum << std::endl;
return 0;
}
©Topperworld
C++ Programming
Output:
6. Operators :
In C++, operators are special symbols that are used to perform operations on
one or more operands. An operand is a value on which an operator acts. C++ has
a wide range of operators, including arithmetic operators, relational operators,
logical operators, and others.
Example:
#include <iostream>
int main() {
int x = 5, y = 3;
int sum = x + y;
©Topperworld
C++ Programming
int difference = x - y;
int product = x * y;
int division = x / y;
int remainder = x % y;
bool logicalAnd = (x > 0) && (y > 0);
bool isEqual = x == y;
bool isGreater = x > y;
bool logicalOr = (x > 0) || (y > 0);
bool logicalNot = !(x > 0);
std::cout << "Sum: " << sum << " Diff: " << difference << "
Prod: " << product << " Div: " << division << " Rem: " <<
remainder << std::endl;
std::cout << "Is x > y? " << isGreater << " Is x == y? " <<
isEqual << std::endl;
std::cout << "x > 0 && y > 0? " << logicalAnd << " x > 0 || y >
0? " << logicalOr << " !(x > 0)? " << logicalNot << std::endl;
return 0;
}
Output:
©Topperworld