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).

Goal: Write a valid, compilable C++ program.

Notes

1. Program Structure: The Anatomy

Hover over the concepts on the left to see where they live inside the code.

1
Preprocessor Directives

Lines starting with #. They tell the compiler to load libraries (like iostream) before the program starts.

2
The main() Function

Every C++ program has exactly one main(). It executes the code and returns an integer (0) to signal success.

3
Statements & Semicolons

The "Full Stop" of programming. Every instruction must end with a ; or the program crashes.

4
White Spaces

Spaces, tabs, and newlines. The compiler usually ignores them, but they make code readable for humans.

main.cpp
#include <iostream>
using namespace std;

int main() { // Start
  // White Space
  cout << "Hi C++";
  return 0;
}

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.

int   float   double
char   void   return
Identifiers

Names for variables, constants, and functions. They can be Predefined (like cout) or User-defined.

myValue   _temp
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 CamelCase or snake_case instead.
  • Case Sensitivity: C++ is strict. taxRate and TAXRATE are two different variables.

Identifier Validator

Type a variable name below to test if it passes the compiler check.

Waiting for input...

4. Essential Concepts & Best Practices

Key takeaways from the lecture notes.

The Rules vs. The Meaning
Syntax

The grammar. If you break these rules (like missing a ;), the code won't compile.

Semantics

The logic. The code might run, but does it do what you intended?

Predefined Identifiers

Words like cout and cin are not reserved words.

Risky Business:

You can redefine them, but you shouldn't. It confuses the compiler!

Naming Best Practices
  • Be Descriptive: Use hourly_rate instead of x.
  • Length Limit: Keep identifiers under 25 characters for readability.
Documentation

Use comments to explain your logic. The compiler ignores them.

// This is a single line comment
int age; // Variable for age
What exactly is a Program?
Computer Program A sequence of statements designed to accomplish a specific task.
Programming Language The set of rules, symbols, and special words used to write those programs.

Module 1 Knowledge Check

Test your understanding of C++ basics below.

1. Which function must exist in every C++ program?

Explanation: Every C++ program must have exactly one function named main(). This is where the program execution starts.

2. Which of the following is a C++ reserved word?

Explanation: int is a keyword used to define integer variables. sum, value, and total are typically identifiers chosen by programmers.

3. Which of the following is a valid identifier in C++?

Explanation: Identifiers cannot start with a number (2number), cannot contain hyphens (total-sum), and cannot contain spaces (total sum). Underscores (_) are allowed.

4. C++ identifiers are case-sensitive. Which statement is TRUE?

Explanation: C++ is case-sensitive, meaning it treats VALUE and value as two completely separate variables.

5. Which of the following best describes a token in C++?

Explanation: A token is the smallest individual element of a C++ program that is meaningful to the compiler, such as keywords, symbols, and identifiers.