Types of Variable in C Language in Hindi — Beginner से Advance तक पूरी जानकारी

Types of variable in C language in Hindi — यह topic beginner के लिये बहोत जरुरी है, इस लिये आज इस topic को detail से समझेंगे। अगर आप coding की दुनिया में नए हैं या फिर अपनी programming skills को मजबूत करना चाहते हैं, तो variables को समझना आपकी पहली और सबसे महत्वपूर्ण जिम्मेदारी है। Variable एक तरह का container होता है जो data को temporarily store करता है — और C language में इसके कई प्रकार होते हैं जिन्हें समझना बहुत जरूरी है।

C language को “mother of all programming languages” कहा जाता है। इसका कारण यह है कि आज की लगभग सभी modern programming languages जैसे C++, Java, Python — इन सबकी जड़ें C में ही हैं। इसलिए जब आप C में variables के प्रकार समझ लेते हैं, तो आपके लिए दूसरी भाषाएं सीखना भी बहुत आसान हो जाता है। Variables की सही समझ के बिना कोई भी program लिखना लगभग असंभव है।

इस blog post में आप सीखेंगे कि C language में variable क्या होता है, variable declare करने का सही तरीका क्या है, variables कितने प्रकार के होते हैं, हर type का scope और lifetime क्या होती है, और real examples के साथ इन्हें कैसे use किया जाता है। चाहे आप beginner हों या intermediate programmer — यह guide आपके लिए एक complete reference की तरह काम करेगी।

C Language में Variable क्या होता है?

Variable एक named memory location होती है जहाँ हम program execution के दौरान data store करते हैं। सीधे शब्दों में कहें तो variable एक “डिब्बा” है जिसमें हम कोई भी value रख सकते हैं और बाद में उसे बदल भी सकते हैं।

उदाहरण के लिए:

int age = 20;

float salary = 45000.50;

char grade = ‘A’;

यहाँ age, salary, और grade — ये सब variables हैं। इनमें अलग-अलग type का data store हो रहा है।

Variable Declaration का Syntax

data_type variable_name = value;

  1.  data_typeint, float, char, double, etc.
  2. variable_name — आप खुद नाम देते हैं
  3. value — optional initialization
Types of Variable in C Language in Hindi

Types of Variable in C Language in Hindi — पूरी जानकारी

C language में variables को मुख्यतः उनके scope, lifetime, और storage के आधार पर अलग-अलग प्रकार में बाँटा गया है। आइए हर एक प्रकार को detail में समझते हैं।

1. Local Variable (लोकल वेरिएबल)

Local variable वह variable होता है जो किसी function या block के अंदर declare किया जाता है। यह variable सिर्फ उसी function या block के अंदर ही accessible होता है, फिर चाहे वह main() हो या कोई और function.

विशेषताएं (Characteristics):

  • यह function के शुरू होते ही बनता है और function खत्म होते ही destroy हो जाता है।
  • इसकी lifetime सिर्फ उस function तक सीमित होती है।
  •  इसे function के बाहर से access नहीं किया जा सकता।
  • Default value garbage होती है (automatically initialize नहीं होता)।

Example Program:

#include <stdio.h>

// Function definition
void myFunction() {

    int num = 10;  // This is a local variable

    printf("Number: %d\n", num);
}

int main() {
    int num1;	// This a local variable
    myFunction();
    // printf("%d", num);
    // This would give an ERROR because num is not accessible here

    return 0;
}
▶️ Output Result:
Number: 10

कब Use करें?

जब आपको किसी specific function के लिए temporary data store करना हो, तब local variables सबसे best choice होते हैं। यह memory efficient भी होते हैं। Local Variable किसी function या block के अंदर ही बनाया जाता है, और उसका Scope वही तक होता है। 

2. Global Variable (ग्लोबल वेरिएबल)

Global variable वह variable होता है जो किसी भी function के बाहर, यानी program के top पर declare किया जाता है। यह पूरे program में कहीं से भी access किया जा सकता है।

