C, C++, MFC  

Chapter 2: Variables, Data Types, and Basic I/O in C++

In Chapter 1, you learned how to display static text. To create useful, dynamic programs, we need a way to store and manipulate information. This chapter introduces the core concepts of variables and data types in C++, along with the essential tools for getting input from the user.

1. Variables and Data Types: Storing Information

A variable is a named storage location in your computer's memory that holds a value. In C++, variables must be declared with a specific data type before they can be used. The data type determines the size of the memory allocated and the kind of value (integer, character, decimal, etc.) that can be stored there.

Fundamental C++ Data Types

Data TypeDescriptionExample
intIntegers (whole numbers)10, -500
floatSingle-precision floating-point numbers (decimals)3.14f, 0.0
doubleDouble-precision floating-point numbers (more precise decimals)1.618
charSingle characters'A', 'z', '5'
boolBoolean values (true or false)true, false

2. Declaring, Initializing, and Naming Variables

Declaration

To declare a variable, specify its type followed by its name:

int age;           // Declares an integer variable named age
double price;      // Declares a double-precision variable named price
bool is_ready;     // Declares a boolean variable

Initialization

It is best practice to initialize variables immediately upon declaration to avoid unpredictable results (known as "garbage values").

// Method 1: Copy Initialization (C-like)
int quantity = 5;

// Method 2: Direct Initialization (preferred)
double interest_rate(0.05);

// Method 3: Uniform Initialization (modern C++ standard)
char initial{'J'};

Variable Naming Rules

  • Must begin with a letter or an underscore (_).

  • Can only contain letters, numbers, and underscores.

  • Are case-sensitive (Age is different from age).

  • Cannot be a C++ keyword (e.g., you can't name a variable int).

3. Basic Input/Output (I/O) with std::cin

While std::cout (standard output) sends data to the console, std::cin (standard input) reads data entered by the user from the console.

The Extraction Operator (>>)

The extraction operator (>>) pulls data out of the input stream and stores it in the specified variable.

#include <iostream>
#include <string> // Need this for using std::string

int main() {
    int favorite_number;
    std::string name;

    // Get input for the name
    std::cout << "Please enter your name: ";
    std::cin >> name; // Extracts text until a whitespace is encountered

    // Get input for the number
    std::cout << "Hello, " << name << "! Enter your favorite number: ";
    std::cin >> favorite_number;

    std::cout << "Your favorite number squared is: " << favorite_number * favorite_number << std::endl;

    return 0;
}

4. Defining Constants in C++

Sometimes you need a value that should never change throughout the program's execution (e.g., the value of $\pi$ or a fixed tax rate). This is where constants come in.

  • Using const: The const keyword is the most common way to declare a constant. It must be initialized when declared.

const double PI = 3.14159;
const int MAX_USERS = 100;

// PI = 3.0; // ERROR! You cannot change a const variable.

Using constants improves code readability and prevents accidental modification of critical values.