dhairyashah
Portfolio

Oct 1st, 2023

What are Tokens in C Language? | Complete Guide for Beginners

"Explore the essential guide to C programming tokens! Learn the significance of keywords, identifiers, constants, and operators."

Author Picture

Dhairya Shah

Software Engineer

Tokens are the building blocks of a C program, and every C source code is broken down into these fundamental units during the compilation process. Tokens can thus be defined as the smallest items in the C programming language the compiler understands.

Types of Tokens

Tokens in the C language can be classified into six different types based on their functions. Following is the list of tokens:

  1. Keywords

  2. Identifiers

  3. Constants

  4. Strings

  5. Special Symbols

  6. Operators

Now, let’s understand each token one by one.

1. Keywords in C

Keywords in C can be defined as the pre-defined or reserved words in the C programming language. Each keyword is meant to perform a specific function in a program. Since the keywords are reserved words we cannot use them as variable names.

C language supports 32 keywords which are given below:

auto         double      int        struct
break        else        long       switch
case         enum        register   typedef
char         extern      return     union
const        float       short      unsigned
continue     for         signed     void
default      goto        sizeof     volatile
do           if          static     while

2. Identifiers in C

Identifiers are used for naming variables, functions, structures, arrays, etc. Identifiers are user-defined. Identifiers can be a mixture of uppercase letters, lowercase letters, underscores, or digits.

The following points should be kept in mind when constructing identifiers in C:

3. Strings in C

Strings are made up of multiple characters and are represented by an array of characters with the null character ‘0’ at the end. Strings are enclosed within double quotes (”).

The size of a string is the total number of characters within a string.

char str[4] = "tiny"

4. Operators in C

Operators are the special symbols to perform specific operations. Operators are applied between the operands.

The operators are classified as:

Unary Operator

