top of page

What is a Variable? / वेरिएबल क्या है?

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Oct 6, 2025
  • 4 min read
  • A variable is a named storage location in memory that holds a value which can change during program execution. / वेरिएबल एक नामित मेमोरी लोकेशन है जो प्रोग्राम के चलने के दौरान बदलने योग्य वैल्यू रखती है।

  • Syntax to declare: data_type variable_name; — e.g., int age; / सिंटैक्स: data_type variable_name; — उदाहरण: int age;



Why variables needed? / वेरिएबल की ज़रूरत

  • Readability: Meaningful names (like score, total) make code easy to understand. / रीडेबिलिटी बढ़ती है।

  • Reusability: एक बार वैरिएबल बनाकर कई जगह इस्तेमाल कर सकते हैं।

  • Memory management: Compiler को पता चलता है कितनी memory allocate करनी है।


Variable Declaration vs Initialization / घोषणा और प्रारंभिकरण

  • Declaration: Compiler को बताना कि नाम और डेटा टाइप है. — int x; / घोषणा: int x;

  • Initialization: उस वेरिएबल को पहली बार वैल्यू देना. — int x = 5; / प्रारंभिकरण: int x = 5;

  • You can declare first and initialize later, या एक ही लाइन में दोनों कर सकते हैं.


Rules for Naming Variables / वेरिएबल के नामकरण के नियम

  1. Letters (a–z, A–Z), digits (0–9) और underscore (_) allowed. / केवल letters, digits और _

  2. पहला character alphabet या underscore होना चाहिए — digit से शुरू नहीं होना चाहिए. / पहला character letter या _ होना चाहिए।

  3. No spaces, और keywords (like int, return) variable names नहीं हो सकते। / spaces और keywords नहीं।

  4. Case-sensitive: sum और Sum अलग होते हैं। / case-sensitive।

  5. Meaningful names रखें: totalMarks बेहतर है बजाय t के। / meaningful names रखना best practice है।


Storage Classes (short overview) / स्टोरेज क्लासेस (संक्षेप)

  • auto (default for local variables), static, extern, register — ये control करते हैं lifetime और linkage.

  • Example: static int counter; keeps value across calls. / static से function calls के बीच value बनी रहती है।


Types of Variables (मुख्य प्रकार) — Hindi + English mix

  1. Local (Automatic) Variables / लोकल (ऑटोमैटिक)

    • Declared inside a function or block. Scope limited to that block. Memory allocated when block executes. / किसी function/block के अंदर declare होते हैं; scope वही तक सीमित।

    • Example: void f(){ int a = 5; }

  2. Global Variables / ग्लोबल

    • Declared outside all functions (usually at top). Accessible from any function (unless shadowed). Lifetime is entire program. / प्रोग्राम भर में accessible; lifetime program के duration तक।

    • Example: int count = 0; (declared before main)

    • Use carefully — overuse reduces modularity.

  3. Static Variables / स्टैटिक

    • Can be global or local with static keyword. For local static, scope is local but lifetime is the whole program. For static global, linkage is limited to the file (internal linkage). / static local की value function calls के बीच बनी रहती है; file-level static file तक ही visible रहता है।

    • Example: static int calls = 0;

  4. Register Variables / रजिस्टर

    • Hints compiler to store variable in CPU register for faster access (compiler may ignore). Cannot take address using &. / आजकल compiler smart है; ये सिर्फ hint होता है।

    • Example: register int i;

  5. External Variables (extern) / एक्सटर्नल

    • extern declares a variable defined in another file. Use for multi-file programs. / multi-file programs में common variables share करने के लिए।

    • Example: In file1: int x = 10; In file2: extern int x;

  6. Constant Variables / स्थिर (const)

    • const makes variable read-only after initialization. Useful for fixed values like PI. / एक बार initialize के बाद value नहीं बदलेगी।

    • Example: const int MAX = 100;


Memory & Lifetime (Short) / मेमोरी और लाइफटाइम

  • Local (automatic): stored on stack, lifetime only during function/block execution. / stack पर, limited lifetime।

  • Global/static: stored in data segment (BSS or data), lifetime entire program. / data segment पर, program के दौरान मौजूद।

  • Register: stored in CPU register (if allocated), very fast access. / register में (यदि compiler allocate करे)।


Examples with Hindi explanations

#include <stdio.h>

int global_count = 0; // Global variable — पूरा प्रोग्राम access कर सकता है

void demo(){
    static int calls = 0; // Static local — value function calls के बीच बनी रहती है
    int local_var = 5;    // Local variable — सिर्फ इस function में valid
    register int i;       // Register hint — fast access (compiler may ignore)

    calls++;
    global_count += calls;
    printf("calls=%d, local=%d, global=%d\n", calls, local_var, global_count);
}

int main(){
    demo(); // calls=1
    demo(); // calls=2 (static preserves value)
    return 0;
}
  • Explanation (Hindi): global_count पूरे प्रोग्राम में उपयोग हो सकता है. calls static होने के कारण पहली call के बाद भी value याद रखेगा. local_var हर बार function call में नया बनाएगा.


Common Pitfalls / सावधानियाँ

  • Overusing global variables makes code hard to maintain. / ग्लोबल का ज्यादा use खराब practice है।

  • Not initializing local variables gives garbage values. Always initialize where possible. / अनइनिशियलाइज़्ड वेरियेबल से garbage मिलता है।

  • Mismatched types in expressions (e.g., mixing int and float) can cause implicit conversions—know usual promotions. / type mixing से implicit conversion होता है—dhyaan रखें।


Quick Revision Checklist (Hindi-English)

  • Declare before use / इस्तेमाल से पहले declare करो.

  • Use meaningful names / meaningful names रखें.

  • Prefer local variables for modularity / modularity के लिए local use करो.

  • Use const for read-only values / read-only के लिए const.

  • Verify storage class when sharing across files (extern) / multi-file sharing के लिए extern.

 
 
 

Comments


bottom of page