C++ Variables

Learn how to declare and use variables in C++.

C++ Variables

Variable computer memory का एक named location (नामित स्थान) है जिसका उपयोग program के दौरान data को store करने के लिए किया जाता है। Variable की value program के execution के दौरान बदली जा सकती है।

C++ में किसी variable को use करने से पहले सामान्यतः उसका data type और name बताया जाता है।

Basic Idea: Data Type + Variable Name = Variable Declaration

What is a Variable?

Variable एक ऐसा नाम है जो memory में किसी location को identify करता है, जहाँ कोई value store की जा सकती है। उदाहरण के लिए, यदि हमें किसी student की age store करनी है, तो हम age नाम का variable बना सकते हैं।

#include <iostream>
using namespace std;

int main()
{
    int age = 17;

    cout << "Age = " << age;

    return 0;
}
Output:
Age = 17

यहाँ age एक variable है और इसमें 17 value store की गई है।

Why are Variables Used?

Program में data को temporarily store करने, उस data पर operations करने और आवश्यकता के अनुसार उसकी value को बदलने के लिए variables का उपयोग किया जाता है।

उदाहरण के लिए, दो numbers को store करके उनका sum निकाला जा सकता है:

#include <iostream>
using namespace std;

int main()
{
    int a = 20;
    int b = 30;

    int sum = a + b;

    cout << "Sum = " << sum;

    return 0;
}
Output:
Sum = 50

Variable Declaration

Variable Declaration का अर्थ है compiler को यह बताना कि variable का कौन-सा data type और कौन-सा name है।

General syntax:

Syntax:
data_type variable_name;

उदाहरण:

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

यहाँ अलग-अलग data types के चार variables declare किए गए हैं।

Variable Initialization

Initialization का अर्थ variable को declare करते समय उसे initial value देना है।

उदाहरण:

int age = 17;
float percentage = 85.5;
char grade = 'A';

यहाँ variables declaration के साथ ही initialize किए गए हैं।

Declaration and Initialization Together

C++ में variable को declare और initialize एक ही statement में किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int number = 25;

    cout << number;

    return 0;
}
Output:
25

Assigning a Value to a Variable

किसी variable को declare करने के बाद assignment operator (=) की सहायता से value दी जा सकती है।

#include <iostream>
using namespace std;

int main()
{
    int number;

    number = 50;

    cout << number;

    return 0;
}
Output:
50

यहाँ पहले number variable declare किया गया और बाद में उसमें 50 value assign की गई।

Changing the Value of a Variable

Variable की value program के execution के दौरान बदली जा सकती है। इसी कारण इसे variable कहा जाता है।

#include <iostream>
using namespace std;

int main()
{
    int number = 10;

    cout << "Before = " << number << endl;

    number = 25;

    cout << "After = " << number;

    return 0;
}
Output:
Before = 10
After = 25
याद रखें: Variable की value बदली जा सकती है, जबकि constant की value को सामान्यतः program के दौरान बदला नहीं जाता।

Data Type of a Variable

Variable का data type यह निर्धारित करता है कि variable में किस प्रकार का data store किया जाएगा और उस data पर किस प्रकार के operations किए जा सकते हैं।

Data Type Typical Use Example
int Whole numbers int age = 17;
float Decimal numbers float marks = 85.5f;
double Decimal numbers with greater precision double pi = 3.14159;
char Single character char grade = 'A';
bool Boolean value bool passed = true;

Integer Variable

int data type का उपयोग सामान्यतः whole numbers (पूर्णांक) को store करने के लिए किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    int marks = 450;

    cout << "Marks = " << marks;

    return 0;
}
Output:
Marks = 450

Floating-Point Variable

float और double का उपयोग decimal values को store करने के लिए किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    float percentage = 85.5f;
    double pi = 3.14159;

    cout << "Percentage = " << percentage << endl;
    cout << "Pi = " << pi;

    return 0;
}
Output:
Percentage = 85.5
Pi = 3.14159

Character Variable

