Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Variables
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 6 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 6 of 50CamboFreelanceJune 19, 2026

C++ Variables

Learn how to declare and use variables in C++. Understand variable types, naming rules, and how to store different kinds of data.


Tutorials › C++ › Variables
Beginner7 min readLesson 6 of 50

In This Lesson

  • Declaring Variables
  • Variable Types
  • Naming Rules
  • Multiple Variables
  • Quiz

What is a Variable?

A variable is a named storage location in memory that holds a value. You must declare the variable type before using it.

Syntax

type variableName = value;

Example

#include <iostream>
using namespace std;

int main() {
    int age = 25;
    double price = 9.99;
    char grade = 'A';
    bool isStudent = true;
    string name = "Alex";

    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Price: " << price << endl;
    cout << "Grade: " << grade << endl;
    cout << "Student: " << isStudent << endl;
    return 0;
}

Output:

Name: Alex
Age: 25
Price: 9.99
Grade: A
Student: 1

Common Variable Types

TypeDescriptionExample
intInteger numbersint x = 10;
doubleDecimal numbers (high precision)double pi = 3.14;
floatDecimal numbers (less precision)float f = 3.14f;
charSingle characterchar c = 'A';
boolTrue or falsebool b = true;
stringText (sequence of characters)string s = "Hi";

Naming Rules

  • Names can contain letters, digits, and underscores.
  • Must start with a letter or underscore (not a digit).
  • Case-sensitive: age and Age are different variables.
  • Cannot use C++ reserved keywords (e.g., int, return).
int myAge = 30;       // valid
int _count = 0;       // valid
int 1name = 5;        // INVALID — starts with digit
int int = 10;         // INVALID — reserved keyword

Declare Multiple Variables

int x = 1, y = 2, z = 3;
cout << x + y + z;    // Output: 6
Note: In C++, string requires the #include <string> header, but it is usually included automatically via #include <iostream>.

Exercise

Declare variables to store: a product name (string), price (double), quantity (int), and whether it is in stock (bool). Print all four values.

Show Solution
#include <iostream>
using namespace std;

int main() {
    string productName = "Laptop";
    double price = 999.99;
    int quantity = 10;
    bool inStock = true;

    cout << "Product: " << productName << endl;
    cout << "Price: $" << price << endl;
    cout << "Quantity: " << quantity << endl;
    cout << "In Stock: " << inStock << endl;
    return 0;
}

Quiz

  1. Which variable name is valid in C++?

    • A) 2score
    • B) my-score
    • C) my_score
    • D) return
    Answer

    C) my_score — underscores are allowed; hyphens are not; names cannot start with digits; reserved words cannot be used.

  2. What type stores a single character in C++?

    • A) string
    • B) int
    • C) char
    • D) letter
    Answer

    C) char

Summary

  • Variables are declared as type name = value;
  • Common types: int, double, char, bool, string.
  • Names are case-sensitive and cannot start with a digit.
  • Multiple variables of the same type can be declared on one line.
← Previous: Comments Next: C++ Data Types →
c++variablesdata-typesbeginner
PreviousLesson 5: C++ CommentsNextLesson 7: C++ Data Types
Back to All Tutorials