Tokens

Home / C Programming / Tokens

Tokens


In C programming, tokens are the smallest individual units that make up a program. The C compiler scans the source code and breaks it down into tokens before further processing. Here are the main types of tokens in C:

Keywords: These are reserved words that have specific meanings in the C language. Examples include "if," "else," "for," "while," "int," "char," and so on.

Identifiers: These are user-defined names used to identify variables, functions, or any other user-defined entities. An identifier must start with a letter (a to z or A to Z) or an underscore (_) and can be followed by letters, digits, or underscores.

Constants: Constants represent fixed values that do not change during program execution. There are several types of constants in C, including integer constants, floating-point constants, character constants, and string constants.

Operators: Operators perform operations on operands. C supports a wide range of operators, such as arithmetic operators (+, -, *, /), relational operators (>, <, >=, <=, ==, !=), logical operators (&&, ||, !), assignment operators (=, +=, -=, etc.), and many more.

Strings: A sequence of characters enclosed in double quotes (e.g., "Hello, World!"). Strings are used to represent textual data.

Delimiters: These include symbols that separate parts of the program, such as commas (,), semicolons (;), parentheses (()), braces ({ }), and square brackets ([]).


Comments: Comments are not considered tokens but serve as explanatory statements within the source code. They are ignored by the compiler and help in documenting the code.

Here's an example of a C program with various tokens:

#include <stdio.h>

int main() {

    int num1 = 5;

    int num2 = 10;

    int sum = num1 + num2;

    printf("The sum of %d and %d is %d\n", num1, num2, sum);

    return 0;

}

In this program, some of the tokens include keywords (#include, int, main, return), identifiers (num1, num2, sum), constants (5, 10), operators (=, +), delimiters (;, ,), and a comment (// The sum of num1 and num2).