techmore.in

C++ Variables

In C++, variables are used to store data that can be manipulated throughout the program. Each variable is associated with a data type that determines the kind of values it can store and how much memory is allocated for that variable.


Declaring Variables

In C++, you declare a variable by specifying its data type followed by the variable name.

Syntax:

cpp
data_type variable_name;

Example:

cpp
int age; // Declares an integer variable called age

Variables can also be initialized (assigned a value) when they are declared:

cpp
int age = 25; // Declares and initializes age with a value of 25

Basic Data Types

  1. Integer Types:

    • Stores whole numbers (both positive and negative).
    • Example: int, short, long, long long
    cpp
    int number = 10; // Integer variable long bigNumber = 1000; // Long integer variable
  2. Floating-Point Types:

    • Used to store decimal numbers.
    • Example: float, double
    cpp
    float temperature = 36.5f; // Floating-point variable double pi = 3.14159; // Double-precision floating-point variable
  3. Character Type:

    • Used to store a single character.
    • Example: char
    cpp
    char grade = 'A'; // Character variable
  4. Boolean Type:

    • Stores true or false.
    • Example: bool
    cpp
    bool isAdult = true; // Boolean variable
  5. String Type:

    • Stores a sequence of characters. Although not a built-in type, strings are commonly used via the std::string class.
    cpp
    std::string name = "John"; // String variable

Variable Naming Rules

  1. Names must begin with a letter or an underscore.

    • Example: int age;, int _number;
  2. Subsequent characters can be letters, digits, or underscores.

    • Example: int count_123;
  3. Case-sensitive:

    • Example: int Age; and int age; are two different variables.
  4. Cannot use reserved keywords (such as int, class, return).


Variable Scope

  • Local Variables: Declared inside a function or block. Only accessible within that function or block.

    cpp
    void example() { int x = 10; // Local variable std::cout << x; }
  • Global Variables: Declared outside of all functions. Accessible by any function within the program.

    cpp
    int x = 10; // Global variable void example() { std::cout << x; // Accessing the global variable }

Variable Initialization

Variables can be initialized in multiple ways:

  1. Direct Initialization:

    cpp
    int x = 5;
  2. Brace Initialization (C++11):

    cpp
    int x{5};
  3. Copy Initialization:

    cpp
    int x(5);

Constant Variables

Use the const keyword to declare constant variables, whose values cannot be changed after initialization.

Example:

cpp
const float PI = 3.14159; // Constant variable PI = 3.14; // Error: Cannot modify a constant variable

Example Code Using Variables

cpp
#include #include int main() { int age = 25; // Integer variable float height = 5.9f; // Floating-point variable char grade = 'A'; // Character variable bool isStudent = true; // Boolean variable std::string name = "John"; // String variable // Output the values of the variables std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Height: " << height << std::endl; std::cout << "Grade: " << grade << std::endl; std::cout << "Is Student: " << isStudent << std::endl; return 0; }

Output:

vbnet
Name: John Age: 25 Height: 5.9 Grade: A Is Student: 1

Summary

  • Variables store data that can be used and modified in the program.
  • They have a data type (like int, float, char, bool) which defines the kind of data they hold.
  • Variables can be local (within a function) or global (outside all functions).
  • Use the const keyword to declare variables that should not change during program execution.
  • Proper variable naming and initialization are crucial for readability and avoiding bugs.