Tokens in C Programming | Free Notes |

In C programming, tokens are the smallest individual units in the source code that the compiler recognizes and processes. These tokens are the building blocks of C programs and include identifiers, keywords, constants, string literals, operators, and punctuation symbols.

Types of Tokens in C


In C programming, there are six types of tokens :

  1. Keywords: Reserved words with special meanings in the C language, such as if, else, for, while, int, float, etc.
  2. Identifiers: Names given to variables, functions, and other user-defined entities in a C program. They consist of letters, digits, and underscores, but cannot start with a digit.
  3. Constants: Fixed values that do not change during program execution. Constants can be of various types, such as integer constants, floating-point constants, character constants, and string literals.
  4. String literals: Sequences of characters enclosed within double quotation marks, used to represent textual data in a C program.
  5. Operators: Symbols used to perform operations on operands. Examples include arithmetic operators (+, -, *, /), relational operators (<, >, ==, !=), logical operators (&&, ||, !), etc.
  6. Punctuation symbols: Special symbols used to separate elements in the source code, such as semicolons (;), commas (,), parentheses (()), braces ({}), square brackets ([]), etc.

C Tokens- Keywords


Keywords are reserved words in C programming language that have predefined meanings and cannot be used as identifiers. They form the basic syntax of the language and dictate its structure and behavior. Examples of keywords include int, float, if, else, for, while, return, etc. These words serve specific purposes and are an integral part of writing C programs.

#include <stdio.h>

int main() {
    int x = 10;
    if (x > 5) {
        printf("x is greater than 5\n");
    }
    return 0;
}

C Tokens- Identifiers


Identifiers are user-defined names given to various elements in a C program, such as variables, functions, arrays, etc. They must follow certain rules:

  • Must begin with a letter (uppercase or lowercase) or an underscore.
  • Can be followed by letters, digits, or underscores.
  • Should not be a keyword. Identifiers provide a way to uniquely identify different entities within a program and play a crucial role in defining its structure and logic.
int num_students;
float calculateAverage(float array[], int size) {
    // Function implementation
}

C Tokens- Constants


Constants in C are fixed values that do not change during the execution of a program. They can be of various types:

  • Integer constants: Whole numbers without decimal points, like 10, -5, 0.
  • Floating-point constants: Numbers with decimal points or in exponent notation, like 3.14, 1.5e2.
  • Character constants: Single characters enclosed in single quotes, like 'A', '5', '%'.
  • String literals: Sequences of characters enclosed in double quotes, like "Hello", "C programming". Constants provide a way to represent fixed values in programs and are used extensively in assignments, comparisons, and calculations.
#define PI 3.14159
const int MAX_SIZE = 100;
char grade = 'A';
char message[] = "Hello, world!";

C Tokens- Strings


String literals are sequences of characters enclosed within double quotation marks. They represent textual data in a C program and are stored as arrays of characters terminated by a null character ('\0'). String literals are used for displaying messages, reading input, and various other purposes involving textual data manipulation.

printf("Hello, world!\n");
char name[] = "John";

C Tokens- Operators


Operators in C are symbols used to perform operations on operands. They can be categorized into various types:

  • Arithmetic operators: +, -, *, /, %
  • Relational operators: <, >, <=, >=, ==, !=
  • Logical operators: &&, ||, !
  • Assignment operators: =, +=, -=, *=, /=, %=
  • Increment/decrement operators: ++, --
  • Bitwise operators: &, |, ^, ~, <<, >> Operators enable computations, comparisons, logical operations, and bitwise manipulations within C programs.
int sum = 10 + 5;
int result = (x > y) ? x : y;
int isEven = (num % 2 == 0) && (num > 0);

C Tokens- Punctuation Symbols


Punctuation symbols are special characters used to separate elements or denote specific syntactic structures in C programs. They include:

  • Semicolon (;): Marks the end of a statement.
  • Comma (,): Separates items in a list or function arguments.
  • Parentheses (()): Enclose expressions, function parameters, and control structures.
  • Braces ({}): Define the scope of functions, control structures, and blocks of code.
  • Square brackets ([]): Used for array indexing. Punctuation symbols play a crucial role in structuring and organizing the code.
int main() {
    int x = 10, y = 5;
    if (x > y) {
        printf("x is greater than y\n");
    }
    return 0;
}

Understanding these tokens and their usage is essential for writing correct and meaningful C programs. Each token serves a specific purpose and contributes to the overall structure and functionality of the program.

We’ll discuss all the tokens one by one in details in the next section…….

Don't be a Chatur. Be the Rancho, Share it to your Friends also....