विशेषताएं:

  • इसकी lifetime पूरे program execution तक होती है।
  • Program के किसी भी function से इसे read और modify किया जा सकता है।
  • Default value 0 (zero) होती है।
  • इसे बहुत सावधानी से use करना चाहिए क्योंकि किसी भी function से इसे modify किया जा सकता है।

Example Program:

#include <stdio.h>

int count = 0;  // Global variable

// Function to increase the value of count
void increment() {
    count++;  // Accessing the global variable here
}

// Function to display the value of count
void display() {
    printf("Count: %d\n", count);  // Accessing the global variable here
}

int main() {

    increment();
    increment();

    display();  // Printing the final value

    return 0;
}
▶️ Output Result:
Count: 2

Global Variable के नुकसान:

  1. Program बड़ा होने पर debugging मुश्किल हो जाती है।
  2. किसी भी function द्वारा accidentally modify होने का खतरा रहता है।
  3. Best practice: Global variables का use minimize करें।

3. Static Variable (स्टैटिक वेरिएबल)

Static variable एक बहुत interesting type होता है। इसे static keyword के साथ declare किया जाता है। यह local variable की तरह किसी function के अंदर होता है, लेकिन इसकी value function call के बाद भी बनी रहती है — यही इसकी खासियत है।

विशेषताएं:

  • एक बार initialize होने के बाद यह program के end तक memory में रहता है।
  • Default value 0 होती है।
  • हर function call पर नया variable नहीं बनता — वही पुराना variable reuse होता है।
  • Scope local रहता है, लेकिन lifetime global जैसी होती है।

Example Program:

#include <stdio.h>

// Function definition
void counter() {

    static int count = 0;  // Static variable

    count++;

    printf("Function called %d time(s)\n", count);
}

int main() {

    counter();  // First function call
    counter();  // Second function call
    counter();  // Third function call

    return 0;
}
▶️ Output Result:
Function called 1 time(s) Function called 2 time(s) Function called 3 time(s)

अगर count static नहीं होता, तो हर बार output “Function called 1 time(s)” ही होता।

कब Use करें?

जब आपको किसी function की state को याद रखना हो — जैसे कि function कितनी बार call हुआ — तब static variable बेहद useful होता है।

4. Automatic Variable (ऑटोमैटिक वेरिएबल)

C language में by default सभी local variables automatic होते हैं। इन्हें auto keyword के साथ declare किया जाता है, लेकिन यह keyword लिखना जरूरी नहीं होता।

Example Program:

#include <stdio.h>

int main() {

    auto int x = 5;  // The auto keyword is optional
    int y = 10;      // This is also automatically an auto variable

    // Printing the values of x and y
    printf("%d %d\n", x, y);

    return 0;
}
▶️ Output Result:
5 10

Modern C programming में auto keyword practically use नहीं होता क्योंकि सभी local variables default रूप से automatic ही होते हैं।

5. External Variable (एक्सटर्नल वेरिएबल)

जब आपका program multiple files में divided हो और आप एक file में declare किए गए variable को दूसरी file में use करना चाहते हों, तो extern keyword का use होता है।

विशेषताएं:

  • यह किसी दूसरी file में declare किए गए global variable को current file में accessible बनाता है।
  • Memory allocation नहीं होती — सिर्फ reference दिया जाता है।
  • Large projects में बहुत useful होता है।

Example Program:

file1.c:

int globalVar = 100;  // Declaration और Definition

file2.c:

#include <stdio.h>
 
extern int globalVar;  // सिर्फ Declaration — definition नहीं
 
int main() {
	printf("Global Variable: %d\n", globalVar);  // Output: 100
	return 0;
}

6. Register Variable (रजिस्टर वेरिएबल)

Register variable वह variable होता है जिसे CPU के register में store करने की request की जाती है — RAM में नहीं। यह execution को faster बनाता है।

विशेषताएं:

  • register keyword से declare किया जाता है।
  • यह compiler पर depend करता है — compiler request को accept या reject कर सकता है।
  • इसका address (&amp;) नहीं लिया जा सकता।
  • Loop counters जैसे frequently accessed variables के लिए best।

Example Program:

#include <stdio.h>

