In C programming, a token is the smallest unit or building block of a program. C tokens are the basic components that make up a C program. There are several types of tokens in C, and they are used to represent different elements of the language. Here are the main categories of C tokens:
1. **Keywords:**
- Keywords are reserved words in C that have predefined meanings. Examples include `int`, `if`, `else`, `while`, `for`, `return`, and `switch`. You cannot use keywords as identifiers (variable or function names) in your program.
2. **Identifiers:**
- Identifiers are user-defined names given to various program elements, such as variables, functions, arrays, and structures. An identifier must start with an alphabetic character (a-z, A-Z) or an underscore (_) and can be followed by a combination of letters, digits, or underscores. For example, `myVariable`, `_count`, and `calculateSum` are identifiers.
3. **Constants:**
- Constants are values that do not change during program execution. C supports several types of constants:
- Integer Constants: These include decimal (e.g., `42`), octal (e.g., `077`), and hexadecimal (e.g., `0x1A`) constants.
- Floating-Point Constants: These include real numbers with decimal points (e.g., `3.14` or `2.0E-5`).
- Character Constants: These are single characters enclosed in single quotes (e.g., `'A'` or `'5'`).
- String Constants: These are sequences of characters enclosed in double quotes (e.g., `"Hello, World!"`).
- Enumeration Constants: These are user-defined symbolic constants created using `enum` declarations.
4. **String Tokens:**
- String tokens are used to represent character strings, including identifiers, keywords, constants, and other string literals in the program.
5. **Operators:**
- Operators are symbols used to perform operations on operands. C supports various types of operators, including arithmetic operators (`+`, `-`, `*`, `/`, `%`), assignment operators (`=`, `+=`, `-=`), comparison operators (`==`, `!=`, `<`, `>`), logical operators (`&&`, `||`, `!`), and more.
6. **Punctuation and Separators:**
- Punctuation symbols and separators are used to structure the program. They include semicolons (`;`) to terminate statements, parentheses (`(` and `)`) for grouping and function calls, curly braces (`{` and `}`) for defining blocks of code, commas (`,`) to separate items in a list, and square brackets (`[` and `]`) for array indexing.
7. **Comments:**
- Comments are not technically tokens but are an essential part of a C program. They are used to provide explanations and annotations within the code. C supports both single-line comments (using `//`) and multi-line comments (using `/* ... */`).
8. **Preprocessor Directives:**
- Preprocessor directives are instructions for the C preprocessor, which processes the code before actual compilation. Examples include `#include` for including header files and `#define` for defining macros.
Here's an example of a C program with various types of tokens:
```c
#include <stdio.h>
int main() {
// This is a single-line comment
int number = 42; // This is an integer constant
char letter = 'A'; // This is a character constant
float pi = 3.14159; // This is a floating-point constant
printf("Hello, %s!\n", "World"); // This is a string token
return 0;
}
```
In this example, you can see keywords (`int`, `char`, `float`, `return`), identifiers (`main`, `number`, `letter`, `pi`), constants (`42`, `'A'`, `3.14159`, `"Hello, World!"`), operators (`=`, `;`, `,`, `(`, `)`, `;`), comments (`//` and `/* ... */`), and a preprocessor directive (`#include`). These elements collectively make up the C program's tokens.