Tokens and Keywords in C

Tokens are one of the most valuable and useful features of C. You can say that we can’t make sentences without words and in C, we can’t develop programs without tokens. It is like the building block of the C programming language.

What are Tokens in C?

Mainly considered as the smallest unit in C programs. It is one of the most conceptual features in C. Without the tokens, you can not develop a program. Compiler breaks the program into small units called tokens and further goes to the stages of compilation.

Character Set in C

The C programming language has its own character set. Character set is a combination of alphabets, letters and some special characters. It is divided into:-

1. Letters

2. Numbers

3. Special Characters

4. White Spaces

1. Alphabets:-

C programming language accepts uppercase and lowercase alphabets.

a. Uppercase characters (A-Z)

b. Lowercase characters (a-z)

2. Numbers:-

C programming language accepts all the digits from 0 to 9.

3. Special Characters:-

, (comma) { (opening curly bracket)
. (period) } (closing curly bracket)
; (semi-colon) [ (left bracket)
: (colon) ] (right bracket)
? (question mark) ( (opening left parenthesis)
‘ (apostrophe) ) (closing right parenthesis)
” (double quotation mark) & (ampersand)
! (exclamation mark) ^ (caret)
|(vertical bar) + (addition)
/ (forward slash) – (subtraction)
\ (backward slash) * (multiplication)
~ (tilde) / (division)
_ (underscore) > (greater than or closing angle bracket)
$ (dollar sign) < (less than or opening angle bracket)
% (percentage sign) # (hash sign)

4. White Spaces:-

a. Blank space

b. New line

c. Carriage return

d. Horizontal tab

Types of Tokens in C

In C programming language, tokens can be further classified into:-

1. Keywords in C:-

In C, keywords are predefined or reserved words which have their own importance. In total, there are 32 keywords in C. It has mixed meaning and it can not be altered or changed. Each keyword has its own functionality.

Below is the table of keywords in C:-

auto double int struct
break else long switch
case enum register typedef
char extern return union
const short float unsigned
continue for signed void
default goto sizeof volatile
do if static while

Apart from these 32 keywords, there are some words which can not be used as a variable. Following is the list of them.

1. asm

2. bool

3. catch

4. class

5. const_cast

6. delete

7. dynamic_cast

8. explicit

9. export

10. false

11. friend

12. inline

13. mutable

14. namespace

15. new

16. operator

17. private

18. protected

19. public

20. reinterpret_cast

21. static_cast

22. template

23. this

24. throw

25. true

26. try

27. typeid

28. typename

29. using

30. virtual

31. wchar_t

Example 1:-

#include<stdio.h>

int main() {
int age=20; // using keyword int
printf("Welcome to TechVidvan Tutorials..\n\n");
if(age>18){ // using keyword if and else
  printf("Eligible to vote!");
}else{
  printf("Not Eligible to vote!");
}
}

Output

Welcome to TechVidvan Tutorials..Eligible to vote!

Example 2:-

#include<stdio.h>

int main() {
int number;
while(1){ // while keyword
  printf("Enter a number: ");
  scanf("%d",&number);
  if(number==0){
    	printf("Do not put zero!\n");
    	continue; // continue keyword
  }
  else if(number<0){
    	printf("Loop Terminated!\n");
    	break;
  }
  printf("\nWelcome to TechVidvan Tutorials..\nThe Number is: %d\n",number);
  break;
}
return 0;
}

Output

Enter a number: 10
Welcome to TechVidvan Tutorials..
The Number is: 10

2. Identifiers in C:-

In C, identifiers are user defined words. Mainly used for naming variables, functions, arrays, structures, etc. It can not be used as keywords. Below are the rules which you have to follow to construct identifiers:-

a. Each identifier has to be a unique one.

b. First character of the identifier should be an alphabet or underscore and then followed by any characters and digits.

c. In the identifier, do not add any commas or blank space.

d. Identifiers are case sensitive.

e. Length of the identifier should not be more than 31 characters.

