Tokens in C

Sunny Bhaskar

10/28/20242 min read

In C programming, a token is the smallest unit of a program that is meaningful to the compiler. When a C program is compiled, the compiler breaks the source code into these tokens. There are six types of tokens in C, and they play a crucial role in defining the structure and syntax of the program.

Types of Tokens in C

1. Keywords

These are reserved words in C that have a predefined meaning and purpose. You cannot use them as identifiers (like variable names).

Examples: `int`, `float`, `if`, `else`, `while`, `return`, `void`, `for`, etc.

Example

int main() {

return 0;

}

Here, `int` and `return` are keywords.

2. Identifiers

Identifiers are names given to variables, functions, arrays, and other user-defined entities.

They must begin with a letter (uppercase or lowercase) or an underscore (`_`), followed by letters, digits, or underscores.

Identifiers are case-sensitive in C (e.g., `sum` and `Sum` are different).

Example

int sum, value;

// sum and value are identifiers

3. Constants

Constants are fixed values that do not change during the execution of a program. They can be integer constants, floating-point constants, character constants, or string literals.

Example

int a = 5;

// 5 is an integer constant

float pi = 3.14;

// 3.14 is a floating-point constant

char letter = 'A';

// 'A' is a character constant

4. Operators

Operators are symbols used to perform operations on variables and values. C supports various types of operators like arithmetic, relational, logical, bitwise, assignment, and more.

Example

int x = 5 + 3;

// + is an arithmetic operator

if (x > 3) { // > is a relational operator

// ...

}

5. Special Symbols

These include symbols that have a special meaning in C, such as braces, parentheses, brackets, commas, semicolons, and the pound sign (`#`).

Example

int main()

{

// { and } are special symbols

int arr[5];

// [ and ] are special symbols for arrays

return 0;

// ; is a semicolon that marks the end of a statement

}

6. Strings

Strings are a sequence of characters enclosed in double quotes. Strings are treated as arrays of characters.

Example

char name[] = "Sunny";

// "Sunny" is a string constant

Example Code with Tokens
#include <stdio.h>
// Special Symbol: #, Identifier: stdio.h

int main()

{

// Keywords: int, main, Special Symbols: (), {}

int a = 10;

// Keyword: int, Identifiers: a, Constant: 10

float b = 3.14;

// Keyword: float, Identifier: b, Constant: 3.14

char letter = 'A';

// Keyword: char, Identifier: letter, Constant: 'A'

// Operator: +, Identifier: sum

int sum = a + (int)b;

// Typecasting b from float to int

printf("Sum: %d", sum);

// Special Symbol: (), Identifier: printf, String: "Sum: %d"

return 0;

// Keyword: return, Constant: 0

}

Summary

Tokens are the smallest building blocks of a C program.

The six types of tokens are Keywords, Identifiers, Constants, Operators, Special Symbols, and Strings.

Each token plays a specific role in defining the structure and logic of the program.

Address

Prayagraj Phaphamau

Contacts

7518998334
sunnygamingyt298@gmail.com

Subscribe to our newsletter