Tokens and Identifiers in C

Tokens in C

In C programming language, a token is the smallest individual unit of a program. The C compiler breaks down the source code into tokens to analyze and understand its structure. Tokens can be classified into several categories:

  1. Keywords: These are reserved words in the C language that have special meanings and cannot be used as identifiers. Examples include int, if, while, for, return, etc.
  2. Identifiers: These are names used to identify variables, functions, or any user-defined entity in the program. Identifiers must follow specific rules and conventions, as discussed in the previous response.
  3. Constants: Constants represent fixed values that do not change during the program execution. They can be of different types, such as integer constants, floating-point constants, character constants, or string literals. Examples include 42, 3.14, 'A', "Hello, World!", etc.
  4. Operators: Operators are used to perform operations on variables or values. C provides a wide range of operators, including arithmetic operators (+, -, *, /, %), assignment operators (=, +=, -=), comparison operators (==, !=, <, >), logical operators (&&, ||, !), etc.
  5. Separators: Separators are used to separate or group different parts of the program. Common separators in C include commas (,), semicolons (;), parentheses (()), braces ({}), brackets ([]), etc.
  6. Special Symbols: Special symbols include special characters used in the program, such as the hash symbol (#) used for preprocessor directives, the dot (.) used for accessing members of a structure or union, the arrow (->) used for accessing members through a pointer, etc.

Identifiers :

In C programming language, an identifier is a name used to identify a variable, function, or any other user-defined entity in a program. It can be composed of letters (both uppercase and lowercase), digits, and underscore character (_). However, the first character of an identifier must be a letter or an underscore. The C language is case-sensitive, so uppercase and lowercase letters are considered distinct.

Here are some rules and conventions for naming identifiers in C:

  1. The length of an identifier should not exceed the compiler-specific limit (usually 31 or 63 characters).
  2. Identifiers cannot be a keyword or reserved word of the C language (e.g., if, while, int).
  3. White spaces and punctuation marks (such as @, $, %, etc.) are not allowed within an identifier.
  4. C identifiers should be meaningful and descriptive to enhance code readability.
  5. Camel case or underscore-separated naming conventions are commonly used. For example: myVariable, myFunction, MAX_SIZE, etc.
  6. Avoid starting an identifier with an underscore followed by a capital letter, as it is typically reserved for system-level identifiers.
  7. C language has a few naming conventions, such as using all uppercase letters for constant identifiers (e.g., PI = 3.14) and starting struct, union, and enum names with capital letters (e.g., struct Student).

Related Posts