Friday, 19 June 2020

Section 2b Tokens in C

C Tokens

C tokens are the basic building blocks in C language that are used to write a C program

Every individual unit in C program is called a token

The following are the 6 types of tokens in C
1) Keywords  : The reserved words is C that cannot be used for variable        
                   (eg:  auto, double, enum )

2) Identifiers    : Names given to constants, variables, structures , functions, array etc

They are used for naming of variables, functions, array etc. These are user-defined names which consist of alphabets, number, underscore ‘_’.  Keywords are not used as identifiers.

3) Constants : These are fixed values used in the program and do not change during the program execution

They can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.

Constants are treated just like regular variables except that their values cannot be modified after their definition.

Types of constant
  1. Integer constants
  2. Floating point constants
  3. Character constants
  4. String constants
  5. Backslash character constants
 How to define constant?

There are two simple ways in C to define constants −
  • Using #define preprocessor.
  • Using const keyword.
Ref: testconst.c, testconst1.c
       
4)  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(“ “).
Let us see how to declare String in C language −
  • char string[20] = {‘H’,’e’,’l’,’l’,’o’, ‘\0’};
  • char string[20] = “Hello”;
  • char string [] = “Hello”;
 Ref: teststring.c

 5) special symbols
    eg : (), {}

6) Operators

     C operators are symbols that are used to perform mathematical or logical     manipulations

  1. Arithmetic Operators - ( +, /, -, *)
  2. Relational Operators - ( > , < , != )
  3. Logical Operators (&&, ||, !)
  4. Assignment Operators ( -, +=)
  5. Increment and Decrement Operators  ( ++, -- )
  6. Conditional Operator (?:)
  7. Bitwise Operators ( <<, >>, &, ^, |)
  8. Special operator
    1. & -- address of the memory location
    2. * pointer to a variable 

Example:


int main()
{
   int x;
   x = 10;
 
   printf ("x = %d \n", total);
}

int - keyword
-  identifier
- operator
main - identifier
(), {}-  special symbols

 

No comments:

Post a Comment