Loading...

Variables, Data Types, and auto in C++

Learn variables, data types, and the auto keyword in C++ with practical examples of type deduction and memory usage.

In C++ programs, variables are used to store and process data. Each variable has a data type, which determines how much memory it occupies and what kind of values it can hold. In this article, we’ll learn about the basic data types in C++, the rules for defining variables, and how to use the auto keyword introduced in modern C++.


1. What Is a Variable?

A variable is a named location in memory that stores a value. During program execution, you can read or modify this value.


int age = 25;
double pi = 3.14159;
char letter = 'A';
bool active = true;

Each variable has a specific type and occupies a different amount of memory.


2. Basic Data Types

The fundamental data types in C++ are as follows:

Data TypeDescriptionExample
intInteger numbers (positive or negative)int number = 42;
doubleFloating-point numbersdouble ratio = 0.75;
charSingle characterchar symbol = 'X';
boolLogical value (true / false)bool active = true;
stringText (sequence of characters)string name = "Ahmet";

The string type in C++ comes with the <string> library:


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

int main() {
    string name = "Ali";
    cout << "Hello " << name << endl;
    return 0;
}

3. Rules for Defining Variables

Valid and invalid examples:


// Valid
int student_count = 30;
double totalPrice = 99.50;

// Invalid
int 2apple = 5;      // cannot start with a number
int sınıf = 10;      // non-ASCII character not allowed
int double = 7;      // cannot use reserved keyword

4. Constants (const)

If you want a variable’s value to remain unchanged throughout the program, use the const keyword.


const double PI = 3.14159;
const int MAX_STUDENTS = 40;

// PI = 3.14;  // error! const variables cannot be modified

5. Type Casting

In C++, it’s possible to convert a value from one type to another.


int x = 10;
double y = 3.0;
double result = x / y; // automatic conversion (int → double)
cout << result; // 3.33333

You can also perform explicit type conversion using static_cast:


int total = 5;
int count = 2;
double average = static_cast<double>(total) / count;
cout << average; // 2.5

6. The auto Keyword (C++11+)

Introduced in modern C++, the auto keyword allows the compiler to automatically infer the variable’s type from the assigned value.


auto number = 10;       // int
auto ratio = 3.14;      // double
auto text = "Hello";    // const char*
auto active = true;     // bool

This feature improves code readability, especially when working with complex types (such as iterators or template-based types).


7. The decltype Keyword (C++11+)

decltype allows you to declare a new variable of the same type as an existing expression.


int a = 5;
decltype(a) b = 10; // b is also of type int

When used together with auto, it allows flexible and type-safe programming.


8. Example: Calculating Average Grade

The following example demonstrates different data types, the use of auto, and type casting all together.


#include <iostream>
using namespace std;

int main() {
    int grade1 = 85;
    int grade2 = 90;
    int grade3 = 78;

    auto average = static_cast<double>(grade1 + grade2 + grade3) / 3;
    cout << "Average: " << average << endl;

    return 0;
}

9. TL;DR

  • Every variable has a type; the type defines its memory size and value range.
  • const variables cannot be modified.
  • static_cast<> provides safe type conversion.
  • auto automatically determines the variable’s type.
  • Examples can be run using Visual Studio 2022 or GCC 11+.

Related Articles