Skip to main content
Course/Module 1/Topic 2 of 4Beginner

Variables & Data Types

Learn C++ data types: int, float, double, char, bool, string. Declare variables and output them with cout.

25 minBy Priygop TeamLast updated: Feb 2026

Variables in C++

C++ is statically typed — you must declare the type of every variable. C++ adds the bool type (true/false) and the string class (from <string>) compared to C. Variables must be declared before use. Use descriptive names and camelCase or snake_case style.

C++ Data Types

  • int — Whole numbers: int age = 25;
  • float — Decimal (less precise): float price = 9.99f;
  • double — Decimal (more precise): double pi = 3.14159;
  • char — Single character: char grade = 'A';
  • bool — True or false: bool isStudent = true;
  • string — Text (from <string>): string name = "Alice";
  • auto — Let compiler deduce type: auto x = 42;

Variables Example

Example
#include <iostream>
#include <string>
using namespace std;

int main() {
    // Declaring variables
    string name = "Alice";
    int age = 25;
    double height = 5.6;
    bool isStudent = true;
    char grade = 'A';

    // Output with cout
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Height: " << height << " feet" << endl;
    cout << "Student: " << isStudent << endl;
    cout << "Grade: " << grade << endl;

    // auto keyword
    auto score = 95;  // compiler deduces int
    cout << "Score: " << score << endl;

    return 0;
}

Try It Yourself: Variables

Try It Yourself: VariablesC++
C++ Editor
✓ ValidTab = 2 spaces
C++|24 lines|594 chars|✓ Valid syntax
UTF-8

Quick Quiz

Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep