In C programming, tokens are the smallest units in the language that have meaning. They are the fundamental building blocks of a C program, much like words are the building blocks of a sentence. Understanding tokens is crucial for grasping how C code is structured and interpreted by the compiler.
Tokens in C can be categorized into the following groups:
1. Keywords
2. Identifiers
3. Strings
4. Operators
5. Constants
6. Special Characters
Keywords are reserved words that have special meaning in C. They are predefined by the language and cannot be used for anything other than their intended purpose. Here is a list of C keywords:
| 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 |
Identifiers are names used to identify variables, functions, arrays, and other user-defined items. They are created by the programmer and must follow specific rules:
• Begin with a letter (A-Z or a-z) or an underscore (_).
• Followed by letters, digits (0-9), or underscores.
• Cannot start with a digit.
• Are case-sensitive (e.g., Variable and variable are different).
• Cannot be keywords.
• Should be meaningful and avoid using more than 31 characters (though many modern compilers support longer identifiers).
int count; // valid identifier
float average_score; // valid identifier
Strings in C are arrays of characters terminated by a null character ('\0'). They are enclosed in double quotes. Strings can be represented in various ways:
char str[10] = "example";
char str[10] = {'e', 'x', 'a', 'm', 'p', 'l', 'e', '\0'};
Operators perform operations on variables and values. They are classified based on the number of operands they work with:
• Unary Operators: Operate on a single operand (e.g., ++, --, sizeof).
• Binary Operators: Operate on two operands. They include:
• Arithmetic Operators: +, -, *, /, %
• Relational Operators: ==, !=, <, >, <=, >=
• Logical Operators: &&, ||, !
• Bitwise Operators: &, |, ^, ~, <<, >>
• Conditional Operator: ?:
• Assignment Operators: =, +=, -=, *=, /=, %=
• Miscellaneous Operators: ,, ?, ::, [], ()
Constants are values that do not change during the execution of a program. They can be defined in two ways:
const int MAX_SIZE = 100;
#define MAX_SIZE 100
• Integer Constants: 10, 200, 0x1a
• Floating-point Constants: 3.14, 2.718, 1.23e4
• Octal Constants: 011, 077
• Hexadecimal Constants: 0x1a, 0xFF
• Character Constants: 'a', 'b', '1'
• String Constants: "Hello", "World"
<#include <stdio.h>>
int main() {
const int limit = 50;
printf("Limit: %d\\n", limit);
return 0;
}
• Square Brackets [ ]: Used for array subscripts.
• Parentheses ( ): Used in function declarations and calls.
• Curly Braces { }: Used to define blocks of code (e.g., in functions, loops).
• Comma ,: Used to separate statements and function parameters.
• Hash #: Used for preprocessor directives (e.g., #include, #define).
• Asterisk *: Used for pointers and multiplication.
• Tilde ~: Bitwise NOT operator.
• Period .: Used to access members of a structure or union.