0% found this document useful (0 votes)
8 views6 pages

T - 9 - CPP Declarations

The document provides an overview of tokens in C++, including keywords, identifiers, constants, operators, and strings. It details the rules for defining identifiers, the types of constants (numerical, real, character, and string), and introduces various operators used in C++. Additionally, it emphasizes the importance of using constants for fixed values in programming.

Uploaded by

joyalprincess
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
0% found this document useful (0 votes)
8 views6 pages

T - 9 - CPP Declarations

The document provides an overview of tokens in C++, including keywords, identifiers, constants, operators, and strings. It details the rules for defining identifiers, the types of constants (numerical, real, character, and string), and introduces various operators used in C++. Additionally, it emphasizes the importance of using constants for fixed values in programming.

Uploaded by

joyalprincess
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/ 6

TOKENS

• The smallest individual unit in a program is known as “token”.


• Every word in a C++ source code can be considered a token.
• Tokens can be one of several things, including keywords, identifiers, constants,
operators, string and punctuation marks.

C++
Tokens

Keywords

• The keywords are reserved set of words with fixed meanings.


• It cannot be used as identifier (or) variable.
• It is used to perform specific tasks or operations.

There are 95 keywords reserved in C++. Some of the main Keywords are:

break try catch char class const continue


default delete auto else friend for float
long new operator private protected public return
short sizeof static this typedef enum throw
mutable struct case register switch and or
namespace static_cast goto not xor bool do
double int unsigned void virtual union While
Inline template
Identifier(or) variables

• The variables are used to hold data temporarily.


• Identifiers are used to refer to these entities in the program, and they can consist of
letters, digits, and underscores.
• Identifier always starts with an alphabet.
• Identifier does not allow blank spaces , punctuations, signs , etc,.
• Identifiers are user-defined and are generally defined in lower case letters. However
, the upper case letters are also permitted.
• The underscore _ symbol can be used as an identifier.
• Identifiers are names of variables , functions , arrays , etc,.
• In other words identifier refers to variety of entities such as structure ,
union,enumeration,constants , typedef names ,functions and objects.
Variable

Pointer

Structure
IDENTIFIERS
Union

Array

Function
Rules for defining identifiers are follows:

• 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".

Valid Identifiers Invalid Identifiers


_name #name (Cannot start with special character except ‘_’)
Number89 2num (Cannot start with a digit)
first_name first name (Cannot include space)
_last_name_ string (Cannot be same as a keyword)
Constants

• The constants in c++ are applicable to the values that do not change during
execution of a program.
• Constants often represent fixed values used frequently in the code, such as the
value of pi or the maximum size of an array.
• To define a constant in C++, you use the const keyword followed by the data type
and the constant's name and then initialize it with a value.
• The value of a constant cannot be changed once it has been defined.

C++ constants

Numerical constants Character constants Symbolic constants

1. Symbolic constants
• A symbolic constants is defined in the same way as a variable.However , once the
constant is initialized , the assigned value cannot be altered.
• We can define the symbolic constants in C++ in three ways.
1. Using the ‘const’ keyword
2. Using ‘#define’ preprocessor directive.
3. Using “enum” keyword

Defining constant using the “const” keyword in C++

• Here is an example:
const double PI = 3.14159;
• In this example, the constant PI is of type double and is initialized with the value of
pi. You can then use the constant PI in your code wherever you need to use the
value of pi.
• Using constants instead of regular variables is important when you need to
represent fixed values in your code.

Define Constants using the ‘#define’ preprocessor directive in C++

• #define preprocessor directive can be used for defining constant.


• The symbol defined using #define are called macros.
• Here is an example

#define MAX 100

2. Numerical Constants
2.1: Integer constants

• Integer constants are represented with whole numbers.


• It requires minimum two bytes and maximum four bytes of memory.
• The numerical constants are represented with numerical numbers. At least one digit
is needed for representing the numerical number.
• The decimal points or fractional part or any other symbols are not permitted.
Neither even blank spaces nor commas are permitted.
• Integer constant could be either positive or may be negative zero.

Example:

Valid example: 10,20,+30,-15,etc,.


Invalid example: 2.3,.235,$76

2.2: Real constants


• Real constants are often known as floating point constants and can be represented
in exponential or fractional form.
• Integer constants are unfit to represented many quantities.
• Many parameters or quantities are defined not only in integer but it also in real
number.
Example:
Length,height,prize
Example for real constants:
2.5 , 5.521 , 3.14

3. Character constants
3.1: single character constants
• A character constants in c++ represented with a single character and it is enclosed
within single quotes.
• Each character constant has a particular integer value associated it.
• Each character is represented with different ASCII value.
• A character can also be represented with single digit or single special symbol or
white space, all enclosed within a pair of single quote marks.
Example:
‘a’ , ‘8’ , ‘&’

3.2: String Constants


• String constants are sequence of characters enclosed within double quote marks.
• The string maybe may be combination of all types of symbols.
Example: “hello” , “india”,”444”

OPERATORS
• An operator is a symbol that operates on a value to perform specific mathematical or
logical computations
• An operator operates the operands.
For example,
int c = a + b;
• Here, ‘+’ is the addition operator. ‘a’ and ‘b’ are the operands that are being ‘added’.
• The new operators are in C++
o >> ( insertion operator)
o << ( Extraction operator )
o :: ( Scope access (or resolution ) operator)
o ::* ( pointer to member decelerator)
o ->* ( deference pointers to pointers to class members)
o .* ( deference pointers to class members )
o Delete ( memory release operator)
o New ( memory allocation operator )

STRING
• String represented by an array of characters. String constants are enclosed in double
quotes.
• Following are the valid examples of string constants.
Example
“person” , Tamilnadu” , “india”

You might also like