C Tokens

C Tokens: Full Details with Examples and Syntaxes

In C programming, a token is the smallest unit of a program that the compiler interprets. The C compiler breaks the program into these tokens for processing. The six types of tokens in C are:

  • Keywords
  • Identifiers
  • Constants
  • Strings
  • Operators
  • Special Symbols

1. Keywords

Keywords are reserved words predefined in the C language. They have a special meaning and cannot be used for anything other than their intended purpose (e.g., as variable names or function names).

C Keywords:

  • int, float, return
  • if, else, switch
  • for, while, do
  • break, continue
  • struct, typedef
  • sizeof, static, volatile

Example of C Keywords :

#include <stdio.h>

int main() {
    int number = 10;  // 'int' is a keyword
    if (number > 5) {
        printf("Number is greater than 5");
    }
    return 0;  // 'return' is a keyword
}
    

2. Identifiers

Identifiers are user-defined names used to identify variables, functions, arrays, structures, etc. Identifiers are not keywords and must follow specific naming rules.

  • Can contain letters (both uppercase and lowercase), digits, and underscores (_).
  • Must not begin with a digit.
  • Are case-sensitive (sum and Sum are different).
  • No spaces or special characters allowed except the underscore.

Example of Identifiers :

#include <stdio.h>

int main() {
    int age = 25;  // 'age' is an identifier
    float salary = 45000.50;  // 'salary' is an identifier
    printf("Age: %d, Salary: %.2f", age, salary);
    return 0;
}
    

3. Constants

Constants are fixed values that cannot be altered by the program during execution. They can be of various types such as integer, floating-point, character, or string literals.

  • Integer Constants: Numbers without decimals (e.g., 10, -5).
  • Floating-point Constants: Numbers with decimals (e.g., 3.14, -0.001).
  • Character Constants: Single characters enclosed in single quotes (e.g., 'A', '9').
  • String Constants: A sequence of characters enclosed in double quotes (e.g., "Hello").

Example of Constants in C :

#include <stdio.h>

int main() {
    const int PI = 3.14159;  // Integer constant
    const char letter = 'A'; // Character constant
    const char* message = "Hello, World!";  // String constant

    printf("%f", PI);
    printf("%c", letter);
    printf("%s", message);

    return 0;
}
    

4. Strings

A String is a sequence of characters enclosed within double quotes. Strings in C are stored as arrays of characters ending with a null character (\0).

Example of Strings :

#include <stdio.h>

int main() {
    char str[] = "Welcome to C programming";  // String
    printf("%s", str);
    return 0;
}
    

5. Operators

Operators in C are symbols used to perform operations on operands. There are several types of operators in C:

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, <, >
  • Logical Operators: && (AND), || (OR), ! (NOT)
  • Assignment Operators: =, +=, -=, *=, /=, %=
  • Increment/Decrement Operators: ++, --

Example of Strings :

#include <stdio.h>

int main() {
    int a = 10, b = 5;

    // Arithmetic operators
    printf("Sum: %d\n", a + b);   // Addition
    printf("Difference: %d\n", a - b);  // Subtraction

    // Relational operator
    if (a > b) {
        printf("a is greater than b\n");
    }

    // Logical operator
    if (a > 0 && b > 0) {
        printf("Both a and b are positive\n");
    }

    return 0;
}
    

6. Special Symbols

C uses various special symbols for different purposes:

  • Comma (,): Separates function arguments or variable declarations.
  • Semicolon (;): Marks the end of a statement.
  • Braces ({ }): Denote the beginning and end of a block of code.
  • Parentheses (( )): Enclose function parameters and expressions.
  • Square Brackets ([ ]): Used for array subscripts.
  • Pound Symbol (#): Precedes preprocessor directives (e.g., #include).

Example of Special Symbols :

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};  // Square brackets for array
    for (int i = 0; i < 5; i++) {  // Parentheses for loop condition
        printf("%d\n", arr[i]);    // Curly braces for loop body
    }
    return 0;
}
    
Output: 1, 2, 3, 4, 5

Summary of C Tokens

  • Keywords: Predefined reserved words in C. Example: int, return, if.
  • Identifiers: User-defined names for variables, functions, etc. Example: age, sum, calculate.
  • Constants: Fixed values in a program. Example: 10, 3.14, 'A', "Hello".
  • Strings: A sequence of characters enclosed in double quotes. Example: "Hello, World!".
  • Operators: Symbols used for performing operations. Example: +, -, *, &&, ==.
  • Special Symbols: Include comma, semicolon, braces, parentheses, etc. Example: {}, [], #include.