TOKEN IN C

Token In C

A token can be a keyword, an identifier, a constant, a string literal, an operator, or a special symbol. The C compiler recognizes and interprets the sequence of tokens in a program.

Here are the types of tokens in C:

  1. Keywords: Keywords are reserved words in the C language that have a special meaning and cannot be used as variable names or function names. Examples of keywords include int, float, if, else, for, and while.
  2. Identifiers: Identifiers are names that are used to identify variables, functions, and other entities in a C program. An identifier can be any sequence of letters, digits, and underscore character _, but it must start with a letter or underscore. Examples of identifiers include sum, num, and my_function.
  3. Constants: Constants are values that cannot be changed during the execution of a program. There are two types of constants in C: numeric constants and character constants. Numeric constants can be integers, floating-point numbers, or exponential numbers. Examples of numeric constants include 12, 3.14, and 1.23e-4. Character constants are enclosed in single quotes, and they represent a single character. Examples of character constants include ‘a’, ‘7’, and ‘\n’.
  4. String literals: String literals are sequences of characters enclosed in double quotes. They represent a string of characters, and they are often used in input/output operations. Examples of string literals include “Hello, world!” and “Enter your name: “.
  5. Operators: Operators are symbols that perform an operation on one or more operands. Examples of operators in C include arithmetic operators (+, , *, /, %), assignment operators (=, +=, -=), relational operators (<, >, <=, >=, ==, !=), and logical operators (&&, ||, !).
  6. Special symbols: Special symbols are punctuation marks and other symbols used to separate, group, or terminate tokens. Examples of special symbols in C include semicolon ;, comma ,, parentheses (), braces {}, and brackets [].

Here’s an example program that demonstrates some of the tokens in C:

				
					#include <stdio.h>
 int main()
 {
 int num1 = 10, num2 = 20;
 int sum = num1 + num2; 
printf("The sum of %d and %d is %d\n", num1, num2, sum);
 return 0;
 } 

				
			

In this program, the tokens include keywords (int, return), identifiers (main, num1, num2, sum), constants (10, 20), assignment operator (=), arithmetic operator (+), string literal (“The sum of %d and %d is %d\n”), special symbols (;, ,, (), {}, []), and function calls (printf).

Join To Get Our Newsletter
Spread the love