char data type का उपयोग एक single character को store करने के लिए किया जाता है। Character को single quotation marks (' ') के अंदर लिखा जाता है।

#include <iostream>
using namespace std;

int main()
{
    char grade = 'A';

    cout << "Grade = " << grade;

    return 0;
}
Output:
Grade = A

Boolean Variable

bool data type का उपयोग logical value को represent करने के लिए किया जाता है। इसकी मुख्य values true और false होती हैं।

#include <iostream>
using namespace std;

int main()
{
    bool passed = true;

    cout << passed;

    return 0;
}
Output:
1

Default output formatting में true को 1 और false को 0 के रूप में display किया जा सकता है।

Rules for Naming Variables

C++ में variable name रखते समय कुछ rules का पालन करना आवश्यक है:

  • Variable name में letters, digits और underscore (_) का उपयोग किया जा सकता है।
  • Variable name का पहला character digit नहीं हो सकता।
  • Variable name में spaces का उपयोग नहीं किया जा सकता।
  • Keyword को variable name के रूप में use नहीं किया जा सकता।
  • C++ case-sensitive है, इसलिए marks और Marks अलग variable names हैं।
  • Variable name meaningful रखना बेहतर होता है, ताकि code को समझना आसान हो।

Valid and Invalid Variable Names

Variable Name Status Reason
age Valid Valid identifier
studentName Valid Letters का उपयोग
marks2 Valid Digit पहले नहीं है
_total Valid Underscore से शुरू हो सकता है
2marks Invalid Digit से शुरू हो रहा है
student name Invalid Space का उपयोग किया गया है
total-marks Invalid Hyphen identifier का valid हिस्सा नहीं है
int Invalid यह keyword है

Case Sensitivity in Variable Names

C++ case-sensitive language है। इसलिए uppercase और lowercase letters को अलग माना जाता है।

#include <iostream>
using namespace std;

int main()
{
    int marks = 80;
    int Marks = 90;

    cout << marks << endl;
    cout << Marks;

    return 0;
}
Output:
80
90

यहाँ marks और Marks दो अलग variables हैं।

Multiple Variables

C++ में एक ही data type के multiple variables को अलग-अलग declare किया जा सकता है या एक statement में भी declare किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int a = 10, b = 20, c = 30;

    cout << a << " " << b << " " << c;

    return 0;
}
Output:
10 20 30

Variable Assignment

एक variable की value दूसरे variable में assign की जा सकती है।

#include <iostream>
using namespace std;

int main()
{
    int a = 25;
    int b = a;

    cout << "a = " << a << endl;
    cout << "b = " << b;

    return 0;
}
Output:
a = 25
b = 25

यहाँ b में a की current value copy होकर store हुई है।

Using Variables in Expressions

Variables का उपयोग arithmetic और अन्य expressions में किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int length = 10;
    int width = 5;

    int area = length * width;

    cout << "Area = " << area;

    return 0;
}
Output:
Area = 50

Taking Input into a Variable

Variables में user से input लेकर value store की जा सकती है। इसके लिए cin का उपयोग किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    int age;

    cout << "Enter your age: ";
    cin >> age;

    cout << "Your age is " << age;

    return 0;
}
Sample Output:
Enter your age: 17
Your age is 17

Variable Scope

Scope यह बताता है कि किसी variable को program के किस भाग में access किया जा सकता है। एक function या block के अंदर declare किया गया local variable सामान्यतः उसी scope में accessible होता है।

उदाहरण:

#include <iostream>
using namespace std;

int main()
{
    int number = 10;

    {
        int value = 20;

        cout << number << endl;
        cout << value;
    }

    return 0;
}
Output:
10
20

Inner block के अंदर number और value दोनों accessible हैं, लेकिन value का scope उस inner block तक सीमित है।

Note: Variable scope का विस्तृत अध्ययन आगे के topics में किया जाएगा। यहाँ basic concept समझना पर्याप्त है।

Local Variable

