Monday, 29 April 2019

What are C Tokens? (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 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 essential to the compiler. Tokens in C are the building blocks in the programming language. In the following section, you can read and learn the types of C tokens.

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


A) Constant in C Tokens

  •  Constant whose value is fixed; we cannot change their value throughout the program. 
  • Constant is also known as literals. Constants are of various types,

i) 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 constants are decimal, octal and hexadecimal. 

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.

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.

ii) 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.

iii) Character Constant

  • The single character constant is enclosed in single quotes like 'A,' 'c' or '5'.
          '5' and 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.

iv) String Constant

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

B) 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 

C) Identifiers

  • Identifiers are the variable name for example name of the array, name of string and function name, etc. 
  • It consists of alphanumeric characters (A-Z), underscore, digits. The first character of identifiers can be a letter or underscore ( _ ). You can't use special characters 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;
}

D) Operator

An operator is a symbol which tells the compiler that which type of operation has to be performed. You can learn various types of operators in C
a) Arithmetic operator
b) Logical operator
c) Relational operator
d) Bitwise operator
e) Unary operator
f) Conditional operator
g) Assignment operator

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

E) Special symbols in C Tokens

Tokens in C have some special meaning in the C programming language for the compiler, and it performs some particular function/task in C.

a) Brackets


Below mention brackets will help when, where and how to use brackets in a program.

i) Square brackets [ ]: These brackets help you 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 contains 5 integer data. How many bytes arr [5] occupy is 5*4=20 bytes. 20 bytes are allocating in memory.

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

           } <====   Closing brackets

You can write statement/code/argument 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

 iii) 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<10i++)


b) 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 the compiler that it is the end of the statement and moves to the next statement. 

c) Comma (,)

It separates more than one statement. Don't use it for the termination of the statement like separating parameters in the function call.


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

d) Assignment operator (=)

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

After this statement, the value of k is moved to g with the help of the assignment operator.

e) Asterisk (*)

This symbol is used for multiplication and to create a pointer variable.


1 comment:


  1. I study your post carefully. It is an amazing post. It is very usefull post for all students. your describing style is also better. thanks for help me.
    students can learn programming on programming tutorial

    ReplyDelete