Tokens in C

Tokens in C

In the world of C programming, tokens are like the Lego pieces that make up the language. They are the smallest meaningful parts that the computer understands, like words in a sentence. These tokens include things like special words, names, symbols, and numbers. By understanding these tokens, you can build and work with C programs effectively. So, let’s take a closer look at these building blocks and how they come together to create your code.

Types of Tokens in C

C Tokens are classified into six depending on the function they do.

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Special Symbols
  6. Operators

1. C Token – Keywords

Keywords in a programming language are like special commands that the language already knows. Each keyword has a specific job in a program. Since they are reserved for the compiler, we can’t use them as names for our own variables because that would give them a different job, and that’s not allowed. You can’t change what keywords mean. However, you can tell the compiler to replace certain words with other words before the program is compiled using C preprocessor directives. In the C language, there are 32 keywords, each with its own unique function

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. C Token – Identifiers

Identifiers are like custom labels we use for naming things in our program, such as variables, functions, and arrays. These labels are made by users and can be quite long, containing letters and numbers. They must start with a letter or an underscore (_). It’s essential that the names of identifiers don’t match any of the reserved keywords because those are already used for special purposes.

Once you give something an identifier name, you can refer to it later in your program. There’s also a unique type of identifier called a “statement label” that’s used in “goto” statements for specific purposes.

Rules for Naming Identifiers

When naming identifiers in C, there are specific rules to follow:

  1. They must start with a letter or an underscore (_).
  2. They can contain only letters, digits, or underscores; no other special characters are allowed.
  3. They cannot be a reserved keyword.
  4. They must not include any white spaces.
  5. They should be no longer than 31 characters, as only the first 31 characters are significant.

Adhering to these rules ensures that your identifiers are correctly recognized and processed by the C compiler.

Example

main: method name.
a: variable name.

3. C Token – Constants

Constants in programming are like unchanging values stored in variables. They work like regular variables, but the key difference is that their values can’t be changed once they’re set in the program. These constants can belong to any data type, just like regular variables.

Examples of Constants in C

const int c_var = 20;
const int* const ptr = &c_var;

4. C Token – Strings

In C and C++, strings are essentially arrays of characters, and they will have a special character called the null character ('\0') at the end. This null character marks the end of the string. Strings are enclosed within double quotes, while individual characters are enclosed within single quotes in these programming languages.

Example of string

char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
char string[20] = “skill vertex”;
char string [] = “skill vertex”;

5. C Token – Special Symbols

These are some essential symbols and their uses in programming:

  1. Brackets []: Used for array element references, indicating single or multidimensional subscripts.
  2. Parentheses (): Indicate function calls and function parameters.
  3. Braces {}: Mark the start and end of a code block containing multiple executable statements.
  4. Comma ,: Separates multiple statements, such as parameters in function calls.
  5. Colon :: Invokes an initialization list.
  6. Semicolon ;: Serves as a statement terminator, marking the end of a logical entity. Each statement must end with a semicolon.
  7. Asterisk *: Used to create pointer variables and for multiplication.
  8. Assignment operator =: Assigns values and performs logical operations.
  9. Pre-processor #: Automatically transforms your program before actual compilation.
  10. Period .: Accesses members of a structure or union.
  11. Tilde ~: Used as a destructor to free memory space.

These symbols play crucial roles in programming languages, helping programmers write clear and efficient code.

6. C Token – Operators

Operators in programming are symbols that perform specific actions when applied to variables and other objects. The objects on which operators operate are known as operands.

Operators can be categorized based on the number of operands they require:

  1. Unary Operators: These operators work with a single operand. Examples include increment and decrement operators.
  2. Binary Operators: Binary operators work with two operands. They can be further divided into categories like arithmetic, relational, logical, assignment, and bitwise operators.
  3. Ternary Operator: The ternary operator is a special case that works with three operands. It’s often referred to as the conditional operator and is represented by ?.

These operators are fundamental tools in programming, allowing developers to perform various operations on data and make decisions within their code.

FAQ- Tokens in C

Q1. What are the tokens in C?

Ans. In a C program, a token is the tiniest unit of code. It encompasses every punctuation mark and word you encounter in the program. When the compiler processes a C program, it breaks it down into these tokens as the initial step in the compilation process. These tokens serve as the building blocks that the compiler uses to understand and analyze the code further.

Q2. What type of word is a token?

Ans. A token can also refer to a specific occurrence of a word, symbol, expression, sentence, or similar element within a larger context. For example, in a printed page, you might find multiple instances or occurrences of the same word, like “and.” This usage of “token” helps identify and count individual occurrences of elements within a set, similar to how you might compare types or categories.

Q3. How to separate tokens in C?

Ans. The strtok() function in C is used to split a string (str[]) into tokens based on specified delimiters. It returns the next token on each call and should be used in a loop to retrieve all tokens. When there are no more tokens, it returns NULL.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment