C++ Fundamentals: Variables, Data Types, ASCII, and IO Operations

Expressions and Statements in C++

  • Expressions:

    • An expression in C++ is a combination of two basic elements: operators and operands.

    • An expression evaluates to a single computed value.

    • Operators: Symbols representing operations such as addition (++), subtraction (−-), multiplication (×\times or *), division (//), and exponentiation/power operations.

    • Operands: Values or sub-expressions acted upon by operators. An operand can be a single literal value (e.g., 1010) or another nested expression.

    • Evaluation Example: The expression 10+2010 + 20 evaluates directly to 3030. Expressions can be chained sequentially, such as 10+20+3010 + 20 + 30, which computes to 6060.

    • Interactive Evaluation: Interactive environments like Jupyter Notebook directly evaluate and display the resulting single value of an isolated expression.

  • Statements:

    • A statement in C++ is an instruction or command telling the computer to perform a specific action, rather than simply evaluating to a value.

    • Syntax Rule: Every C++ statement must terminate with a semicolon (;).

    • Comparison across languages:

    • C++: Every statement must end with a semicolon (;). Whitespace, spaces, and line indents are ignored by the compiler.

    • Java: Uses semicolons (;) to terminate statements.

    • Python: Uses line breaks and explicit indentation to define statements instead of semicolons.

    • Statement Composition Rules:

    • A statement can contain expressions within it.

    • A statement cannot contain another statement nested inside it (a command cannot be placed directly inside another command).

    • Multiple statements can be written on a single line in C++, provided each statement ends with a semicolon (;).

Data Types and Memory Allocation

  • Variable Declarations:

    • Declaring a variable instructs the computer to allocate a specific section of memory to store data for later use.

    • C++ is a statically typed language; every variable must have its data type explicitly declared before it can be used.

    • Unlike dynamically typed languages like Python (where x = 5 infers the type automatically), C++ requires specifying whether the value is an integer, a floating-point number, a character, or a boolean.

  • Primitive Data Types:

    • A primitive data type represents a single fundamental value that the computer can process directly.

    • Boolean (bool):

    • Can only hold one of two possible values: true or false.

    • At the hardware level, computers operate on binary (11s and 00s).

    • Binary mapping: 1=true1 = \text{true} and 0=false0 = \text{false}.

    • C++ evaluates any non-zero value containing a 11 as true. Only exact 00 evaluates to false (e.g., evaluating integer 22 in a boolean context yields true).

    • Requires the absolute minimum amount of memory storage (1 bit theoretically, typically stored in 1 byte).

    • Character (char):

    • Represents any individual single symbol, including letters, digits, punctuation, or white spaces (e.g., hitting the spacebar produces a single space character ' ').

    • Must always be enclosed in single quotes (e.g., 'a', 'b', '&', '1'). Using double quotes for characters is invalid.

    • Requires 1 byte of memory storage.

    • Integer (int):

    • Stores whole numbers, positive or negative, without decimal places (e.g., 11, 423423, −6-6).

    • Has finite upper and lower bounds due to fixed memory block sizes (e.g., cannot store extremely large numbers like 10300010^{3000}).

    • Consumes 4 bytes of memory storage in standard implementations (four times the memory of a char).

    • Floating-Point Types (float and double):

    • Store rational numbers containing decimal points (e.g., 1.01.0, 0.540.54, 43.443.4).

    • Strings (Non-Primitive Type):

    • A string is a contiguous collection or sequence of multiple characters.

    • Strings are not primitive data types because they cannot be represented as a single numeric value in memory; they are composite data structures built from individual characters.

  • Variable Assignment and Memory Concepts:

    • Declaration and Assignment Syntax:

    • bool y = true;

    • int x = 36 * 2; (Evaluates 36×2=7236 \times 2 = 72 using the asterisk operator *, then stores 7272 in variable x).

    • char a = 'w';

    • Assignment Operator (=) vs Mathematical Equality:

    • The = symbol in C++ is an assignment operator, not an equation of equivalence.

    • It evaluates the expression on the right-hand side first, then stores the resulting value into the memory address defined by the variable on the left-hand side.

    • Uninitialized Variables:

    • Declated variables without an assigned initial value (e.g., char b, c;) allocate memory space, but the contents at that memory location are unknown (containing garbage memory or null representations like \x00).

    • Importance of Explicit Memory Allocation:

    • Declaring data types allows the computer to allocate exact memory blocks.

    • Using precise data types avoids memory inefficiency, which is essential in constrained high-performance computing environments (e.g., running massive AI models on GPUs).

ASCII Character Representation and Manipulation

  • ASCII Standard:

    • ASCII stands for American Standard Code for Information Interchange.

    • Since computers store data exclusively as binary numbers (00s and 11s), every character in C++ is mapped internally to an integer ASCII value ranging from 00 to 255255 (256256 total values).

  • ASCII Values for Key Characters:

    • '!' (Exclamation Mark) = 3333

    • '$' (Dollar Sign) = 3434

    • 'Z' (Capital Z) = 9090

    • ''` (Apostrophe) = 9696

    • 'a' (Lowercase a) = 9797

  • Character Arithmetic Examples:

    • Performing arithmetic on a character operates directly on its underlying integer ASCII value.

    • If char a = '!'; (ASCII value 3333), evaluating a - 2 computes 33−2=3133 - 2 = 31.

    • If char a = '$'; (ASCII value 3434), evaluating a - 2 computes 34−2=3234 - 2 = 32.

    • Variable reassignment update trace:

    • int x = 36 * 2; (x=72x = 72)

    • x = x + 10; (72+10=8272 + 10 = 82)

    • x + 5; (82+5=8782 + 5 = 87)

  • Algorithm for Finding the xx-th Letter of the Alphabet:

    • Problem: Given an integer xx (where 1 to 261 \text{ to } 26 represents alphabet positions), output the corresponding letter (e.g., x=5→’e’x = 5 \rightarrow \text{'e'}, x=7→’g’x = 7 \rightarrow \text{'g'}, x=26→’z’x = 26 \rightarrow \text{'z'}).

    • Method 1 (Using Base Constant ASCII Value):

    • Since 'a' has an ASCII value of 9797, the character immediately preceding 'a' is the apostrophe ''` with ASCII value 9696.

    • Formula: 96+x96 + x

    • Example for x=7x = 7: 96+7=10396 + 7 = 103, which corresponds to 'g'.

    • Method 2 (Using Character Offset Math - Preferred):

    • Avoids hardcoded