WikiGalaxy

Personalize

C++ Variables: Introduction

Definition of Variables

In C++, variables are used to store data that can be manipulated by the program. They are named storage locations in memory.

Types of Variables

Variables can be of different types such as int, float, char, etc., each serving different purposes and constraints.

Variable Declaration

Declaring a variable involves specifying its type followed by its name, e.g., int age;.

Variable Initialization

Initialization is when you assign a value to a variable at the time of declaration, e.g., int age = 25;.


#include <iostream>
using namespace std;

int main() {
    int age = 25;
    cout << "Age: " << age;
    return 0;
}
    

Console Output:

Age: 25

Scope of Variables

Local Variables

Local variables are declared inside a function or block and can only be accessed within that function or block.

Global Variables

Global variables are declared outside of all functions and can be accessed from any function within the program.


#include <iostream>
using namespace std;

int globalVar = 100; // Global variable

int main() {
    int localVar = 50; // Local variable
    cout << "Global: " << globalVar << ", Local: " << localVar;
    return 0;
}
    

Console Output:

Global: 100, Local: 50

Constants in C++

Defining Constants

Constants are variables whose value cannot be changed once defined. They are declared using the const keyword.


#include <iostream>
using namespace std;

int main() {
    const float PI = 3.14159;
    cout << "Value of PI: " << PI;
    return 0;
}
    

Console Output:

Value of PI: 3.14159

Data Types in C++

Primitive Data Types

C++ supports various primitive data types like int, char, float, and double, each with different memory sizes and ranges.


#include <iostream>
using namespace std;

int main() {
    int integerVar = 10;
    char charVar = 'A';
    float floatVar = 3.14;
    double doubleVar = 3.14159265359;
    cout << "Int: " << integerVar << ", Char: " << charVar;
    return 0;
}
    

Console Output:

Int: 10, Char: A

Type Conversion

Implicit Conversion

Also known as automatic conversion, it occurs when data types are automatically converted by the compiler.

Explicit Conversion

Also known as type casting, it is the conversion of one data type into another explicitly by the programmer.


#include <iostream>
using namespace std;

int main() {
    int i = 10;
    double d = i; // Implicit conversion
    double pi = 3.14;
    int intPi = (int)pi; // Explicit conversion
    cout << "Double: " << d << ", Int from double: " << intPi;
    return 0;
}
    

Console Output:

Double: 10, Int from double: 3

Variable Naming Conventions

Rules for Naming

Variable names must begin with a letter or an underscore, followed by letters, numbers, or underscores.

Best Practices

Use meaningful names that convey the purpose of the variable. Avoid single-character names except for loop counters.


#include <iostream>
using namespace std;

int main() {
    int userAge = 30;
    float accountBalance = 1000.50;
    cout << "User Age: " << userAge << ", Account Balance: " << accountBalance;
    return 0;
}
    

Console Output:

User Age: 30, Account Balance: 1000.5

Variable Lifetime

Static Variables

Static variables are initialized only once and retain their value between function calls.

Automatic Variables

Automatic variables are created and destroyed each time a function is called.


#include <iostream>
using namespace std;

void demoFunction() {
    static int staticVar = 0;
    int autoVar = 0;
    staticVar++;
    autoVar++;
    cout << "Static: " << staticVar << ", Auto: " << autoVar << endl;
}

int main() {
    demoFunction();
    demoFunction();
    return 0;
}
    

Console Output:

Static: 1, Auto: 1 Static: 2, Auto: 1

Variable Storage Classes

Auto

The default storage class for local variables.

Register

Used to define variables that should be stored in a register instead of RAM for faster access.

Extern

Allows a variable to be accessible across multiple files.


#include <iostream>
using namespace std;

extern int globalVar;

int main() {
    register int regVar = 10;
    cout << "Register Variable: " << regVar;
    return 0;
}
    

Console Output:

Register Variable: 10

Dynamic Variables

Dynamic Memory Allocation

Dynamic variables are allocated memory during runtime using operators like new and delete.


#include <iostream>
using namespace std;

int main() {
    int* ptr = new int;
    *ptr = 20;
    cout << "Dynamic Variable: " << *ptr;
    delete ptr;
    return 0;
}
    

Console Output:

Dynamic Variable: 20

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025