f. Name of the identifier must be meaningful.

g. An identifier should not start with numbers.

Syntax:-

int amount;
double totalbalance;

Below is the table that shows the valid use of identifiers:-

Identifier Name Valid or Invalid Correction
_num valid No correction needed.
student name invalid studentname
your.bill invaild your_bill
my_age valid No Correction needed.

3. Strings in C:-

In C, strings are an array of characters having null character ‘\0’ at the end of the string. The size of the strings is the number of characters in the string. Strings are enclosed in double quotes(“”) and characters are enclosed in single quotes(‘’).

Syntax:-

char a[10]={'1','2','3'};
char b[]="TechVidvan";

Memory Diagram:-

T e c h V i d v a n \0

Example:-

#include<stdio.h>

int main() {
char s[100]="Welcome To TechVidvan Tutorials..\n";
printf("%s",s);
return 0;
}

Output

Welcome To TechVidvan Tutorials..

4. Operators in C:-

Mainly used to perform operations on provided data. With the help of the operators, you can perform mathematical operations. It is one of the most useful and efficient features.

Further classified into:-

Unary Operator:-
Mainly used with a single operand. For example, increment(++), decrement(–), sizeof(), (type)* etc.

a. Binary Operator in C

Binary operator is applied between two operands. Following is the list of binary operators in C:-

1. Arithmetic Operator

2. Relational Operator

3. Logical Operator

4. Misc Operators

5. Bitwise Operators

6. Shift Operators

7. Conditional Operators

8. Assignment Operators

b. Constants in C

From the name, you know that the main purpose of this is to make the value fix. And you cannot change the constant value. In two ways, you can declare a constant:-

1. Using const keyword

2. Using #define pre-processor

Syntax:-

const
Using const keyword:-
const variableName;
Using #define keyword:-
#define NAME value;

Types of constants in C:-

Constant Example
Integer Constant 10, 11, 34 etc.
Floating-point Constant 45.6, 46.2, 56.2 etc.
Octal Constant 011, 088, 022 etc.
Hexadecimal Constant 0x1a, 0x4b, 0x6b etc.
Character Constant ‘A’, ’b’, ’c’ etc.
String Constant “C”, “TechVidvan” etc.

Special Symbols in C

Apart from the above discussed token, some special symbols are used in C. These symbols have special meaning and they can’t be used for any other purposes.

1. Square Brackets []:– Mainly used for single and multi-dimensional arrays.

2. Simple Brackets ():- Used for function declaration and function calling like printf(), scanf() etc.

3. Curly Braces {}:- Used for opening and closing the codes and the loops.

4. Comma (,):- Used for separating more than one statement.

5. Hash and pre-processor (#):- It denotes that we are using the header file.

6. Asterisk (*):- Mainly used for pointer variables and also used for multiplication.

7. Tilde (~):- Mainly used for destructing the memory.

8. Period (.):- Mainly used for accessing a member of a structure or a union.

Example of the implementation of tokens in C:-

#include<stdio.h>

int main() {
int num;
printf("Welcome to TechVidvan Tutorials..\n\n");
printf("Enter numbers between 1 to 3: \n");
scanf("%d",&num);
switch(num){
  case 1:
  printf("You entered 1!!");
  break;
  case 2:
  printf("You entered 2!!");
  break;
  case 3:
  printf("You entered 3!!");
  break;
  default:
  printf("Please, enter valid number!\n");
}
return 0;
}

Output

Welcome to TechVidvan Tutorials..Enter numbers between 1 to 3: 1
You entered 1!!

Example of function in C

#include<stdio.h>

int main() {
int i=5;
int areaSquare(int a){
  return a*a;
}
int area = areaSquare(i);
printf("Square Area is = %d\n",area);
return 0;
}

Output

Square Area is = 25

Summary

A token is the smallest unit in programs. Keywords are predefined or reserved words that have their own importance. The main purpose of constant is to make the value fix. In C, identifiers are user defined words. Mainly used for naming variables, functions, arrays, structures etc.