Monday, 29 April 2019

What are Tokens in C? (Learn with Video)

A token in C is a character, word or something that a programmer uses to create a program. It's like bricks, cement, sand and other things that will be used to make the structure. Similarly, tokens in C are their own predefined types or functions in the compilation that make syntax for a program.




Whenever we are talking about the English language, punctuation, conjunction and words are tokens in that language.  Tokens in C are the very small unit and it is very important to the compiler. Basically, tokens in C are the building blocks in the programming language.

Program
int main ( )

{
int a=10b=20sum;
sum=a+b;
printf ("sum=%d"sum);
return 0;
}

 Tokens in the C (the above) program are mention below. 

main - identifiers
{ }, ( ) - special symbols
int - keywords
a, b, sum - identifiers
10, 20 - constant
=   assignment Operator
+   addition operator



tokens in C
Types of tokens in C


CATEGORIZATION OF TOKENS IN C

  1. Constant

  •  Constant whose value is fixed, we cannot change their value throughout the program. 
  • Constant is also known as literals.

Types of Constant


1.1 Integer Constant

An integer constant is a whole number, it should have at least one number and this number can be a positive (+) or negative (-) number.

Integer Constant are decimal, octal and hexadecimal. 

An octal integer constant (0): 0 is added before a number like 022 (it means given or mention number is octal). The octal number lies from 0 to 7. For example, 123 is octal value and 9AB is not an octal value.
Hexadecimal integer constant (0x or 0X): 0x or 0X is added before a number like 0x345 or 0X345 (it means given number is hexadecimal) shows that particular number is hexadecimal. Hexadecimal values are lies from 0 to 16 (0,1,2,3,4,5,6,7,8,9, A(10), B(11), C(12), D(13), E(14), F(15). For example, 0xD, 0XA.
A decimal integer constant: Decimal values lies from 0 to 9. These values may be positive or negative. 223 is a decimal number or -34 is also a decimal value.

Note: Space, commas and special characters are not permitted between digits.

1.2 Float Constant

Float Constant are also known as real constant. Decimal values in mathematics are called float constant in the C programming language. For example 17.4456, -89.67, 0.65e4.

1.3 Character Constant

  • The single character constant is enclosed in single quotes like 'A', 'c' or '5'.
          '5' and 5 both are different.
  • printf ("%c", a);
         This statement will print the character 'a'.
  • printf ("%d", a);
          This statement will print the value of character 'a' that is 97.

1.4 String Constant

  • A string is an array of character and always ended with a null character ['\0'].
  • A string constant is enclosed in double quotes. 
  • "Hello World" is a string constant
  • It can contain any special characters, space, numbers etc. like "Welcome 98", "123457654", "123%^$@#".

2. Keywords

  • Keywords are reserved words in the C programming language. We cannot use as variable, constant or string. It has a special/unique meaning for the compiler. 
  • 32 keywords are defined and we can not redefine these keywords in our C programming language. If we are trying to use these 32 keywords as a variable name or in any other manner in the C program then compiler produces an error. 

Tokens in C
Keywords 

3. Identifiers

  • Identifiers consist of alphanumeric characters (A-Z), underscore, digits. Identifiers first character can be a letter or underscore ( _ ). Special characters are not used in c as identifiers. For example, _B6 is a valid identifier, void/main are keywords so, we can't use as an identifier, Var.b  is also not used as an identifier.
  • Identifiers are case sensitive. We cannot use keywords as identifiers.

For example
#include <stdio.h>
int main ( )
{
int RollNo=10;
char Name_of_Student[ ] = "ABCDE";
float marks =12.5;
int arr[5]={1,2,3,4,5};
char row = A';
printf ("%d\n%s\n%f\n%c\n", RollNo, Name_of_Student, marks, row);
//Note: RollNo,  Name_of_Student, marks, arr, row are identifiers.
return 0;
}

4. Operator

An operator is a symbol which tells the compiler that which type of operation has to be performed.

Types of operator

  1.   Arithmetic operator
  2.   Logical operator
  3.   Relational operator
  4.   Bitwise operator
  5.   Unary operator
  6.   Conditional operator
  7.   Assignment operator

For example
output = a + b;
= Assignment operator
+ Addition operator (Arithmetic operator)
a and b are operands

To read the article on OPERATOR IN C

5. Special symbols

They have some special meaning in the C programming language for the compiler and it is used to perform some special function/task in C.

5.1. Brackets

5.1.1 Square brackets [ ]: It is used to define the size of an array, string etc. For example, int arr[5];

The integer data type is of 4 bytes and arr[5] means array arr contain 5 integer data. How many bytes arr [5] occupy is 5*4=20 bytes. 20 bytes are allocating in memory.

5.1.2 Curly brackets { }: These brackets are used to define the scope of the block.
          {   <====  Opening brackets

           } <====   Closing brackets

Statement/code/argument is written in between the opening and closing brackets.

 For example
 #include <stdio.h>
int main( )
 {                    // Opening bracket
 printf ("Welcome to My blog");           // statement
  return 0;
                // Closing bracket

 5.1.3 Round brackets ( ): These brackets are used to defined function like main ( ), for ( ), strcpy ( ), strlen ( ) etc. and function parameters are written inside these brackets. 
For example  for (i=0; i<10; i++)


5.2 A semicolon (;): In the English language, we use a full stop (.) to terminate from the statement/line. Similarly, a semicolon is used to terminate from statements in the C programming language. This symbol tells to the compiler, it is the end of the statement and moves to the next statement. 


5.3 The comma (,): Comma is used to separate more than one statement. It is not used for the termination of the statement. Comma separate more than one statement like separating parameters in the function call.


For example: 
int a=20, b=30, c,i;
add (a,b);


5.4 The assignment operator (=):  This operator is used to transfer the data from one variable to another variable. For example: 
g =17;
The value 17 is transfer to value g.
or 
int k = 20;
g = k;

The value of k i.e. 20 is transferred to g with the help of the assignment operator.

5.5 Asterisk (*):  This symbol is used for multiplication and to create a pointer variable.




No comments:

Post a Comment