Skip to content
doubtly

1 answer

Tokens in C language are the smallest individual units of a program, such as keywords, identifiers, constants, operators, and special symbols. Here’s an explanation of each type of token with examples:

  1. Keywords:
    • Keywords are reserved words that have predefined meanings in C language.
    • Example: int, if, else, for, while, return, void, struct, etc.
  2. Identifiers:
    • Identifiers are names given to various program elements such as variables, functions, arrays, etc.
    • Rules for identifiers: They must begin with a letter (uppercase or lowercase) or an underscore (_), followed by letters, digits, or underscores.
    • Example: sum, i, MAX_SIZE, calculateArea, first_name, etc.
  3. Constants:
    • Constants are fixed values that do not change during program execution.
    • Types of constants include integer constants, character constants, floating-point constants, and string constants.
    • Example integer constant: 10, -20, 0xFF (hexadecimal), 077 (octal), etc.
    • Example character constant: 'A', 'b', '7', '%', etc.
    • Example floating-point constant: 3.14, -0.0012, 2.0E-5, etc.
    • Example string constant: "Hello", "123", "C programming", etc.
  4. Operators:
    • Operators perform operations on operands.
    • Types of operators include arithmetic operators, relational operators, logical operators, assignment operators, etc.
    • Example arithmetic operators: +, -, *, /, %.
    • Example relational operators: ==, !=, <, >, <=, >=.
    • Example logical operators: && (AND), || (OR), ! (NOT).
    • Example assignment operators: =, +=, -= etc.
  5. Special Symbols:
    • Special symbols include punctuation marks and other symbols used in C syntax.
    • Example: ; (semicolon), , (comma), {} (curly braces), () (parentheses), [] (square brackets), . (dot), -> (arrow), etc.
  6. Whitespace:
    • Whitespace includes spaces, tabs, and newlines.
    • Whitespace is used to separate tokens but is otherwise ignored by the compiler.
    • Example: Spaces between keywords, identifiers, operators, etc.

Example:

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

In this example:

  • Keywords: int, return, void.
  • Identifiers: main, num1, num2, sum.
  • Constants: 10, 20, 0.
  • Operators: =, +.
  • Special Symbols: {, }, =, ;, (), ,, "\n".
  • Whitespace: Spaces, newlines.
Doubtly · 13 Apr 2024 Report

Your answer

Log in or create a free account to answer this question.