C Tutorial



TOKENS IN C


Tokens in C

In C programming, a token is the smallest unit of a program. Tokens are the building blocks of a C program. A C program is made up of various types of tokens. These include:

  • Keywords
  • Identifiers
  • Constants
  • String Literals
  • Operators
  • Separators

πŸ”‘ Keywords

Keywords are predefined, reserved words in C that have special meaning. These words cannot be used as identifiers (names of variables, functions, etc.). Examples of keywords include:

int, float, double, char, if, else, while, return, for, break, continue, switch
  

πŸ–ŠοΈ Identifiers

Identifiers are the names used to identify variables, functions, arrays, or any other user-defined item. The rules for naming identifiers are:

  • 1. An identifier can contain letters (A-Z, a-z), digits (0-9), and underscores (_).
  • 2. The first character must be a letter or an underscore.
  • 3. Identifiers are case-sensitive (e.g., age and Age are different).

πŸ’¬ Constants

Constants are fixed values that do not change during the execution of the program. In C, constants can be of various types:

  • Integer Constants: Whole numbers, e.g., 10, 100.
  • Floating-Point Constants: Numbers with decimal points, e.g., 3.14, 2.718.
  • Character Constants: A single character enclosed in single quotes, e.g., 'A', 'b'.

πŸ’» String Literals

String literals are a sequence of characters enclosed in double quotes. For example:

"Hello, World!"
"Welcome to C programming!"
  

βž• Operators

Operators in C are symbols that perform operations on variables and values. There are different types of operators in C, including:

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, >, <, >=, <=
  • Logical Operators: &&, ||, !
  • Assignment Operator: =

πŸ” Separators

Separators are symbols that separate statements and parts of the program. The common separators are:

  • Comma (,): Used to separate items in a list.
  • Semicolon (;): Used to terminate a statement.
  • Parentheses ( () ): Used for grouping or to define a function's parameters.
  • Braces ( {} ): Used to define a block of code.

πŸ“ˆ Example: Tokens in a C Program

Here’s an example program with various tokens:

#include    // Header File

int main() {  // Function Start
    int num = 5;   // Keyword, Identifier, Constant
    printf("Value: %d", num);   // Keyword, Identifier, String Literal, Operator
    return 0;   // Keyword, Operator
}
  

πŸ“ Try It Yourself

  1. Identify the different tokens used in the following code:
  2. int a = 10;
    char b = 'A';
    float result = a * 2.5;
    printf("Result: %f", result);
          
  3. Write a program that prints a string literal and an integer constant.

❌ Common Mistakes

  • 🚫 Using a keyword as an identifier (e.g., using int as a variable name).
  • 🚫 Forgetting to terminate statements with a semicolon.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review