Lesson 5: Comments, Tokens, and Basic Syntax Rules
Comments
Comments are non-executable text used to explain the code. They are ignored by the compiler.
1. Single-Line Comments (C99 and later)
c // This is a single-line comment int x = 10; // Inline explanation
2. Multi-Line Comments (C89/ANSI C)
c /* This is a multi-line comment. It can span several lines and is useful for block documentation. */
C Tokens
A token is the smallest individual unit in a C program. Tokens are categorized as:
- Keywords: Reserved words that have predefined meanings (e.g.,
int,if,while). - Identifiers: Names given to variables, functions, arrays, etc. (Must start with a letter or underscore, cannot be a keyword).
- Constants: Fixed values that do not change during execution (e.g.,
10,3.14,'A') - Strings: Sequences of characters enclosed in double quotes (e.g.,
"Hello"). - Operators: Symbols used for operations (e.g.,
+,=,*). - Separators/Punctuators: Symbols used to group or separate things (e.g.,
{,},;,().
Syntax Rules
- Case Sensitivity: C is case-sensitive.
MyVariableis different frommyvariable. - Statement Termination: Every statement (instruction) must end with a semicolon (
;). - Whitespace: Whitespace (spaces, tabs, newlines) is generally ignored by the compiler, allowing for flexible code formatting, except within string literals or tokens.