A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows:
- Keywords
- Identifiers
- Constants
- Strings
- Special Symbols
- Operators
- Keyword: Keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. Since keywords are referred names for a compiler, they can’t be used as variable names because by doing so, we are trying to assign a new meaning to the keyword which is not allowed. You cannot redefine keywords. However, you can specify text to be substituted for keywords before compilation by using C/C++ preprocessor directives.C language supports 32 keywords which are given below:
auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
While in C++ there are 31 additional keywords other than C Keywords they are:
asm bool catch class const_cast delete dynamic_cast explicit export false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t
- Identifiers: Identifiers are used as the general terminology for naming of variables, functions and arrays. These are user defined names consisting of arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers; they are reserved for special use. Once declared, you can use the identifier in later program statements to refer to the associated value. A special kind of identifier, called a statement label, can be used in goto statements.
There are certain rules that should be followed while naming c identifiers:
- They must begin with a letter or underscore(_).
- They must consist of only letters, digits, or underscore. No other special character is allowed.
- It should not be a keyword.
- It must not contain white space.
- It should be up to 31 characters long as only first 31 characters are significant.
Some examples of c identifiers:
name Remark _A9 Valid Temp.var Invalid as it contains special character other than the underscore void Invalid as it is a keyword C program:
void main() { int a = 10; }
In the above program there are 2 identifiers:
- main: method name.
- a: variable name.
- Constants: Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined. Constants refer to fixed values. They are also called as literals.
Constants may belong to any of the data type.Syntax:const data_type variable_name; (or) const data_type *variable_name;
Types of Constants:- Integer constants – Example: 0, 1, 1218, 12482
- Real or Floating point constants – Example: 0.0, 1203.03, 30486.184
- Octal & Hexadecimal constants – Example: octal: (013 )8 = (11)10, Hexadecimal: (013)16 = (19)10
- Character constants -Example: ‘a’, ‘A’, ‘z’
- String constants -Example: “GeeksforGeeks”
- Strings: Strings are nothing but an array of characters ended with a null character (‘