Learn with Yasir

Share Your Feedback

Programming in C++ – Complete Beginner’s Guide with Examples, Exercises & Downloadable PDFs


Learn C++ from scratch with clear explanations, practical code examples, hands-on exercises, and downloadable PDF lessons. Covers variables, data types, operators, control flow, functions, iostream, and common pitfalls—ideal for students and self-learners.

Download PDF

Identifier

Keywords

Data Types


C++ Program: Understanding Data Types

Question: Write a C++ program to demonstrate different data types (int, float, double, char, bool, string) and print example values for each.

#include <iostream>
using namespace std;

int main() {
    // Integer: whole numbers
    int age = 20;
    cout << "Integer example (age): " << age << endl;

    // Floating-point: numbers with decimal
    float height = 5.9;
    double weight = 70.5;
    cout << "Float example (height): " << height << endl;
    cout << "Double example (weight): " << weight << endl;

    // Character: single letter or symbol
    char grade = 'A';
    cout << "Character example (grade): " << grade << endl;

    // Boolean: true or false
    bool isStudent = true;
    cout << "Boolean example (isStudent): " << isStudent << endl;

    // String: sequence of characters
    string name = "Ali";
    cout << "String example (name): " << name << endl;

    return 0;
}

Explanation

  1. int → Stores whole numbers like 1, 10, -5.
  2. float → Stores decimal numbers, e.g., 3.14.
  3. double → Similar to float but with more precision.
  4. char → Stores a single character like 'A' or 'x'.
  5. bool → Stores true or false.
  6. string → Stores text or words like "Hello".

Sample Output

Integer example (age): 20
Float example (height): 5.9
Double example (weight): 70.5
Character example (grade): A
Boolean example (isStudent): 1
String example (name): Ali

Note: Boolean values print as 1 (true) or 0 (false) in C++.


✅ C++ Program: Character Data Type Example

Question: Write a C++ program to demonstrate the use of the char (character) data type and display example values.

#include <iostream>
using namespace std;

int main() {
    // Character variable
    char grade = 'A';
    char symbol = '#';
    char letter = 'Z';

    cout << "Character example 1 (grade): " << grade << endl;
    cout << "Character example 2 (symbol): " << symbol << endl;
    cout << "Character example 3 (letter): " << letter << endl;

    return 0;
}

💡 Explanation

  • The char data type is used to store a single character, such as a letter, digit, or symbol.
  • Character values are always enclosed in single quotes (‘ ‘).
  • Each character has an ASCII code (a numeric value in computer memory).

🖥️ Sample Output

Character example 1 (grade): A
Character example 2 (symbol): #
Character example 3 (letter): Z

Interger Overflow and Underflow

Here’s a beginner-friendly C++ example to explain the concept of overflow and underflow in simple terms 👇


Question: Write a C++ program to demonstrate the concept of overflow and underflow using integer data type.


✅ C++ Program: Overflow and Underflow Example

#include <iostream>
#include <limits> // for numeric_limits
using namespace std;

int main() {
    // Find the maximum and minimum values an int can hold
    int maxValue = numeric_limits<int>::max();
    int minValue = numeric_limits<int>::min();

    cout << "Maximum value of int: " << maxValue << endl;
    cout << "Minimum value of int: " << minValue << endl;

    // Overflow: adding 1 to the maximum value
    cout << "\nAfter overflow (maxValue + 1): " << (maxValue + 1) << endl;

    // Underflow: subtracting 1 from the minimum value
    cout << "After underflow (minValue - 1): " << (minValue - 1) << endl;

    return 0;
}

💡 Explanation for Beginners

  1. numeric_limits<int>::max() gives the largest value an integer can store.
  2. numeric_limits<int>::min() gives the smallest value an integer can store.
  3. Overflow happens when a value becomes larger than the maximum limit — it wraps around to the smallest value.
  4. Underflow happens when a value becomes smaller than the minimum limit — it wraps around to the largest value.

For more details about ‘numeric_limits’, See C++ numeric_limits – Get Min/Max Values for Data Types (Beginner’s Guide)


🖥️ Sample Output

Maximum value of int: 2147483647
Minimum value of int: -2147483648

After overflow (maxValue + 1): -2147483648
After underflow (minValue - 1): 2147483647

Variables

CPP example

Questions: Write a C++ program to demonstrate variable declaration, initialization

#include <iostream>
using namespace std;

int main() {
    // 1. What is a variable?
    // A variable is a name that stores a value in memory. 
    // You can change the value of a variable during program execution.

    // 2. Variable declaration and initialization
    int age = 20;          // 'int' is the type, 'age' is the variable name, 20 is the initial value
    float height = 5.9;    // float stores numbers with decimals
    char grade = 'A';      // char stores a single character
    bool isStudent = true; // bool stores true or false
    string name = "Ali";   // string stores a sequence of characters

    // 3. Printing variable values
    cout << "Age: " << age << endl;
    cout << "Height: " << height << endl;
    cout << "Grade: " << grade << endl;
    cout << "Is student? " << isStudent << endl;
    cout << "Name: " << name << endl;

    // 4. Changing variable values
    age = 21;   // Value of 'age' is updated
    name = "Ahmed"; // Value of 'name' is updated

    cout << "\nAfter updating variables:" << endl;
    cout << "Age: " << age << endl;
    cout << "Name: " << name << endl;

    /* 5. Rules for declaring variables:
        - Must start with a letter or underscore (_)
        - Can contain letters, digits, and underscores
        - Cannot start with a digit
        - Cannot use C++ keywords (like int, float, return, etc.)
        - Should have meaningful names
    */

    return 0;
}

✅ Explanation:

  • Variables store data.
  • Declaration is telling the program the type of variable (e.g., int age;).
  • Initialization is giving it a value for the first time (e.g., int age = 20;).
  • You can update the value anytime.
  • Follow the naming rules to avoid errors.

Literals

CPP example

Question: Write a C++ program to demonstrate the concept of literals, including long literals.

#include <iostream>
using namespace std;

int main() {
    // 1. Integer literal
    int age = 25;  
    cout << "Integer literal (age): " << age << endl;

    // 2. Long integer literal
    long population = 7800000000L; // 'L' specifies a long literal
    cout << "Long literal (population): " << population << endl;

    // 3. Floating-point literal
    float height = 5.9f;   // 'f' specifies a float literal
    double weight = 70.5;  // double literal by default
    cout << "Float literal (height): " << height << endl;
    cout << "Double literal (weight): " << weight << endl;

    // 4. Character literal
    char grade = 'A';  
    cout << "Character literal (grade): " << grade << endl;

    // 5. Boolean literal
    bool isStudent = true;  
    cout << "Boolean literal (isStudent): " << isStudent << endl;

    // 6. String literal
    string name = "Ali";  
    cout << "String literal (name): " << name << endl;

    // 7. Escape sequence literal
    cout << "This is a new line\nand this is the next line using a literal." << endl;

    return 0;
}

Explanation for Beginners:

  1. Literals are fixed values written directly in code.
  2. Examples:

    • 25 → integer literal
    • 7800000000L → long integer literal
    • 5.9f → float literal
    • 70.5 → double literal
    • 'A' → character literal
    • "Ali" → string literal
    • true → boolean literal
    • \n → escape sequence literal
  3. Literals cannot be changed during program execution.
  4. Use suffixes like L for long and f for float to specify the type explicitly.

Constants



CPP Example

for more details, see Difference Between Preprocessor and Compiler in C++