100% found this document useful (1 vote)
32 views10 pages

Token in CPP

The document provides an overview of tokens in C++ programming, which are the smallest units of a program, including keywords, identifiers, constants, strings, special symbols, and operators. It details the characteristics and rules for each type of token, such as the 95 reserved keywords in C++, the naming conventions for identifiers, and the use of constants and operators. Examples are provided to illustrate the proper usage of these tokens in C++ code.

Uploaded by

technicalvcpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
32 views10 pages

Token in CPP

The document provides an overview of tokens in C++ programming, which are the smallest units of a program, including keywords, identifiers, constants, strings, special symbols, and operators. It details the characteristics and rules for each type of token, such as the 95 reserved keywords in C++, the naming conventions for identifiers, and the use of constants and operators. Examples are provided to illustrate the proper usage of these tokens in C++ code.

Uploaded by

technicalvcpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C++ Programming

Topperworld.in

Token

⚫ A C++ program is composed of tokens which are the smallest individual


unit.
⚫ Tokens can be one of several things, including keywords, identifiers,
constants, operators, or punctuation marks.

❖ 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.

There are 95 keywords in C++ :

alignas (since C++11) alignof (since C++11) and

and and_eq asm

atomic_cancel (TM TS) atomic_commit (TM TS) atomic_noexcept (TM


TS)

auto(1) bitand bitor

bool break case

catch char char16_t (since C++11)

char32_t (since C++11) class(1) compl

concept (since C++20) const constexpr (since C++11)

const_cast continue co_await (coroutines TS)

co_return (coroutines TS) co_yield (coroutines TS) decltype (since C++11)

default(1) delete(1) do

double dynamic_cast else

enum explicit export(1)

extern(1) false float

for friend goto

©Topperworld
C++ Programming

if import (modules TS) inline(1)

int long module (modules TS)

mutable(1) namespace new

noexcept (since C++11) not not_eq

nullptr (since C++11) operator or

or_eq private protected

public register(2) reinterpret_cast

requires (since C++20) return short

signed sizeof(1) static

static_assert (since static_cast struct(1)


C++11)

switch synchronized (TM TS) template

this thread_local (since throw


C++11)

true try typedef

typeid typename union

unsigned using(1) virtual

void volatile wchar_t

while xor xor_eq

©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.

However, some rules must be followed when choosing an identifier:

➢ The first character must be a letter or an underscore.


➢ Identifiers cannot be the same as a keyword.
➢ Identifiers cannot contain any spaces or special characters except for the
underscore.
➢ Identifiers are case-sensitive, meaning that a variable named
"myVariable" is different from a variable named "myvariable".

©Topperworld
C++ Programming

Here are some examples of valid and invalid identifiers in C++:

⚫ Valid:

 my_variable
 student_name
 balance_due

⚫ Invalid:

 my variable (contains a space)


 student# (contains a special character)
 int (same as a keyword)

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

std::cout << "String Token: " << message << std::endl;


return 0;
}

Output:

String Token: Hello, world!

©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.

The pound sign (#) is used to represent the preprocessor directive, a


special type of instruction processed by the preprocessor before the code
is compiled. The percent sign (%) is used as the modulus operator, which
is used to find the remainder when one number is divided by another.
The vertical bar (|) is used to represent the bitwise OR operator, while the
caret (^) is used to represent the bitwise XOR operator. The exclamation
point (!) is used to represent the logical NOT operator, which is used to
negate a Boolean value.

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:

The sum of 5 and 10 is: 15

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.

Arithmetic operators: It is used to perform mathematical operations


such as addition, subtraction, multiplication, and division. Some examples
of arithmetic operators include + (used for addition), - (used for
subtraction), * (used for multiplication), and / (used for division).
Relational operators: It is used to compare two values and determine
their relationship. Some examples of relational operators include == (used
to check for equality), != (used to check for inequality), > (used to check if
a value is greater than another), and < (used to check if a value is less than
another).
Logical operators: It is used to combine two or more relational
expressions and determine their logical relationship. Some examples of
logical operators include && (used for the logical AND operation), ||
(used for the logical OR operation), and ! (used for the logical NOT
operation).

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:

Sum: 8 Diff: 2 Prod: 15 Div: 1 Rem: 2


Is x > y? 1 Is x == y? 0
x > 0 && y > 0? 1 x > 0 || y > 0? 1 !(x > 0)? 0

©Topperworld

You might also like