C Programs Tutorials | IT Developer
IT Developer

C Programming - C Tokens



Share with a Friend

C Programming - C Tokens

In the C programming language, a token is the smallest unit of a program. The compiler identifies tokens during the compilation process to understand the program structure.

Types of Tokens in C

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Operators
  6. Special Symbols
  1. Keywords
  • Reserved words with a predefined meaning in C.
  • Cannot be used as identifiers (e.g., variable names or function names).
  • Examples: :
int, float, if, else, while, return, break, void, char
  • Example in Code:
int number = 10;  // 'int' is a keyword
  1. Identifiers
  • Names given to variables, functions, arrays, etc., defined by the programmer.
  • Must begin with a letter or an underscore (_) and can be followed by letters, digits, or underscores.
  • Case-sensitive.
  • Rules:
    • Cannot be a keyword.
    • Should not contain spaces or special symbols (except _).
    • Must be unique within its scope.
  • Example in Code:
int age = 25;  // 'age' is an identifier
  1. Constants
  • Fixed values that do not change during program execution.
  • Types:
    • Integer Constants: Whole numbers (e.g., 10, -45).
    • Floating-point Constants: Decimal numbers (e.g., 3.14, -0.99).
    • Character Constants: A single character enclosed in single quotes (e.g., 'A').
    • String Constants: A sequence of characters enclosed in double quotes (e.g., "Hello").
  • Example in Code:
const float PI = 3.14;  // 'PI' is a constant
  1. Strings
  • A sequence of characters enclosed in double quotes.
  • Represented as an array of characters internally.
  • Example in Code:
char greeting[] = "Hello, World!";
  1. Operators
  • Symbols used to perform operations on variables and values.
  • Types:
    • Arithmetic Operators: +, -, *, /, %
    • Relational Operators: ==, !=, <, >, <=, >=
    • Logical Operators: &&, ||, !
    • Assignment Operators: =, +=, -=, *=, /=
    • Bitwise Operators: &, |, ^, ~, <<, >>
  • Example in Code:
int sum = a + b;  // '+' is an arithmetic operator
  1. Special Symbols
  • Special characters used in C for specific purposes.
  • Examples:
    • Braces {}: Define blocks of code.
    • Parentheses (): Enclose function arguments or expressions.
    • Brackets []: Enclose array elements.
    • Comma ,: Separate multiple variables or parameters.
    • Semicolon ;: End statements.
    • Pound sign #: Preprocessor directives.
    • Asterisk *: Pointer declaration or multiplication.
  • Example in Code:
int arr[5] = {1, 2, 3, 4, 5};  // '{}' and '[]' are special symbols

Code Example with All Tokens

#include <stdio.h>  // Preprocessor directive

 

int main() {  // 'int' is a keyword, 'main' is an identifier

    int num = 10;  // 'int' is a keyword, 'num' is an identifier, '10' is a constant

    float pi = 3.14;  // 'float' is a keyword, 'pi' is an identifier, '3.14' is a constant

    char c = 'A';  // 'char' is a keyword, 'c' is an identifier, 'A' is a character constant

   

    printf("Number: %d\n", num);  // 'printf' is a function, "Number: %d" is a string

    return 0;  // 'return' is a keyword, '0' is a constant

}

Summary of Tokens

Token Type Examples
Keywords

int, float, return, if

Identifiers

num, pi, main

Constants

10, 3.14, 'A'

Strings

"Hello", "Number: %d"

Operators

+, -, *, =, ==

Special Symbols

{}, (), [], #, ;

These tokens form the building blocks of any C program. Understanding them is crucial for writing and interpreting C code effectively.