Difference Between Local and Global Variable
Programming me variables do type ke hote hain — Local aur Global. Dono ka use alag-alag situations me hota hai aur unki accessibility bhi different hoti hai. Neeche inka simple aur clear difference diya hua hai.
Local Variable (Hinglish Explanation)
Local variable woh variable hota hai jo sirf function ke andar declare kiya jata hai. Is variable ko aap function ke bahar use nahi kar sakte. Jab function run hota hai tab hi ye variable active hota hai.
// Example of Local Variable
#include <stdio.h>
void test() {
int x = 10; // local variable
printf("%d", x);
}
int main() {
test();
// printf("%d", x); // ERROR: x yaha access nahi ho sakta
}
Global Variable (Hinglish Explanation)
Global variable woh hota hai jo saare functions ke bahar declare kiya jata hai. Ye program ke kisi bhi function me access ho sakta hai. Iski life program ke pure execution tak hoti hai.
// Example of Global Variable
#include <stdio.h>
int x = 20; // global variable
void test() {
printf("%d", x);
}
int main() {
test();
printf("%d", x);
}
Difference Between Local and Global Variable
| Local Variable | Global Variable |
|---|---|
| Function ke andar declare hota hai. | Saare functions ke bahar declare hota hai. |
| Sirf usi function ke andar accessible. | Pure program me accessible. |
| Function ke run hote hi create hota hai aur khatam hote hi destroy. | Program ke start se end tak exist karta hai. |
| Memory usage kam hota hai. | Memory program ke duration tak occupied rehti hai. |
| Values accidentally overwrite hone ka chance kam. | Kai functions use karte hain to value overwrite ho sakti hai. |
Final Hinglish Line
Local variable function ke andar limited hota hai, jabki global variable pure program me use kiya ja sakta hai. Dono ka use situation ke hisaab se hota hai.
Comments
Post a Comment