C Tokens explanation with suitable examples

A token is a group of characters that logically belong together. The programmer can write a program by using tokens. Various C tokens used are:

  • Keywords
  • Identifiers
  • Literals
  • Punctuators
  • Operators.

1. Keywords

These are some reserved words in C++ which have predefined meaning to compiler called keywords. We can not use this words as an identifier or as variable names.  Some commonly used Keyword are given below:

auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while

2. Identifiers

Symbolic names can be used in C  for various data items used by a programmer in his program. A symbolic name is generally  known as an identifier. The identifier is a sequence of characters taken from C character set.

Rules for constructing identifier name:

  1. First character should be an alphabet or underscore.
  2. Succeeding characters might be digits or letter.
  3. Punctuations and special characters aren’t allowed except underscore.
  4. Identifiers should not be keywords.

3. Literals

Literals or constants are data items that never change their value during the execution of the program. The following types of literals are available in C++.

  • Integer-Constants
  • Character-constants
  • Floating-constants
  • Strings-constants
  • Integer Constants

Integer constants are whole number without any decimal part. C allows three types of integer constants.

  1. Decimal integer constants : It consists of sequence of digits and should not begin with 0. For example 12,455.
  2. Octal integer constants: It represent the numbers in octal form (0-7).It consists of sequence of digits starting with 0 (zero). For example. 014, 012.
  3. Hexadecimal integer constant: It represents hexadecimal number (0-9,A-F).It consists of sequence of digits preceded by ox or OX.
  •  Character constants

A character constant in C must contain one or more characters and must be enclosed in single quotation marks. For example ‘b’, ‘6’, etc. C allows nongraphic characters which cannot be typed directly from keyboard, e.g., backspace, tab, carriage return etc. These characters can be represented by using an escape sequence. An escape sequence represents a single character. The following table gives a listing of common escape sequences.

Escape Sequence Description
\a Audible alert(bell)
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\” Double quotation mark
\’ Single quotation mark
\? Question mark
\0 Null
  • Floating constants

They are also called real constants. They are numbers having fractional parts. They may be written in fractional form or exponent form. A real constant in fractional form consists of signed or unsigned digits including a decimal point between digits. For example 4.5,-6.77 etc.

  •  String Literals

A sequence of character enclosed within double quotes is called a string literal. String literal is by default (automatically) added with a special character ‘\0′ which denotes the end of the string. Therefore the size of the string is increased by one character. For example “csetips” will re represented as “csetips\0” in the memory and its size is 8 characters.

4. Punctuators

The following characters are used as punctuators in C++.

Brackets [   ] Opening and closing brackets indicate single and multidimensional array subscript.
Parentheses (   ) Opening and closing brackets indicate functions calls,; function parameters for grouping expressions etc.
Braces {   } Opening and closing braces indicate the start and end of a compound statement.
Comma , It is used as a separator in a function argument list.
Semicolon ; It is used as a statement terminator.
Colon : It indicates a labeled statement or conditional operator symbol.
Asterisk * It is used in pointer declaration or as multiplication operator.
Equal sign = It is used as an assignment operator.

5. Operators

Operators are special symbols used for specific purposes. Operators are used with operands to build expressions. We will study about this concept in next article.