Unary operators in C are operators that perform operations on a single operand. They operate on only one operand to produce a new value. Here are some common unary operators in C:

  1. Increment (++) and Decrement (—) Operators:

    • Increment operator (++) increases the value of the operand by 1.

    • Decrement operator (-) decreases the value of the operand by 1.

    int a = 5;
    int b = ++a; // Increment a before using its value, b becomes 6, a becomes 6
    int c = a--; // Use the current value of a, then decrement it, c becomes 6, a becomes 5
  2. Unary Plus (+) and Unary Minus (-) Operators:

    • Unary plus (+) represents the positive value of the operand.

    • Unary minus (`) represents the negative value of the operand.

    int x = 10;
    int y = -x; // y is -10
  3. Logical NOT (!) Operator:

    • Logical NOT (!) is used to invert the logical state of its operand. If the operand is true, the NOT operator makes it false, and vice versa.

    int condition = 1; // true
    int result = !condition; // result is 0 (false)
  4. Bitwise NOT (~) Operator:

    • Bitwise NOT (~) inverts the bits of its operand.

    unsigned int num = 5; // binary: 0000 0101
    unsigned int result = ~num; // result is 4294967290 (binary: 1111 1010)
  5. Sizeof Operator:

    • sizeof operator is used to get the size of a data type or a variable.

    int size = sizeof(int); // size is the number of bytes required to store an int on the system

These are just a few examples of unary operators in C. Unary operators play a crucial role in manipulating and evaluating expressions in C programming.

Binary Operators

Binary operators in C are operators that perform operations between two operands. These operators require two operands to carry out the specified operation. Here are some common binary operators in C:

  1. Arithmetic Operators:

    • Addition (+): Adds two operands.

    • Subtraction (-): Subtracts the right operand from the left operand.

    • Multiplication (*): Multiplies two operands.

    • Division (/): Divides the left operand by the right operand.

    • Modulus (%): Returns the remainder of the division of the left operand by the right operand.

    int a = 10, b = 3;
    int sum = a + b;       // sum is 13
    int difference = a - b;// difference is 7
    int product = a * b;   // product is 30
    int quotient = a / b;  // quotient is 3
    int remainder = a % b; // remainder is 1
  2. Relational Operators:

    • Equal to (==): Checks if two operands are equal.

    • Not equal to (!=): Checks if two operands are not equal.

    • Greater than (>): Checks if the left operand is greater than the right operand.

    • Less than (<): Checks if the left operand is less than the right operand.

    • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.

    • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

    int x = 5, y = 10;
    int isEqual = (x == y); // isEqual is 0 (false)
    int isNotEqual = (x != y); // isNotEqual is 1 (true)
  3. Logical Operators:

    • Logical AND (&&): Returns true if both operands are true.

    • Logical OR (||): Returns true if at least one operand is true.

    • Logical NOT (!): Inverts the logical state of the operand.

    int p = 1, q = 0;
    int logicalAnd = (p && q); // logicalAnd is 0 (false)
    int logicalOr = (p || q);  // logicalOr is 1 (true)
  4. Assignment Operators:

    • Assignment (=): Assigns the value of the right operand to the left operand.

    • Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.

    int num = 5;
    num += 3; // num is now 8 (equivalent to num = num + 3)
  5. Bitwise Operators:

    • Bitwise AND (&): Performs bitwise AND between corresponding bits of two operands.

    • Bitwise OR (|): Performs bitwise OR between corresponding bits of two operands.

    • Bitwise XOR (^): Performs bitwise XOR between corresponding bits of two operands.

    unsigned int a = 5, b = 3;
    unsigned int bitwiseAnd = a & b; // bitwiseAnd is 1 (binary: 0000 0001)

These are some examples of binary operators in C. They are fundamental for performing various operations and comparisons in C programming.

Ternary Operator

The ternary operator, also known as the conditional operator, is a unique operator in C that takes three operands and is used to evaluate a condition. It has the following syntax:

condition ? expression_if_true : expression_if_false;

Here’s how it works:

Here’s an example:

int x = 10, y = 20;
int max = (x > y) ? x : y;

In this example, if x is greater than y, the value of x is assigned to max; otherwise, the value of y is assigned to max.

The ternary operator is a concise way to write simple conditional statements. It’s often used for assigning values based on a condition in a single line, providing a more compact and readable code compared to an equivalent if-else statement.

Here’s another example to illustrate its use:

int num = 15;
char* result = (num % 2 == 0) ? "Even" : "Odd";

In this example, if num is even, the string “Even” is assigned to result; otherwise, the string “Odd” is assigned.

Keep in mind that while the ternary operator can make code more concise, it’s important to use it judiciously to maintain readability. Complex conditions or expressions within the ternary operator can make the code less clear, and in such cases, an if-else statement might be more appropriate.

5. Contants in C

In C, a constant is a value that remains unchanged throughout the program’s execution. Constants are essentially named values that are treated as literals and are not allowed to be modified once they are defined. There are two main types of constants in C:

1. Numeric Constants:

Numeric constants represent fixed numerical values. They can be of various types, such as integers, floating-point numbers, and characters.

Integer Constants:

const int MAX_VALUE = 100;

Floating-point Constants:

const float PI = 3.14;

Character Constants:

const char NEW_LINE = '\\\\n';

2. Symbolic Constants:

Symbolic constants are identifiers that represent a constant value. They are often defined using the #define preprocessor directive.

#define PI 3.14
#define MAX_VALUE 100

Symbolic constants are typically used for values that are used multiple times in a program. They offer the advantage of easy modification and understanding of the code.

Conclusion

In conclusion, tokens are the fundamental building blocks of C programming, serving as the smallest units of code that compilers understand. These tokens fall into six categories: keywords, identifiers, constants, strings, special symbols, and operators.

Keywords are reserved words in C with predefined functionalities, while identifiers are user-defined names for variables, functions, and other elements. Strings are arrays of characters enclosed in double quotes, and constants represent unchanging values, categorized into numeric and symbolic constants.

Operators, a crucial aspect of C programming, include unary, binary, and ternary operators, each serving different purposes. They perform operations between operands and are essential for manipulating data in expressions.

Understanding and effectively using these tokens are essential skills for any C programmer. Proper use of keywords, meaningful identifiers, and constants enhances code readability, while mastery of operators allows for efficient expression manipulation. By grasping the significance of each token type, programmers can write clear, concise, and effective C code.

I hope this article was helpful to you, thanks for reading! Have a great day!