2025年11月7日金曜日

what are the tokens in c?

 The tokens in the C programming language are the smallest individual units that the compiler recognizes.1 They are the basic vocabulary of C.2

The six types of tokens recognized by a C compiler are:


1. Keywords (Reserved Words) 🔑3

These are words that have a predefined meaning to the C compiler. They cannot be used as identifiers (variable names, function names, etc.).4

  • Examples: int, float, if, else, for, while, return, void, const, static, struct, etc.5 (There are 32 standard keywords in the C99 standard).6


2. Identifiers 🏷️

These are names given by the programmer to entities like variables, functions, arrays, and user-defined data types.7

  • Rules:

    • Must start with a letter (a-z, A-Z) or an underscore (_).8

    • Can be followed by letters, digits (0-9), or underscores.9

    • They are case-sensitive (e.g., Count is different from count).10


3. Constants (Literals) 🔢

These represent fixed values that do not change during program execution.11

  • Integer Constants: Whole numbers (e.g., 10, -500).12

  • Floating-Point Constants: Numbers with decimal parts (e.g., 3.14, 1.2e-5).13

  • Character Constants: A single character enclosed in single quotes (e.g., 'A', '\n').14

  • String Constants: A sequence of characters enclosed in double quotes (e.g., "Hello World").15


4. Operators 🧮

These symbols perform specific operations when placed between operands.16

  • Examples: Arithmetic (+, -, *, /), Relational (<, >), Logical (&&, ||), Assignment (=), Increment/Decrement (++, --).17


5. Punctuators (Separators/Delimiters) 🛑

These symbols are used to organize the structure of the program code.

  • Examples:

    • Semicolon (;): Terminates a statement.18

    • Curly Braces ({, }): Define the scope of functions and blocks.19

    • Parentheses ((, )): Used for function arguments and controlling expression precedence.20

    • Square Brackets ([, ]): Used for array indexing.

    • Comma (,): Used to separate items in lists (e.g., function arguments or multiple variable declarations).21

    • Colon (:): Used in switch statements and labeled statements.22


6. Identifiers (Preprocessor Directives) ⚙️

These are instructions for the preprocessor, which acts before the compiler translates the code.23

  • Examples: #include, #define, #if.


Understanding how the compiler tokenizes your code helps in grasping syntax rules. I can search for a visualization showing how a simple C statement is broken down into these tokens.

The tokens in C are the smallest meaningful units of a program that the compiler processes. Think of them as the individual words and punctuation marks of the C language.

There are six fundamental categories of tokens in C:


1. Keywords (Reserved Words) 🔑

These are words that the C standard reserves for specific, built-in meanings. They cannot be used for other purposes like naming variables.

  • Examples: int, if, else, for, return, void, struct, while.


2. Identifiers 🏷️

These are the names you choose for your program elements, such as variables, function names, or array names.

  • Rules: They must start with a letter (A-Z, a-z) or an underscore (_). Subsequent characters can be letters, digits (0-9), or underscores. Identifiers are case-sensitive (MyVar is different from myvar).


3. Constants (Literals) 🔢

These are fixed values that are written directly into the code and do not change during runtime.

  • Integer Constants: Whole numbers like 100, -45.

  • Floating-Point Constants: Numbers with decimals like 9.81, 3.14f.

  • Character Constants: A single character enclosed in single quotes, like 'x' or '\n' (the newline character).

  • String Constants: A sequence of characters enclosed in double quotes, like "Enter a value".


4. Operators 🧮

These symbols perform operations on operands (variables or constants).

  • Examples: Arithmetic (+, -, *, /), Comparison (==, >), Logical (&&, ||), Assignment (=).


5. Punctuators (Delimiters) 🛑

These symbols provide structure and define the syntax boundaries within the code.

  • Semicolon (;): Marks the end of a statement.

  • Parentheses (( )): Used for function calls and controlling expression evaluation order.

  • Curly Braces ({ }): Define the start and end of code blocks (like functions or loops).

  • Comma (,): Separates multiple items in a list (e.g., arguments in a function call).


6. Preprocessor Directives ⚙️

These are instructions processed by the C preprocessor before the compiler sees the code. They all start with the hash symbol (#).

  • Examples: #include <stdio.h> (to bring in the standard I/O library) and #define PI 3.14 (to define a constant).


To illustrate how the compiler breaks down a line of code, here is a conceptual look at tokenization:

int count = 10;

This single line is recognized by the compiler as a sequence of these tokens:

  1. int $\rightarrow$ Keyword

  2. count $\rightarrow$ Identifier

  3. = $\rightarrow$ Operator (Assignment)

  4. 10 $\rightarrow$ Constant (Integer Literal)

  5. ; $\rightarrow$ Punctuator (Statement terminator)

Would you like to explore how these tokens are combined with control structures to form the logic of a C program?

0 件のコメント:

コメントを投稿