C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Basic Elements of C
  5. Tokens in C

Tokens in C

Tokens in C are the smallest individual units of a program that have meaning to the compiler.

Thank you for reading this post, don't forget to subscribe!
  • The compiler breaks a C program into these tokens during compilation.
  • Everything written in a C program is composed of tokens.

Identifying and understanding tokens is essential because they are the building blocks of statements and expressions in the C language.

Types of C Tokens:

Keywords are reserved words in C that have special meanings and purposes. They are part of the syntax and cannot be used as identifiers.

  • Examples: int, float, if, else, while, return, for, switch.
  • Cannot be used as variable or function names.

Example:

#include <stdio.h>  // Preprocessor directive to include standard I/O

int main() {
    int age = 20;  // 'int' and 'return' are keywords

    if (age > 18) {          // 'if' is a keyword
        printf("Adult\n");   // 'printf' is a standard library function
    } else {
        printf("Minor\n");   // 'else' is also a keyword
    }

    return 0;  // 'return' is a keyword
}

An identifier is the name given to variables, functions, or classes. It helps uniquely identify them.

Rules:

  • Must start with a letter (A–Z or a–z) or underscore (_).
  • Can contain letters, digits (0–9), and underscores.
  • Cannot use C keywords as identifiers.
  • Case-sensitive (e.g., Total and total are different).
  • Cannot contain special characters like @, #, $, %, etc.

Example:

// Valid Identifiers
int myVariable = 10;
float _price = 45.50;
char name123[] = "C Language";

// Invalid Identifiers
int 123value = 50;     // Starts with digit
float my-price = 10.5; // Contains special character '-'
int return = 30;       // Uses a keyword

Constants are fixed values that do not change during program execution.

There are two standard ways to define constants in C:

  • Using the const Keyword:
const int MAX = 100;
const float PI = 3.14159;
  • Using the #define Preprocessor Directive
#define MAX 100
#define PI 3.14159

Operators are special symbols or keywords used to perform operations on variables and values.

  • They are used to manipulate data and variables in C.

Types of Operators in C:

Types of Operators in C

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    int sum = a + b;       // Arithmetic
    int isEqual = (a == b); // Relational
    int logical = (a > 0 && b > 0); // Logical
    a += 2;                // Assignment
    int size = sizeof(a);  // Sizeof
    int max = (a > b) ? a : b; // Ternary

    printf("Sum: %d\n", sum);
    printf("Is Equal: %d\n", isEqual);
    printf("Logical AND: %d\n", logical);
    printf("Size of a: %d bytes\n", size);
    printf("Max: %d\n", max);

    return 0;
}

Punctuators are symbols in C that are used to group, separate, or structure code elements. They define the grammar of the C language.

Common Punctuators in C:

image 23

Example:

#include <stdio.h>  // Using #

int main() {        // Using (), {}
    int numbers[] = {1, 2, 3};  // [], {}, ;
    printf("C programming!\n");  // ;
    return 0;        // ;
}

String is a sequence of characters enclosed in double quotes (” “). These are actually stored as character arrays in C and always end with a null character (\0).

Example:

#include <stdio.h>

int main() {
    char greeting[] = "Hello, World!";  // String literal assigned to a character array

    printf("%s\n", greeting);  // %s is used to print string

    return 0;
}

How can we help?