Character Set of C Language and C Tokens

Character Set of C Language

The character set in C language refers to the valid set of characters that can be used in the source code. These characters include letters, digits, symbols, and special characters. They form the building blocks of C programs.


1. Letters

  • Definition: The alphabetic characters used in C.
  • Includes:
    • Uppercase Letters: A, B, C, ..., Z
    • Lowercase Letters: a, b, c, ..., z

2. Digits

  • Definition: Numeric characters used in the program.
  • Includes:
    • Digits: 0, 1, 2, ..., 9

3. Special Characters

  • Definition: Characters with special purposes in the C language.
  • Includes:
    ~ ` ! @ # $ % ^ & * ( ) - _ = +
    { } [ ] | \ : ; " ' < > , . ? /
  • Usage:
    • +, -, *, /: Arithmetic operators.
    • {}, [], (): Braces and brackets.
    • ;: Statement terminator.
    • &: Address operator.

4. White Space

  • Definition: Characters used for spacing in the code to improve readability.
  • Includes:
    • Space ( )
    • Tab (\t)
    • Newline (\n)

5. Escape Sequences

  • Definition: Special character combinations used to represent non-printable or special characters.
  • Examples:
    Escape Sequence Description
    \n Newline
    \t Horizontal Tab
    \\ Backslash
    \' Single Quote
    \" Double Quote
    \0 Null Character

6. Symbols

  • Definition: Characters used for specific meanings in programming.
  • Examples:
    • Operators: +, -, *, /, %, etc.
    • Comparisons: >, <, >=, <=, ==, !=
    • Assignment: =

7. Constants

Although not characters, constants (like 123, 'A', or "Hello") are formed using the character set.


Summary Table

Category Examples
Letters A, B, ..., Z, a, b, ..., z
Digits 0, 1, 2, ..., 9
Special Characters !, @, #, $, ^, &, etc.
Escape Sequences \n, \t, \\, \', etc.
White Space Space, Tab, Newline

This character set forms the foundation for writing all C programs!

C Tokens

In the C language, tokens are the smallest units or building blocks of a program. A token is a meaningful element that the compiler recognizes during the compilation process. C has six types of tokens:


1. Keywords

  • Definition: Reserved words in C that have predefined meanings and cannot be used as variable names or identifiers.
  • Examples:
    int, float, if, else, while, return, break, continue, for, void, etc.
  • Purpose: Used to define the syntax and structure of C programs.

2. Identifiers

  • Definition: Names given to variables, functions, arrays, or other user-defined elements in a program.
  • Rules:
    1. Must start with a letter or underscore (_).
    2. Can contain letters, digits, and underscores.
    3. Cannot use C keywords.
  • Examples:
    age, totalSum, _temp, main, calculateArea

3. Constants

  • Definition: Fixed values that do not change during the execution of the program.
  • Types:
    • Integer Constants: Whole numbers (e.g., 10, -45).
    • Floating-point Constants: Decimal numbers (e.g., 3.14, -0.005).
    • Character Constants: Single characters enclosed in single quotes (e.g., 'A', '3').
    • String Constants: Sequence of characters enclosed in double quotes (e.g., "Hello", "C Language").

4. Operators

  • Definition: Symbols that perform operations on variables and values.
  • Types:
    • Arithmetic Operators: +, -, *, /, %
    • Relational Operators: <, >, <=, >=, ==, !=
    • Logical Operators: &&, ||, !
    • Bitwise Operators: &, |, ^, ~, <<, >>
    • Assignment Operators: =, +=, -=, *=, /=
    • Others: Increment (++), Decrement (--).

5. Special Symbols

  • Definition: Symbols with special meanings in C programs.
  • Examples:
    () {} [] ; , " " # *
  • Usage:
    • Parentheses () for function calls.
    • Braces {} for blocks of code.
    • Square brackets [] for arrays.
    • Semicolon ; to terminate statements.

6. Punctuation/Separators

  • Definition: Characters that help separate statements or elements in a program.
  • Examples:
    ; (semicolon), , (comma), " " (double quotes), ' ' (single quotes)

Examples of Tokens in a Program

 

#include <stdio.h> // Preprocessor directive (not a token)

int main() { // Tokens: int, main, (, )
int a = 10, b = 20, sum; // Tokens: int, a, =, 10, ,, b, =, 20, sum, ;
sum = a + b; // Tokens: sum, =, a, +, b, ;
printf(“Sum = %d”, sum); // Tokens: printf, (, “Sum = %d”, sum, ), ;
return 0; // Tokens: return, 0, ;
}


Summary of C Tokens

Token Type Examples
Keywords int, return, if, else, for, void
Identifiers main, a, sum, calculateArea
Constants 10, 3.14, 'A', "Hello"
Operators +, -, *, /, <, >, &&, =
Special Symbols {}, [], (), #, ;, "
Separators ;, ,, (), {}

By understanding C tokens, you can write syntactically correct and meaningful C programs.

Leave a Comment

Your email address will not be published. Required fields are marked *