int main() {

    register int i;  // Register variable

    // Loop from 0 to 9
    for(i = 0; i < 10; i++) {
        printf("%d ", i);
    }

    return 0;
}
▶️ Output Result:
0 1 2 3 4 5 6 7 8 9

Note: Modern compilers खुद ही optimization करते हैं, इसलिए आजकल register keyword कम use होता है। लेकिन competitive exams और interviews के लिए यह जानना जरूरी है।

Variable TypeScopeLifetimeDefault ValueKeyword
LocalFunction/BlockFunction durationGarbage
GlobalEntire programEntire program0
StaticFunction/BlockEntire program0static
AutomaticFunction/BlockFunction durationGarbageauto
ExternalMultiple filesEntire program0extern
RegisterFunction/BlockFunction durationGarbageregister

C Language में Variable Naming Rules (नामकरण के नियम)

Types of variable in C language in Hindi को समझने के साथ-साथ यह भी जानना जरूरी है कि variables का नाम कैसे रखें:

  1. Variable name में alphabets (a-z, A-Z), digits (0-9), और underscore (_) ही use हो सकते हैं।
  2. नाम हमेशा letter या underscore से शुरू होना चाहिए — digit से नहीं।
  3. C language case-sensitive है — Age और age दो अलग variables हैं।
  4.  Reserved keywords (जैसे int, float, if, for) को variable name नहीं बनाया जा सकता।
  5. Variable name में spaces नहीं होने चाहिए।

Valid Variable Names:

age, _count, salary2024, total_marks, firstName

Invalid Variable Names:

2age (digit से शुरू), my age (space है), int (reserved keyword), first-name (hyphen है)

Storage Classes और Variables का संबंध

C language में हर variable की एक storage class होती है जो ऊपर अभी आपने पढ़ा। जो यह तय करती है:

  1. Storage Location — Memory (RAM) या CPU Register
  2. Default Value0 या Garbage
  3. Scope — कहाँ accessible है
  4. Lifetime — कब तक memory में रहेगा

C में 4 Storage Classes होती हैं: auto, register, static, extern — और ये सभी directly variable types से related हैं।

Common Mistakes जो Beginners करते हैं

Programming सीखते समय beginners अक्सर ये गलतियाँ करते हैं:

  1. Local variable को initialize किए बिना use करना — इससे garbage value मिलती है और unexpected results आते हैं।
  2. Global variables का अत्यधिक use — यह code को messy और debug करने में मुश्किल बना देता है।
  3. Variable के scope को ignore करना — एक function में declare किए variable को दूसरे function में use करने की कोशिश करना।
  4. Same name के variables का confusion — Local और global दोनों में same name होने पर local variable priority लेता है।

Local और Global Variable का Priority Rule Example:

#include <stdio.h>

int x = 50;  // Global variable

int main() {

    int x = 10;  // Local variable with the same name

    // The local variable gets higher priority
    printf("%d\n", x);

    return 0;
}
▶️ Output Result:
10

Practical Project — Variables का Real Use

चलिये हम एक student grade calculator बनाते हैं। जिसमे आप types of variable in C language का practical use देखेंगे:

#include <stdio.h>

// Global variable
// This variable keeps track of the total number of students
int totalStudents = 0;

// Function to calculate and display grade
void calculateGrade(float marks) {

    // Static variable
    // Its value remains saved between function calls
    static int studentCount = 0;

    // Local variable
    // Used to store the grade of the current student
    char grade;

    // Increasing counters
    studentCount++;
    totalStudents++;

    // Checking marks and assigning grade
    if(marks >= 90) {
        grade = 'A';
    }
    else if(marks >= 75) {
        grade = 'B';
    }
    else if(marks >= 60) {
        grade = 'C';
    }
    else {
        grade = 'F';
    }

    // Displaying student information
    printf("Student %d\n", studentCount);
    printf("Marks : %.1f\n", marks);
    printf("Grade : %c\n\n", grade);
}

