Variables

 Variables

A named storage location in memory that holds a value that can be modified during the execution of a program.

Associated with a specific data type, which defines the type of data it can hold, such as integers, floating-point numbers, characters, and so on.

 

Declaring a Variable

To declare a variable in C++, you need to specify:

  1. Data type: The type of data the variable will hold.
  2. Variable name: The identifier used to refer to the variable.

Syntax:

data_type variable_name;


Example:

int age;        // Declares an integer variable named 'age'

float salary;   // Declares a floating-point variable named 'salary'

char grade;     // Declares a character variable named 'grade'


2. Assigning a Value to a Variable

After declaring a variable, you can assign a value to it. This is done using the assignment operator =.

Syntax:

variable_name = value;

Example:

int age = 25;      // Assigning value 25 to 'age'

float salary = 50000.50;  // Assigning value 50000.50 to 'salary'

char grade = 'A';  // Assigning 'A' to 'grade'


Types of Variables in C++

Primitive (Basic) Data Types:

  1. int: Used to store integers (whole numbers).
    • Example: int age = 30;
  2. float: Used to store single-precision floating-point numbers.
    • Example: float pi = 3.14f;
  3. double: Used to store double-precision floating-point numbers.
    • Example: double salary = 50000.75;
  4. char: Used to store a single character.
    • Example: char grade = 'A';
  5. bool: Used to store boolean values (true or false).
    • Example: bool isPassed = true;
  6. string: A class from the C++ Standard Library that stores a sequence of characters (more commonly used for text).
    • Example: string name = "John";


Modifiers for Data Types:

  • signed: Indicates a variable can hold both positive and negative values.
  • unsigned: Indicates a variable can hold only positive values (including zero).
  • long: Specifies a variable can hold a larger range of values.
  • short: Specifies a variable can hold a smaller range of values.

Example:

unsigned int x = 5;   // Only positive integers

long int largeNum = 123456789;  // Larger integer range

short int smallNum = 10;  // Smaller integer range 


Rules for Naming Variables

In C++, variable names must follow certain rules:

  1. Start with a letter or an underscore (_), followed by letters, digits, or underscores.
    • Valid: age, total_score, _count
    • Invalid: 123number, @variable
  2. Cannot use C++ keywords or reserved words.
    • Keywords: int, if, while, class, return, etc.
  3. Case-sensitive: Variable names are case-sensitive, so age and Age are different variables.
  4. Descriptive names: It’s recommended to use descriptive names that convey the purpose of the variable (e.g., score, temperature).


Variable Initialization

When a variable is declared, it may be initialised (assigned a value at the time of declaration) or left uninitialised.

Example of Initialisation:

int a = 10;   // 'a' is initialized with 10


Variable Scope

The scope of a variable refers to the region of the program where the variable can be accessed or modified. The main types of variable scope are:

  1. Local Variables: Declared inside a function or block, and can only be accessed within that function/block.
    • Example:
    • void example() {
    •     int a = 10;  // Local variable to 'example'
    •     cout << a;   // a can be used here
    • }
    • // cout << a;  // Error: a is out of scope here
  2. Global Variables: Declared outside any function, typically at the top of the program. These variables can be accessed from any function in the program.
    • Example:
    • int globalVar = 100;  // Global variable
    •  
    • void example() {
    •     cout << globalVar;  // Can access global variable here
    • }
  3. Static Variables: Variables that retain their value between function calls.
    • Example:
    • void count() {
    •     static int counter = 0;  // Static variable
    •     counter++;
    •     cout << counter << endl;
----------------------------------------------------------------------------------------------------------------

Post a Comment

0 Comments