Mastering Module 1
Program Structure, Tokens & Identifiers
Video Overview
Before we write complex logic, we must understand the "Grammar" of C++. In this module, we dissect a program to see its organs (Structure), its atoms (Tokens), and its naming rules (Identifiers).
Notes
1. Program Structure: The Anatomy
Hover over the concepts on the left to see where they live inside the code.
Preprocessor Directives
Lines starting with #. They tell the compiler to load libraries (like iostream) before the program starts.
The main() Function
Every C++ program has exactly one main(). It executes the code and returns an integer (0) to signal success.
Statements & Semicolons
The "Full Stop" of programming. Every instruction must end with a ; or the program crashes.
White Spaces
Spaces, tabs, and newlines. The compiler usually ignores them, but they make code readable for humans.
2. The Smallest Units: Tokens
Just as atoms are the building blocks of matter, Tokens are the smallest individual units meaningful to the compiler.
Special Symbols
Mathematical operators and punctuation marks. Note that a Blank space is also considered a special symbol.
<= != == >=
Multi-character symbols like != are treated as one single token.
Word Symbols
Also called Reserved Words. They are always lowercase and have special meaning. You cannot use them as variable names.
char void return
Identifiers
Names for variables, constants, and functions. They can be Predefined (like cout) or User-defined.
result2 printData
Best Practice: Use suggestive names (e.g., hourly_rate) rather than just x.
3. Naming Rules (The Legalities)
Identifiers are strict. Following syntax rules is mandatory; breaking them causes compiler errors.
-
The First Character: Must be a letter (a-z, A-Z) or an underscore (
_). It cannot start with a number. -
Characters Allowed: Only letters, digits, and underscores. No hyphens (
-) or symbols like@, #, %. -
No Spaces: Spaces are never allowed. Use
CamelCaseorsnake_caseinstead. -
Case Sensitivity: C++ is strict.
taxRateandTAXRATEare two different variables.
Identifier Validator
Type a variable name below to test if it passes the compiler check.
4. Essential Concepts & Best Practices
Key takeaways from the lecture notes.
The Rules vs. The Meaning
The grammar. If you break these rules (like missing a ;), the code won't compile.
The logic. The code might run, but does it do what you intended?
Predefined Identifiers
Words like cout and cin are not reserved words.
You can redefine them, but you shouldn't. It confuses the compiler!
Naming Best Practices
-
Be Descriptive: Use
hourly_rateinstead ofx. - Length Limit: Keep identifiers under 25 characters for readability.
Documentation
Use comments to explain your logic. The compiler ignores them.
int age; // Variable for age
What exactly is a Program?
Module 1 Knowledge Check
Test your understanding of C++ basics below.