किसी function या block के अंदर declare किया गया variable सामान्यतः local variable कहलाता है। इसका scope उसी function या block तक सीमित रहता है जहाँ इसे declare किया गया है।

#include <iostream>
using namespace std;

int main()
{
    int number = 100;

    cout << number;

    return 0;
}
Output:
100

Uninitialized Variables

यदि किसी automatic local variable को declaration के समय कोई initial value नहीं दी जाती, तो उसकी value निर्धारित नहीं होती। ऐसे variable को बिना initialize किए पढ़ना उचित नहीं है।

इसलिए variable को declare करते समय meaningful initial value देना एक अच्छी programming practice है।

int number = 0;
Best Practice: जहाँ उचित हो, variables को declaration के समय initialize करें।

Constants and Variables

Variable की value program के दौरान बदली जा सकती है, जबकि constant का उद्देश्य ऐसी value रखना है जिसे program के संबंधित scope में बदलने की अनुमति नहीं होती।

Variable Constant
Value बदली जा सकती है। Value को बदलने की अनुमति नहीं होती।
उदाहरण: int age = 17; उदाहरण: const int days = 7;

Example of Variable and Constant

#include <iostream>
using namespace std;

int main()
{
    int age = 17;
    const int days = 7;

    age = 18;

    cout << "Age = " << age << endl;
    cout << "Days = " << days;

    return 0;
}
Output:
Age = 18
Days = 7

Good Naming Practices

Variable names ऐसे रखने चाहिए जिनसे variable में stored data का purpose आसानी से समझ में आए।

Less Meaningful Better Name
x studentAge
a totalMarks
p percentage
n numberOfStudents

Meaningful variable names code को पढ़ने और समझने में आसान बनाते हैं।

Complete Example

अब एक example देखते हैं जिसमें variables declare किए गए हैं, values assign की गई हैं और उन पर calculation की गई है:

#include <iostream>
using namespace std;

int main()
{
    int physics = 80;
    int mathematics = 90;
    int computer = 85;

    int total = physics + mathematics + computer;
    float percentage = total / 3.0f;

    cout << "Total = " << total << endl;
    cout << "Percentage = " << percentage;

    return 0;
}
Output:
Total = 255
Percentage = 85

Important Points

  • Variable memory में data store करने के लिए named location है।
  • Variable को use करने से पहले उसका data type और name specify किया जाता है।
  • Variable declaration का सामान्य syntax data_type variable_name; है।
  • Initialization में variable को initial value दी जाती है।
  • Assignment operator (=) का उपयोग value assign करने के लिए किया जाता है।
  • Variable की value program के दौरान बदली जा सकती है।
  • Variable name में letters, digits और underscore का उपयोग किया जा सकता है।
  • Variable name digit से शुरू नहीं हो सकता।
  • Variable name में spaces का उपयोग नहीं किया जा सकता।
  • C++ case-sensitive language है।
  • Keyword को variable name के रूप में उपयोग नहीं किया जा सकता।
  • Meaningful variable names program की readability improve करते हैं।
  • Variable का scope यह निर्धारित करता है कि उसे program के किस भाग में access किया जा सकता है।

Board Focus

Exam के लिए याद रखें:
Variable → Named memory location
Declaration → int age;
Initialization → int age = 17;
Assignment → age = 18;
Input → cin >> age;
Output → cout << age;
Value Change → Variable की value बदली जा सकती है
Case Sensitive → age और Age अलग हैं

Board Important Questions

Very Short Answer Questions

Q1. Variable क्या है?

Answer: Variable memory में एक named location है जिसका उपयोग data store करने के लिए किया जाता है।

Q2. Variable declaration क्या है?

Answer: Variable का data type और name specify करना variable declaration कहलाता है।

Q3. Initialization क्या है?

Answer: Variable को initial value देना initialization कहलाता है।

Q4. Variable की value assign करने के लिए किस operator का उपयोग किया जाता है?

Answer: Assignment operator (=) का उपयोग किया जाता है।

