🟢 Foundation  ·  Lesson 07

Variables and Data Types in C++

Variables and Data Types in C++

What is Variables and Data Types in C++?

Variables and Data Types in C++
A variable is a named memory location used to store data. Data type tells the compiler what kind of value will be stored and how much memory may be needed.
Level
🟢 Beginner – C++ Foundation
Example File
variables-data-types.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
intInt used in Variables and Data Types in C++ programming.
floatFloat used in Variables and Data Types in C++ programming.
doubleDouble used in Variables and Data Types in C++ programming.
charChar used in Variables and Data Types in C++ programming.
boolBool used in Variables and Data Types in C++ programming.
stringString used in Variables and Data Types in C++ programming.

Syntax / Pattern

data_type variable_name = value;

Example Program

#include <iostream>
using namespace std;

int main() {
    int age = 16;
    double percentage = 92.5;
    char grade = 'A';
    bool passed = true;

    cout << age << " " << percentage << " " << grade << " " << passed;
    return 0;
}

Expected Output

16 92.5 A 1

Program Explanation

  • bool true is printed as 1 by default.
  • double stores decimal values with better precision than float.
  • char stores a single character.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • student records
  • fee calculation
  • marks and percentage
  • conditions

Common Mistakes

  • Using int for decimal values.
  • Using single quotes for full strings.
  • Using invalid variable names with spaces.

Practice Tasks

  1. Create variables for roll number, name, marks and grade.
  2. Print size of different data types using sizeof().

Summary

Variables and Data Types in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

What is Variables and Data Types in C++?

Variables and Data Types in C++
A variable is a named memory location used to store data. Data type tells the compiler what kind of value will be stored and how much memory may be needed.
Level
🟢 Beginner – C++ Foundation
Example File
variables-data-types.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
intInt used in Variables and Data Types in C++ programming.
floatFloat used in Variables and Data Types in C++ programming.
doubleDouble used in Variables and Data Types in C++ programming.
charChar used in Variables and Data Types in C++ programming.
boolBool used in Variables and Data Types in C++ programming.
stringString used in Variables and Data Types in C++ programming.

Syntax / Pattern

data_type variable_name = value;

Example Program

#include <iostream>
using namespace std;

int main() {
    int age = 16;
    double percentage = 92.5;
    char grade = 'A';
    bool passed = true;

    cout << age << " " << percentage << " " << grade << " " << passed;
    return 0;
}

Expected Output

16 92.5 A 1

Program Explanation

  • bool true is printed as 1 by default.
  • double stores decimal values with better precision than float.
  • char stores a single character.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • student records
  • fee calculation
  • marks and percentage
  • conditions

Common Mistakes

  • Using int for decimal values.
  • Using single quotes for full strings.
  • Using invalid variable names with spaces.

Practice Tasks

  1. Create variables for roll number, name, marks and grade.
  2. Print size of different data types using sizeof().

Summary

Variables and Data Types in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial