C Token

In C programs, each word and punctuation is referred to as a token. C Tokens are the smallest building block or smallest unit of a C program.

Tokens in C is the most important element to be used in creating a program in C.

We can define the token as the smallest individual element in C. For example, we cannot create a sentence without using words; similarly, we cannot create a program in C without using tokens in C. Therefore, we can say that tokens in C is the building block or the basic component for creating a program in C language

The compiler breaks a program into the smallest possible units and proceeds to the various stages of the compilation, which is called token.C Supports Six Types of Tokens:

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Operators
  6. Special Symbols

Let us begin with Keywords.

Keywords

Keywords are predefined, reserved words in C and each of which is associated with specific features. These words help us to use the functionality of C language. They have special meaning to the compilers.

There are total 32 keywords in C.

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
continueforsignedvoid
doifstaticwhile
defaultgotosizeofvolatile
constfloatshortunsigned

Identifiers

Each program element in C programming is known as an identifier. They are used for naming of variables, functions, array etc. These are user-defined names which consist of alphabets, number, underscore ‘_’. Identifier’s name should not be same or same as keywords. Keywords are not used as identifiers.

Rules for naming C identifiers −

  • It must begin with alphabets or underscore.
  • Only alphabets, numbers, underscore can be used, no other special characters, punctuations are allowed.
  • It must not contain white-space.
  • It should not be a keyword.
  • It should be up to 31 characters long.

Strings

A string is an array of characters ended with a null character(\0). This null character indicates that string has ended. Strings are always enclosed with double quotes(“ “).

Strings are nothing but an array of characters ended with a null character (‘\0’). This null character indicates the end of the string. Strings are always enclosed in double-quotes. Whereas, a character is enclosed in single quotes in C and C++.Declarations for String: 

  • char string[20] = {‘p’,’r’,’o’,’g’,’a’,’m’ ‘\0’};
  • char string[20] = “program”;
  • char string [] = “program”;
  • when we declare char as “string[20]”, 20 bytes of memory space is allocated for holding the string value.
  • When we declare char as “string[]”, memory space will be allocated as per the requirement during the execution of the program.

Let us see how to declare String in C language −

  • char string[20] = {‘s’,’t’,’u’,’d’,’y’, ‘\0’};
  • char string[20] = “demo”;
  • char string [] = “demo”;

Here is an example of tokens in C language,

Example

#include >stdio.h>
int main() {
   // using keyword char
   char a1 = 'H';
   int b = 8;
   float d = 5.6;
   // declaration of string
   char string[200] = "demodotcom";
   if(b<10)
   printf("Character Value : %c\n",a1);
   else
   printf("Float value : %f\n",d);
   printf("String Value : %s\n", string);
   return 0;
}

Output

Character Value : H
String Value : demodotcom

4.Constants: Constants are also like normal variables. But, the 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 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: 
 

  1. Integer constants – Example: 0, 1, 1218, 12482
  2. Real or Floating-point constants – Example: 0.0, 1203.03, 30486.184
  3. Octal & Hexadecimal constants – Example: octal: (013 )8 = (11)10, Hexadecimal: (013)16 = (19)10
  4. Character constants -Example: ‘a’, ‘A’, ‘z’
  5. String constants -Example: “Program”

5.Special Symbols: The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose.[] () {}, ; * = # 
 

  • Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts.
  • Parentheses(): These special symbols are used to indicate function calls and function parameters.
  • Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.
  • Comma (, ): It is used to separate more than one statements like for separating parameters in function calls.
  • Colon(:): It is an operator that essentially invokes something called an initialization list.
  • Semicolon(;): It is known as a statement terminator.  It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon.
  • Asterisk (*): It is used to create a pointer variable.
  • Assignment operator(=): It is used to assign values.
  • Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation.

6.Operators: Operators are symbols that trigger an action when applied to C variables and other objects. The data items on which operators act upon are called operands. 
Depending on the number of operands that an operator can act upon, operators can be classified as follows: 
 

  • Unary Operators: Those operators that require only a single operand to act upon are known as unary operators.For Example increment and decrement operators
  • Binary Operators: Those operators that require two operands to act upon are called binary operators. Binary operators are classified into : 
    1. Arithmetic operators
    2. Relational Operators
    3. Logical Operators
    4. Assignment Operators
    5. Conditional Operators
    6. Bitwise Operator
Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *