Variables in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Variables in C++ Notes | Basic Computer Engineering | RGPV BTech First Year


Variables in C++

C++ Programming рдореЗрдВ Variable рд╕рдмрд╕реЗ рдорд╣рддреНрд╡рдкреВрд░реНрдг Concept рд╣реИред Variable Memory рдХрд╛ рд╡рд╣ Named Location рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕рдореЗрдВ Program Execution рдХреЗ рджреМрд░рд╛рди Data Store рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдХрд┐рд╕реА рднреА Program рдореЗрдВ Input рд▓реЗрдирд╛, Processing рдХрд░рдирд╛ рддрдерд╛ Output рдкреНрд░рд╛рдкреНрдд рдХрд░рдирд╛ Variables рдХреЗ рдмрд┐рдирд╛ рд╕рдВрднрд╡ рдирд╣реАрдВ рд╣реИред рдкреНрд░рддреНрдпреЗрдХ Variable рдХрд╛ рдПрдХ рдирд┐рд╢реНрдЪрд┐рдд Data Type, Name рддрдерд╛ Memory Location рд╣реЛрддрд╛ рд╣реИред

рдЬрдм Program Execute рд╣реЛрддрд╛ рд╣реИ рддрдм Compiler рдкреНрд░рддреНрдпреЗрдХ Variable рдХреЗ рд▓рд┐рдП Memory Allocate рдХрд░рддрд╛ рд╣реИред Program рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдЕрдиреБрд╕рд╛рд░ Variable рдХреА Value рдмрджрд▓ рд╕рдХрддреА рд╣реИ, рдЗрд╕рд▓рд┐рдП рдЗрд╕реЗ Variable рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред рдпрджрд┐ рдХрд┐рд╕реА Value рдХреЛ Program рдХреЗ рджреМрд░рд╛рди рдмрджрд▓рдирд╛ рди рд╣реЛ рддреЛ рдЙрд╕рдХреЗ рд▓рд┐рдП Constant рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред


Introduction

Programming рдореЗрдВ Data рдХреЛ Memory рдореЗрдВ Store рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП Variables рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Variables рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ User Input, Intermediate Results рддрдерд╛ Final Output рдХреЛ рд╕реБрд░рдХреНрд╖рд┐рдд рд░рдЦрд╛ рдЬрд╛рддрд╛ рд╣реИред рдкреНрд░рддреНрдпреЗрдХ Variable рдХрд╛ рдПрдХ Unique Name рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕реЗ Identifier рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред

Variable рдХрд╛ рдЙрдкрдпреЛрдЧ Mathematical Calculations, Decision Making, Loop Control, Array Processing, File Handling рддрдерд╛ Object Oriented Programming рд╕рд╣рд┐рдд рд▓рдЧрднрдЧ рдкреНрд░рддреНрдпреЗрдХ Programming Concept рдореЗрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред


Definition of Variable

Variable Memory рдХрд╛ рдПрдХ Named Storage Location рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕рдореЗрдВ рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ Data Type рдХрд╛ Data Store рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ рддрдерд╛ рдЬрд┐рд╕рдХреА Value Program Execution рдХреЗ рджреМрд░рд╛рди рдмрджрд▓ рд╕рдХрддреА рд╣реИред

Variable = Named Memory Location + Data Type + Value

Characteristics of Variables

  • Memory Occupy рдХрд░рддреЗ рд╣реИрдВред
  • Unique Identifier рд░рдЦрддреЗ рд╣реИрдВред
  • Specific Data Type рд╣реЛрддрд╛ рд╣реИред
  • Program Execution рдХреЗ рджреМрд░рд╛рди Value рдмрджрд▓ рд╕рдХрддреА рд╣реИред
  • Compiler рджреНрд╡рд╛рд░рд╛ Memory Allocate рдХреА рдЬрд╛рддреА рд╣реИред
  • Scope рддрдерд╛ Lifetime рд╣реЛрддрд╛ рд╣реИред

Syntax of Variable Declaration

data_type variable_name;

Examples

int age;
float salary;
char grade;
double percentage;
bool result;

Variable Initialization

Variable Declare рдХрд░рдиреЗ рдХреЗ рд╕рд╛рде-рд╕рд╛рде рдЙрд╕рдХреА рдкреНрд░рд╛рд░рдореНрднрд┐рдХ Value рджреЗрдирд╛ Variable Initialization рдХрд╣рд▓рд╛рддрд╛ рд╣реИред

int age = 20;
float pi = 3.14159;
char grade = 'A';
bool pass = true;

Rules for Declaring Variables

Rule Description
Name Must be a Valid Identifier
Starting Character Alphabet or Underscore
Keyword Cannot be Used
Spaces Not Allowed
Special Symbols Not Allowed Except Underscore
Case Sensitivity C++ is Case Sensitive

Types of Variables

  1. Local Variable
  2. Global Variable
  3. Static Variable
  4. Automatic Variable
  5. Register Variable

1. Local Variable

Local Variable рд╡рд╣ Variable рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕реЗ рдХрд┐рд╕реА Function рдЕрдерд╡рд╛ Block рдХреЗ рдЕрдВрджрд░ Declare рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рдХрд╛ Scope рдХреЗрд╡рд▓ рдЙрд╕реА Block рддрдХ рд╕реАрдорд┐рдд рд░рд╣рддрд╛ рд╣реИред

void display()
{
  int marks = 90;
  cout << marks;
}

Characteristics of Local Variable

  • Declared Inside Function.
  • Accessible Only Within Function.
  • Created During Function Call.
  • Destroyed After Function Ends.

2. Global Variable

Global Variable рд╡рд╣ Variable рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕реЗ рд╕рднреА Functions рдХреЗ рдмрд╛рд╣рд░ Declare рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕реЗ рдкреВрд░реЗ Program рдореЗрдВ рдХрд┐рд╕реА рднреА Function рджреНрд╡рд╛рд░рд╛ Access рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред

int totalStudents = 120; int main() {   cout << totalStudents; }

Characteristics of Global Variable

  • Declared Outside Functions.
  • Accessible Throughout Program.
  • Allocated Once.
  • Exists Until Program Terminates.

3. Static Variable

Static Variable рд╡рд╣ Variable рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕реЗ static Keyword рдХреА рд╕рд╣рд╛рдпрддрд╛ рд╕реЗ Declare рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдпрд╣ рдХреЗрд╡рд▓ рдПрдХ рдмрд╛рд░ Memory Allocate рдХрд░рддрд╛ рд╣реИ рддрдерд╛ Program рдХреЗ рд╕рдорд╛рдкреНрдд рд╣реЛрдиреЗ рддрдХ рдЕрдкрдиреА Value рдХреЛ рд╕реБрд░рдХреНрд╖рд┐рдд рд░рдЦрддрд╛ рд╣реИред рдпрджрд┐ Function рдХреЛ рдХрдИ рдмрд╛рд░ Call рдХрд┐рдпрд╛ рдЬрд╛рдП рддреЛ Static Variable рдЕрдкрдиреА Previous Value рдХреЛ рдмрдирд╛рдП рд░рдЦрддрд╛ рд╣реИред

void counter()
{
  static int count = 0;
  count++;
  cout << count;
}

Characteristics of Static Variable

  • Declared using static Keyword.
  • Memory Allocated Only Once.
  • Retains Previous Value.
  • Exists Until Program Terminates.
  • Useful for Function Call Counting.

4. Automatic Variable

Automatic Variable рд╡рд╣ Variable рд╣реЛрддрд╛ рд╣реИ рдЬреЛ Function рдпрд╛ Block рдХреЗ рдЕрдВрджрд░ Declare рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдпрджрд┐ рдХрд┐рд╕реА Variable рдХреЗ рд╕рд╛рде рдХреЛрдИ Storage Class рдирд╣реАрдВ рд▓рд┐рдЦрд╛ рдЧрдпрд╛ рд╣реЛ рддреЛ рд╡рд╣ Default рд░реВрдк рд╕реЗ Automatic Variable рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИред

void demo()
{
  int number = 10;
}

Characteristics

  • Default Storage Class.
  • Created on Function Call.
  • Destroyed After Function Ends.
  • Stored in Stack Memory.

5. Register Variable

Register Variable рд╡рд╣ Variable рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕реЗ register Keyword рджреНрд╡рд╛рд░рд╛ Declare рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Compiler рдкреНрд░рдпрд╛рд╕ рдХрд░рддрд╛ рд╣реИ рдХрд┐ рдРрд╕реЗ Variable рдХреЛ CPU Register рдореЗрдВ Store рдХрд┐рдпрд╛ рдЬрд╛рдП рдЬрд┐рд╕рд╕реЗ Program рдХреА Execution Speed рдмрдврд╝ рд╕рдХреЗред

register int i;

Characteristics

  • Stored in CPU Register (If Possible).
  • Faster Access.
  • Suitable for Frequently Used Variables.
  • Compiler Decision Based Allocation.

Scope and Lifetime of Variables

Variable Type Scope Lifetime
Local Inside Function Function Execution
Global Whole Program Entire Program
Static Block / Function Entire Program
Automatic Inside Function Function Execution
Register Inside Function Function Execution

Memory Representation of Variables

рдЬрдм Program Execute рд╣реЛрддрд╛ рд╣реИ рддрдм рдкреНрд░рддреНрдпреЗрдХ Variable рдХреЛ Memory рдореЗрдВ рдПрдХ Address рдкреНрд░рджрд╛рди рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Variable рдХрд╛ Data Type рдпрд╣ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд░рддрд╛ рд╣реИ рдХрд┐ рдЙрд╕реЗ рдХрд┐рддрдиреА Memory Allocate рд╣реЛрдЧреАред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП char рд╕рд╛рдорд╛рдиреНрдпрддрдГ 1 Byte, int 4 Bytes рддрдерд╛ double 8 Bytes Memory рд▓реЗрддрд╛ рд╣реИ (Compiler рдПрд╡рдВ Platform рдкрд░ рдирд┐рд░реНрднрд░ рдХрд░рддрд╛ рд╣реИ)ред

Data Type Typical Size
char 1 Byte
int 4 Bytes
float 4 Bytes
double 8 Bytes
bool 1 Byte

Comparison of Variable Types

Feature Local Global Static
Declaration Inside Function Outside Function Inside/Outside Function
Scope Local Global Limited
Lifetime Function Call Entire Program Entire Program
Value Retention No Yes Yes

Best Practices

  • Always use Meaningful Variable Names.
  • Initialize Variables Before Use.
  • Keep Variable Scope as Small as Possible.
  • Avoid Unnecessary Global Variables.
  • Use const for Fixed Values.
  • Follow Standard Naming Conventions.

Common Errors

  • Using Uninitialized Variables.
  • Declaring Duplicate Variable Names.
  • Using Invalid Identifiers.
  • Accessing Local Variables Outside Their Scope.
  • Confusing Local and Global Variables.

Applications of Variables

  • Input Data Storage.
  • Mathematical Calculations.
  • Loop Counters.
  • Decision Making.
  • Array Processing.
  • Object Data Members.
  • File Processing.
  • Database Applications.

Industrial Importance

Software Development рдореЗрдВ Variables рдХрд╛ рдЙрдкрдпреЛрдЧ рдкреНрд░рддреНрдпреЗрдХ Application рдореЗрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Banking Software, ERP Systems, Embedded Systems, Operating Systems, Web Applications рддрдерд╛ Artificial Intelligence Programs рд╕рднреА Variables рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ Data рдХреЛ Store рдПрд╡рдВ Process рдХрд░рддреЗ рд╣реИрдВред рдЙрдЪрд┐рдд Variable Declaration рддрдерд╛ Naming Convention Program рдХреА Readability рдПрд╡рдВ Maintainability рдХреЛ рдмреЗрд╣рддрд░ рдмрдирд╛рддреЗ рд╣реИрдВред


Viva Questions

  1. What is a Variable?
  2. What is Variable Initialization?
  3. What is the difference between Local and Global Variables?
  4. What is a Static Variable?
  5. What is a Register Variable?
  6. What is Variable Scope?
  7. What is Variable Lifetime?
  8. Why should Variables be Initialized?
  9. Which Variable has Global Scope?
  10. Which Storage Class retains its value?

Exam Oriented Important Questions

  1. Define Variable in C++.
  2. Explain Variable Declaration and Initialization.
  3. Discuss different types of Variables.
  4. Differentiate Local, Global and Static Variables.
  5. Explain Scope and Lifetime of Variables.
  6. Write a note on Register Variable.
  7. Describe Best Practices for Variable Naming.
  8. Explain Memory Representation of Variables.

Conclusion

Variables C++ Programming рдХреЗ рд╕рдмрд╕реЗ рдорд╣рддреНрд╡рдкреВрд░реНрдг Building Blocks рд╣реИрдВред рдЗрдирдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ Program Data рдХреЛ Store, Modify рддрдерд╛ Process рдХрд░рддрд╛ рд╣реИред Local, Global, Static, Automatic рддрдерд╛ Register Variables рдХрд╛ рд╕рд╣реА рдЙрдкрдпреЛрдЧ Program рдХреЛ рдЕрдзрд┐рдХ Efficient, Readable рддрдерд╛ Maintainable рдмрдирд╛рддрд╛ рд╣реИред рдЗрд╕рд▓рд┐рдП рдкреНрд░рддреНрдпреЗрдХ Computer Engineering Student рдХреЗ рд▓рд┐рдП Variables рдХреА рд╕реНрдкрд╖реНрдЯ рд╕рдордЭ рдЕрддреНрдпрдВрдд рдЖрд╡рд╢реНрдпрдХ рд╣реИред

Related Articles

Manipulators in C++ Notes | endl, setw, setprecision, setfill | Basic Computer Engineering | RGPV BTech First Year

0...

Read More тЖТ

Type Conversion in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Type Co...

Read More тЖТ

Assignment Operator in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Assignm...

Read More тЖТ

Copy Constructor in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Copy Co...

Read More тЖТ

this Pointer in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

this Po...

Read More тЖТ