Q1. Explain tokens in C++. Also discuss their role and importance.
Ans. The smallest individual unit in a program is known as a token.
C++ has the following tokens:
- Keywords
- Identifiers
- Literals
- Punctuators
- Operators
- 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.
For example:
break, delete, if, goto, int, char, etc.
- 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 through variables, objects, classes, functions, arrays etc.
For example:
Myfile, MYFILE, _CHGK, DATE_7_73,etc.
Identifier forming rule of C++ states the following:
An identifier is an arbitrarily long sequence of letters and digits. The first character must be a letter; the underscore (_) counts as a letter. Upper and lower-case letters are different. All characters are significant.
- Literals
Literals (often referred to as constants) are data items that never change their value during a program run.
C++ allows three types of literals:
- Integer Constant
- Character Constant
- Floating Constant
- String Literal
For example:
1234, ‘t’, 2.0, “rty”, etc. Read More …