int main() {

    // Student marks
    float student1Marks = 92.5;
    float student2Marks = 78.0;
    float student3Marks = 55.5;

    // Calling the function for each student
    calculateGrade(student1Marks);
    calculateGrade(student2Marks);
    calculateGrade(student3Marks);

    // Displaying total number of students
    printf("Total Students = %d\n", totalStudents);

    return 0;
}
▶️ Output Result:
Student 1 Marks : 92.5 Grade : AStudent 2 Marks : 78.0 Grade : BStudent 3 Marks : 55.5 Grade : FTotal Students = 3

निष्कर्ष (Conclusion)

तो दोस्तों, इस complete guide में हमने types of variable in C language in Hindi में बिल्कुल शुरुआत से लेकर advance level तक समझा। हमने देखा कि C language में variables सिर्फ data store करने का तरीका नहीं हैं — बल्कि यह program की memory management, performance, और structure की नींव होते हैं।

आइए एक बार briefly recap करें जो आपने आज सीखा:

  • Local Variable — function के अंदर, सीमित scope, temporary lifetime
  • Global Variable — पूरे program में accessible, पर सावधानी से use करें
  • Static Variablefunction calls के बीच value याद रखता है
  • Automatic Variable — default local variable, auto keyword optional
  • External Variable — multiple files में variable share करने के लिए
  • Register Variable — fast execution के लिए CPU register में store

अगर आप C language में variables को deeply समझ चुके हैं, तो समझिए आपने programming की सबसे मजबूत foundation रख दी है। यही concepts आगे चलकर C++, Java, और Python सीखते वक्त भी काम आएंगे।

एक important बात याद रखें — अच्छा programmer वही होता है जो सही जगह पर सही type का variable use करे। Global variables का अत्यधिक use avoid करें, static variables को समझदारी से use करें, और हमेशा variable को initialize करके चलें।

क्या यह article आपके लिए helpful रहा? नीचे comment करके जरूर बताएं। अगर कोई doubt है या कोई topic और detail में समझना है, तो बेझिझक पूछें। इस post को अपने दोस्तों के साथ share करें जो C language सीख रहे हैं — क्योंकि knowledge बाँटने से बढ़ता है! 

Frequently Asked Questions

Q 1: C language में variable declare करना क्यों जरूरी है?
Ans: C एक statically typed language है, जिसका मतलब है कि program run होने से पहले compiler को यह पता होना चाहिए कि कौन सा variable किस type का data store करेगा। बिना declaration के compiler memory allocate नहीं कर सकता और error देता है। यह Python जैसी dynamically typed languages से अलग है।
Q 2: Local और Global variable में मुख्य अंतर क्या है?
Ans: Local variable सिर्फ उस function में accessible होता है जहाँ वह declare हुआ है, जबकि global variable पूरे program में कहीं से भी access किया जा सकता है। Local variable function खत्म होने पर destroy हो जाता है, लेकिन global variable program के end तक memory में रहता है। Local की default value garbage होती है, जबकि global की default value 0 होती है।
Q 3: Static variable और global variable में क्या अंतर है?
Ans: दोनों की lifetime पूरे program execution तक होती है और default value 0 होती है। लेकिन मुख्य अंतर scope में है — global variable पूरे program में accessible होता है, जबकि static variable का scope सिर्फ उस function/block तक सीमित होता है जहाँ वह declare हुआ है। Static variable function के बाहर से access नहीं किया जा सकता।
Q 4: Register variable का address क्यों नहीं लिया जा सकता?
Ans: Register variable CPU के register में store होता है, RAM में नहीं। Address (& operator) सिर्फ RAM memory locations के लिए काम करता है। चूँकि registers का कोई memory address नहीं होता, इसलिए & operator register variables पर apply नहीं होता। Compiler इस पर error देता है।
Q 5: Types of variable in C language in Hindi — इन्हें कब और कैसे चुनें?
Ans: यह depend करता है आपकी जरूरत पर। अगर data सिर्फ एक function में चाहिए — local variable use करें। अगर पूरे program में data share करना है — global variable use करें (पर कम से कम)। अगर function calls के बीच value याद रखनी है — static variable best है। Multiple files में share करना है — extern use करें। Speed critical loop counter है — register consider करें।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top