C++ Tokens / Lexical Units :

C++ Tokens :

In a passage of text, individual words and punctuation marks are called tokens, lexical units or lexical elements. C++ has the following tokens :

Keywords :

Keywords are the words that convey a special meaning to the language compiler. These are reserved for special purpose and must not be used as normal identifier names.

  1. asm
  2. auto
  3. break
  4. case
  5. catch
  6. char
  7. class
  8. const
  9. continue
  10. default
  11. delete
  12. do
  13. double
  14. else
  15. enum
  16. extern
  17. float
  18. for
  19. friend
  20. goto
  21. if
  22. inline
  23. int
  24. long
  25. new
  26. operator
  27. private
  28. protected
  29. public
  30. register
  31. return
  32. short
  33. signed
  34. sizeof
  35. static
  36. struct
  37. switch
  38. temple
  39. this
  40. throw
  41. try
  42. typedef
  43. union
  44. unsigned
  45. virtual
  46. void
  47. volatile
  48. while

Identifiers :

Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program.
The following are some valid identifiers :

  1. Myfile
  3. _CHK
  4. DATE_7_77
  5. _DS
  6. FILE13
  7. Z2T0Z9
  8. _HJI3_JK

The following are some invalid identifiers :

  • DATA-REC (contains special character [ other than A-Z, a-z and _ ] )
  • 29CLCT (Starting with a digit)
  • break (reserved keywords)
  • My.file (contains special character dot (.) )

Literals :

Literals are are data items that never change their value during a program run. C++ allows server kinds of literals :

bool literal

The bool literal in C++ is used to represent one of the two boolean values (true - boolean true or false - boolean false). Internally boolean true has value of 1 and boolean false has value of 0.

integer constant

Integer constants are whole numbers with out any fractional part. The method of writing integer constants has been specified in the following rule :

  • Decimal Integer Constants - An integer constant consisting of a sequence of digits is taken to be decimal integer constant unless it begins with 0.
  • Octal Integer Constants - A sequence of digits starting with 0 is taken to be an octal integer.
  • Hexadecimal Integer Constants - A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer.

character constant

A character constant is one character enclosed in single quotes, as in 'Z' ( Escape Sequences in C++ ) .

floating constant

Real constants are numbers having fractional parts. The following are valid real constants in fractional form :

2.0, 17.5, -15.0, -0.15255

The following are invalid real constants :

7 - No decimal point
7. - No digit after decimal point
+17/6 - '/' Illegal symbol
525.68.01 - Two decimal points
5258,01 - comma not allowed

string literal

'Multiple Character' constants are treated as string literal. A string literal is a sequence of characters surrounded by double quotes ( "abcd" ).

Punctuator :

The following characters are used as punctuator is C++:

Brackets [ ]

Opening and closing brackets indicate single and multidimensional array subscripts.

Parentheses ( )

Opening and closing parentheses indicate function calls and function parameters. Parentheses also group expressions and isolate conditional expressions.

Braces { }

Opening and closing braces indicate the start and end of compound statement (block of code containing more than one executable statements).

Comma ,

It is used as a separator in a function arguments lists.

Semicolon ;

It is used as a statement terminator. Every executable statement is terminated by a semicolon (;).

Colon :

It indicates a labeled statement.

Asterisk *

It is used for pointer declaration and the asterisk is also used as an operator to either pointer or as the multiplication operator.

Ellipsis ...

Ellipsis (...) are used in the formal argument lists of function prototypes to indicate a variable number of argument.

Equal to sing =

It is used for variable initial and as an assignment operator in expressions.

Pound sing #

The pound sign(#) is used for preprocessor directive and double pound sing(##) is also used as operators to perform token replacement and during the preprocessor scanning phase.

Operators :

Operators and Expressions - Operators are tokens that trigger some computation when applied to variables and other objects in an expression. The following list gives a brief description of the operators and their functions :

Unary operators :

Unary operator are those operators that require one operator to operate upon. following are some unary operators :

  • & Address operator
  • * Indirection operator
  • + Unary plus
  • - Unary minus
  • ~ Bitwise complement
  • ++ increment operator
  • -- decrement operator
  • & Address operator
  • ! Logical negation

Binary operators :

Binary operators are those operators that require two operands to operate upon. following are some binary operators :

Arithmetic Operators
%Remainder / Modulus
Shift Operators
<<shift left
>>shift right
Bitwise Operators
&Bitwise AND
^Bitwise exclusive OR (XOR)
|Bitwise OR
Logical Operators
&&Logical AND
||Logical OR
Assignment Operators
/=Assign quotient
+=Assign sum
<<=Assign left shift
&=Assign bitwise AND
*=Assign product
%=Assign remainder
-=Assign difference
>>=Assign right shift
^=Assign bitwise or (XOR)
Relational Operators
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
==Equal to
!=Not equal to
Component selection Operators
Direct component selector
Indirect components selector
Class member Operators
::Scope access/resolution
.*Deference pointer to class member
-->*Deference pointer to class member
Conditional Operators

Escape Sequence in C++ :

Escape SequenceNon-graphic Character
\aAudible bell (alert)
\nNewline or linefeed
\rCarriage Return
\tHorizontal tab
\vVertical tab
\'Single quote
\"Double quote
\?Question mark
\OnOctal number
\xHnHexadecimal number

Next Update


We have a YouTube channel for live game streaming purposes, the channel name is "NightHawk". You can visit our gaming channel and support us, Thank you