Geek Slack

Learn C++
About Lesson


C++ Variables


C++ Variables

Variables are fundamental components of any programming language. In C++, variables are used to store data that can be manipulated by the program. This chapter will introduce you to the basics of variables in C++ and provide examples to help you understand how to use them effectively.

1. Variable Declaration and Initialization

In C++, you must declare a variable before using it. The declaration specifies the variable’s type and its name. Initialization assigns an initial value to the variable.

#include <iostream>

int main() {
    int myNumber;        // Declaration
    myNumber = 10;       // Initialization
    int myOtherNumber = 20; // Declaration and Initialization
    std::cout << "myNumber: " << myNumber << std::endl;
    std::cout << "myOtherNumber: " << myOtherNumber << std::endl;
    return 0;
}

2. Basic Data Types

C++ supports various data types, including:

  • int – Integer type
  • float – Floating-point type
  • double – Double-precision floating-point type
  • char – Character type
  • bool – Boolean type
#include <iostream>

int main() {
    int myInt = 100;
    float myFloat = 3.14f;
    double myDouble = 3.14159;
    char myChar = 'A';
    bool myBool = true;

    std::cout << "Integer: " << myInt << std::endl;
    std::cout << "Float: " << myFloat << std::endl;
    std::cout << "Double: " << myDouble << std::endl;
    std::cout << "Character: " << myChar << std::endl;
    std::cout << "Boolean: " << myBool << std::endl;
    return 0;
}

3. Scope of Variables

The scope of a variable is the region of the code where the variable can be accessed. Variables can be declared in different scopes:

  • Local Variables: Declared inside a function or block and accessible only within that function or block.
  • Global Variables: Declared outside of all functions and accessible from any part of the program.
#include <iostream>

int globalVar = 100; // Global variable

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

4. Constants

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

#include <iostream>

int main() {
    const int myConstant = 100;
    std::cout << "Constant: " << myConstant << std::endl;
    // myConstant = 200; // Error: cannot assign a new value to a constant
    return 0;
}

5. Type Conversion

C++ allows you to convert variables from one type to another. This can be done either implicitly or explicitly.

Implicit Type Conversion

The compiler automatically converts one data type to another.

#include <iostream>

int main() {
    int myInt = 10;
    double myDouble = myInt; // Implicit conversion from int to double
    std::cout << "Double: " << myDouble << std::endl;
    return 0;
}

Explicit Type Conversion

You can explicitly convert one data type to another using type casting.

#include <iostream>

int main() {
    double myDouble = 3.14;
    int myInt = static_cast<int>(myDouble); // Explicit conversion from double to int
    std::cout << "Integer: " << myInt << std::endl;
    return 0;
}

6. Auto Keyword

The auto keyword allows the compiler to automatically deduce the type of a variable from its initializer.

#include <iostream>

int main() {
    auto myInt = 100;
    auto myDouble = 3.14;
    auto myString = "Hello, World!";
    
    std::cout << "Integer: " << myInt << std::endl;
    std::cout << "Double: " << myDouble << std::endl;
    std::cout << "String: " << myString << std::endl;
    return 0;
}

Conclusion

This chapter introduced you to variables in C++. You learned how to declare and initialize variables, the different data types available, variable scope, constants, type conversion, and the auto keyword. Understanding these basics is essential for writing effective and efficient C++ programs.

Join the conversation