What are C Tokens? (Learn with Video)

C Tokens

C tokens are the building blocks in the C programming language.

Compiler breaks a C program into small units and proceeds for compilation. This small unit is known as tokens.

A token 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, C tokens are their predefined types or functions in the compilation that make syntax for a program.

In English text, punctuation, conjunction, and words are known as tokens. Similarly, a C program is made up of variables, identifiers, keywords, operators etc.

What are C Tokens in a Program?

#include <stdio.h>
int main()
int a = 10, b = 20, sum = 0; //Initialization of variables or identifiers
sum = a+b;
printf ("sum=%d\n",sum);
return 0;

Token in the above C program:
Identifiers: main, a,b,sum
Keywords: int
Constant: 10,20,0
Operators: +, =
Special symbols: ; , ( ), "", {},

Types of Tokens in C

C Tokens are classified as:

C Tokens

A) Constant in C Tokens

  •  Constant whose value is fixed; we cannot change their value throughout the program or the execution of 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.

Escape Characters
It is a sequence of characters. When you are using it with strings then it doesn't represent itself.
256 characters in the character set of the C programming language. The character set is divided into two parts: ASCII Character set and Extended Character set. But some characters are not the part of character set known as escape characters (C Tokens).

Escape Character
Alarm or Beep
Form Feed
New Line
Carriage Return
Vertical Tab
Single quote
Double quote
Question mark
Octal number
Hexadecimal number

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 (C Tokens)

  • 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. 


















C) Identifiers

  • It is another type of C tokens. 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. Some symbols are shown below:

Question Mark
Double Quotation Mark
Exclamation Mark
Vertical Bar
Forward Slash
Backward Slash
Dollar Sign
Percentage Sign
Opening Curly Bracket
Closing Curly Bracket
Left Bracket
Right Bracket
Opening Left Parenthesis
Closing Right Parenthesis
Greater than or Closing Angle Bracket
Less than or Opening Angle Bracket
Hash Sign

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

What are C Tokens? (Learn with Video) What are C Tokens? (Learn with Video) Reviewed by Glory on 12:01 Rating: 5

No comments:

Powered by Blogger.