Introduction to C++ Programming: Identifiers, Variables, Data Types, and Operators

Fundamentals of C++: Identifiers and Variable Management

  • Definition of an Identifier: An identifier is a name assigned to various parts of a program, such as variables, functions, and other user-defined items.

  • Rules for Constructing Identifiers:

    • First Character: Must be an alphabetic character (a-z, A-Z) or an underscore ( _ ). It cannot begin with a numeric digit.
    • Subsequent Characters: After the initial character, identifiers may consist of letters, digits (00-99), or underscores.
    • Case Sensitivity: C++ is case-sensitive. For example, Sem and sem are treated as two distinct variables.
    • Reserved Keywords: Identifiers cannot be any of the C++ reserved keywords (e.g., int, float, void). These words have predefined meanings in the language.
  • Identifier Validation Examples:

    • totalSales: Valid (standard camelCase).
    • total_Sales: Valid (uses underscore).
    • total.Sales: Invalid (contains a period . which is not permitted).
    • 4thQtrSales: Invalid (starts with a numeric digit 44).
    • totalSale$: Invalid (contains the special character $ which is not permitted).

Variables and Memory Allocation

  • Definition of a Variable: A variable is a named location in the computer's memory designated to hold data.

  • Key Characteristics:

    • It possesses a specific name (identifier).
    • It has a specific data type that defines the kind of data it can store.
    • Pre-declaration Requirement: In C++, a variable must be declared (defined) before it can be utilized within the program.
  • Variable Declaration Syntax:

    • Format: data type variable name;
    • Single declaration example: int semester;
    • Multiple declarations on one line: float radius = 2, pi = 3.142, area;

Primary Data Types in C++

  • Integer Types (int / long): Used for storing whole numbers without decimals.
    • Examples: 11, 100100, 50-50.
  • Floating-Point Types (float / double): Used for storing real numbers or numbers with fractional parts.
    • Examples: 12.4512.45, 3.8-3.8, 3.1423.142.
  • Character Type (char): Used to store individual characters.
    • Examples in transcript: char name;, char name[21]; (character array for names), char address[30];.
    • Note: The transcript lists ali as an example for char, though char typically holds a single character unless defined as an array or string.
  • Boolean Type (bool): Used to store logical values of true or false.
    • Representation: true is often represented as 11 and false as 00.
    • Examples: bool allDone = true;, bool finished = false;.

Special Characters and Syntax Symbols

  • // (Double Slash): Marks the beginning of a single-line comment.
  • # (Pound Sign): Indicates the beginning of a preprocessor directive (e.g., #include).
  • < > (Open/Close Brackets): Used to enclose filenames within a #include directive.
  • ( ) (Open/Close Parentheses): Used during the naming and calling of functions.
  • { } (Open/Close Brace): Encloses a group of program statements, such as the body of a function.
  • " " (Quotation Marks): Encloses a string of characters (string literals).
  • ; (Semicolon): Marks the end of a programming statement.

C++ Input and Output Statements

  • Output Statement (cout):

    • Purpose: Displays text or data on the computer screen.
    • Operator: Uses the stream insertion operator <<.
    • Syntax: cout << "Text content";
    • Examples:
      • cout << "UPSI"; results in: UPSI
      • cout << "FSKIK" << endl; (Moves the cursor to a new line after printing).
    • New Line Manipulators:
      • endl: Starts a new line of output.
      • \n: Escape sequence for starting a new line. It is placed inside the double quotes (e.g., cout << "UPSI \n";).
  • Input Statement (cin):

    • Purpose: Reads data entered by the user via the keyboard.
    • Operator: Uses the stream extraction operator >>.
    • Syntax: cin >> variableName;
    • Examples: cin >> semester;, cin >> cgpa;.
  • User Interaction (Prompts):

    • A prompt is a message that instructs a user on what data to enter.
    • Best Practice: Always use a cout statement to display a prompt immediately before a cin statement.
    • Example: cpp cout << "What is your semester? "; cin >> semester; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Operators in C++

  • Arithmetic Operators: Used for numeric mathematical calculations.

    • Modulus (%): Computes the remainder resulting from integer division.
    • Modulus Example: cout << 13 % 5; will display 33.
    • Restriction: The modulus operator requires both operands to be integers. Using a floating-point number (e.g., 13 % 5.0) will result in a compilation error.
  • Assignment Operator (=):

    • Purpose: To store a specific value in a variable.
    • Example: int semester = 1; (This initializes the variable semester with the value 11).

Program Development and Logic

Simple Program Structure
#include <iostream>
using namespace std;

void main()
{
    // statements go here
    cout << "Hello there";
}
Application Exercise: Calculating the Area of a Circle
  • Given Constraints:

    • radius = 22
    • pi = 3.1423.142
    • Formula: area=pi×radius×radius\text{area} = \text{pi} \times \text{radius} \times \text{radius}
  • Development Steps:

    1. Define the Problem: Read and understand the requirements.

    2. Analysis:

      • Input: radius
      • Process: area=pi×radius×radius\text{area} = \text{pi} \times \text{radius} \times \text{radius}
      • Output: area
    3. Design (Pseudo-code):

      • START
      • SET radius = 22, area = 00, pi = 3.1423.142
      • area = pi * radius * radius
      • PRINT area
      • END
    4. Implementation (Coding): ```cpp // sample C++ program

      include

      using namespace std;

      void main() { float radius = 2, pi = 3.142, area; area = pi * radius * radius; cout << "Area of the circle is: " << area; }         ```

  • Expected Output: Area of the circle is: 12.568