Q5. क्या C++ case-sensitive language है?

Answer: हाँ, C++ case-sensitive language है।

Q6. क्या variable name digit से शुरू हो सकता है?

Answer: नहीं, variable name digit से शुरू नहीं हो सकता।

Q7. Variable में input लेने के लिए किसका उपयोग किया जाता है?

Answer: cin का उपयोग किया जाता है।

Short Answer Questions

Q8. Variable declaration और initialization में अंतर बताइए।

Declaration Initialization
Variable का data type और name specify करता है। Variable को initial value देता है।
Example: int age; Example: int age = 17;

Q9. Variable naming के नियम लिखिए।

Answer: Variable name में letters, digits और underscore का उपयोग किया जा सकता है। Variable name digit से शुरू नहीं हो सकता। इसमें spaces नहीं हो सकते। Keywords को variable names के रूप में उपयोग नहीं किया जा सकता। C++ case-sensitive होने के कारण uppercase और lowercase names अलग माने जाते हैं।

Q10. Variable की value program के दौरान कैसे बदली जा सकती है?

Answer: Assignment operator (=) की सहायता से variable को नई value assign करके उसकी value बदली जा सकती है।

int number = 10;
number = 20;

अब number की value 20 हो जाएगी।

Q11. Meaningful variable names का क्या महत्व है?

Answer: Meaningful variable names से variable के purpose को आसानी से समझा जा सकता है। इससे program की readability, understanding और maintainability बेहतर होती है।

Long Answer Questions

Q12. C++ में variables को उदाहरण सहित समझाइए।

Answer: Variable memory में एक named location है जिसका उपयोग data store करने के लिए किया जाता है। Variable declaration में उसका data type और name specify किया जाता है। Variable को declaration के समय initialize किया जा सकता है या बाद में value assign की जा सकती है। Program के दौरान variable की value बदली भी जा सकती है।

#include <iostream>
using namespace std;

int main()
{
    int number = 10;

    cout << "Before = " << number << endl;

    number = 25;

    cout << "After = " << number;

    return 0;
}
Output:
Before = 10
After = 25

Q13. Variable declaration, initialization और assignment को उदाहरण सहित समझाइए।

Answer: Variable declaration में variable का type और name बताया जाता है, जैसे int age;। Initialization में variable को declaration के समय initial value दी जाती है, जैसे int age = 17;। Assignment में पहले से declared variable को value दी जाती है, जैसे age = 18;

Quick Revision

  • Variable: Named memory location
  • Declaration: Data type और name specify करना
  • Initialization: Initial value देना
  • Assignment: Variable को value देना या बदलना
  • Data Type: Stored data का type निर्धारित करता है
  • cin: Input लेने के लिए
  • cout: Output देने के लिए
  • Scope: Variable की accessibility का क्षेत्र
  • Case Sensitive: Uppercase और lowercase अलग माने जाते हैं
One-Line Revision: C++ variable memory में data store करने के लिए एक named location है, जिसे किसी data type के साथ declare किया जाता है और जिसकी value program के दौरान बदली जा सकती है।

Practice Questions

  1. Variable क्या है?
  2. Variable declaration क्या है?
  3. Initialization और assignment में क्या अंतर है?
  4. C++ में variable naming के नियम लिखिए।
  5. क्या variable name digit से शुरू हो सकता है? कारण बताइए।
  6. C++ case-sensitive language होने का क्या अर्थ है?
  7. Variable की value program के दौरान कैसे बदली जाती है?
  8. Meaningful variable names क्यों उपयोग किए जाते हैं?
  9. Local variable क्या है?
  10. Variable और constant में अंतर बताइए।
  11. निम्न code का output बताइए:
    int x = 10;
    x = 25;
    cout << x;
  12. एक C++ program लिखिए जिसमें तीन variables में तीन subjects के marks store करके total display किया जाए।
  13. C++ variables को declaration, initialization और assignment के साथ उदाहरण सहित समझाइए।
Lesson 19 of 39
On This Page