sizeof() Operator in Hindi
/ BCA / Programming with C and CPP
sizeof() Operator in Hindi
sizeof() Operator in Hindi
Introduction to sizeof() Operator in Hindi
जब हम C या C++ जैसी programming language सीखते हैं, तो हमें यह जानना जरूरी होता है कि कोई variable या data type memory में कितना space लेता है। इसी कार्य के लिए sizeof() operator का उपयोग किया जाता है। यह operator हमें बताता है कि किसी variable, constant या data type के लिए कितनी bytes memory allocate होती है।
C में हर data type (जैसे int, float, char) की अपनी एक specific size होती है जो उस कंप्यूटर सिस्टम की architecture (जैसे 32-bit या 64-bit) पर depend करती है। यह size जानने के लिए हम sizeof() operator का उपयोग करते हैं।
इस operator को हम parentheses के साथ या बिना parentheses के भी use कर सकते हैं। लेकिन किसी data type के साथ जब हम इसका use करते हैं तो parentheses ज़रूरी होते हैं।
// उदाहरण:
int a;
printf("%d", sizeof(a)); // Output: 4 (मान लीजिए int का size 4 bytes है)
printf("%d", sizeof(int)); // Output: 4
Getting Data Size using sizeof() Operator in Hindi
sizeof() operator का मुख्य उद्देश्य यह पता लगाना होता है कि कोई data type या variable कितनी bytes memory लेता है। इससे हमें memory optimization में मदद मिलती है और हम यह जान पाते हैं कि कौन सा data type हमारे program के लिए उपयुक्त है।
- यदि आप int, float, double या char जैसे basic data types की size जानना चाहते हैं, तो आप सीधे sizeof() का उपयोग कर सकते हैं।
- यह operator compile time पर ही value दे देता है, इसका मतलब है कि जब आप program compile कर रहे होते हैं, तभी इसका मूल्य तय हो जाता है।
// सभी प्रमुख data types के size:
printf("Size of int: %d\n", sizeof(int)); // आमतौर पर 4 bytes
printf("Size of float: %d\n", sizeof(float)); // आमतौर पर 4 bytes
printf("Size of double: %d\n", sizeof(double)); // आमतौर पर 8 bytes
printf("Size of char: %d\n", sizeof(char)); // हमेशा 1 byte
यदि आप किसी variable के साथ sizeof() का उपयोग करते हैं, तो यह उस variable के data type के आधार पर size return करता है:
int x = 10;
char ch = 'A';
double d = 100.23;
printf("Size of x: %d\n", sizeof(x)); // Output: 4
printf("Size of ch: %d\n", sizeof(ch)); // Output: 1
printf("Size of d: %d\n", sizeof(d)); // Output: 8
Using sizeof() with Variables and Datatypes in Hindi
sizeof() operator का उपयोग हम न केवल primitive data types के साथ कर सकते हैं, बल्कि user-defined data types जैसे structure, array, और pointers के साथ भी कर सकते हैं। इससे हमें बड़ी memory blocks की size का पता चलता है।
1. sizeof() with Array
यदि आप किसी array का size जानना चाहते हैं, तो sizeof() आपके लिए बहुत उपयोगी होता है। यह पूरी array की memory size return करता है।
int arr[10];
printf("Size of array: %d\n", sizeof(arr)); // Output: 40 (मान लीजिए int का size 4 bytes है)
यदि आप array के elements की संख्या जानना चाहते हैं:
int length = sizeof(arr) / sizeof(arr[0]);
printf("Total elements: %d\n", length); // Output: 10
2. sizeof() with Structure
Structure एक user-defined data type होता है जिसमें हम कई variables को एक साथ store कर सकते हैं। हम sizeof() operator का उपयोग करके structure की total size भी जान सकते हैं।
struct Student {
int id;
char name[20];
float marks;
};
printf("Size of structure: %d\n", sizeof(struct Student));
// आमतौर पर output 28 या उससे अधिक depending on padding
3. sizeof() with Pointer
Pointer variable के लिए sizeof() operator उस pointer की size return करता है, न कि जिस variable को वह point कर रहा है उसकी। यह भी system architecture पर निर्भर करता है। जैसे 64-bit system में pointer की size 8 bytes होती है।
int *ptr;
printf("Size of pointer: %d\n", sizeof(ptr)); // Output: 8 (on 64-bit system)
Additional Information about sizeof() Operator in Hindi
sizeof() की कुछ विशेषताएं:
- यह compile time operator है यानी value compile होने से पहले ही तय हो जाती है।
- इसका return type हमेशा unsigned integer होता है।
- Parentheses का use करना आवश्यक है जब आप type के साथ use कर रहे हों।
Parentheses कब जरूरी होते हैं?
- Variable के साथ: parentheses वैकल्पिक हैं —
sizeof xयाsizeof(x) - Data type के साथ: parentheses अनिवार्य हैं —
sizeof(int)
Integer Promotion और sizeof()
यदि आप sizeof('A') लिखते हैं, तो यह char के बजाय int मानकर size देता है क्योंकि character constants C में integer type माने जाते हैं। इसलिए यह 1 नहीं बल्कि 4 return करेगा।
printf("Size of 'A': %d\n", sizeof('A')); // Output: 4 (not 1)
Sizeof Operator Table (Most Common Types)
| Data Type | Size (Bytes) |
|---|---|
| char | 1 |
| int | 4 |
| float | 4 |
| double | 8 |
| short int | 2 |
| long int | 8 |
| pointer (on 64-bit) | 8 |
Use Cases of sizeof() in Programs
- Memory allocation करते समय malloc() या calloc() में sizeof का उपयोग करते हैं।
- Structure या array का कुल size निकालने में मदद करता है।
- Pointer के साथ जब dynamic memory manage करनी हो तब उपयोगी होता है।
- Variable को भेजने या compare करने के लिए exact size जानना ज़रूरी होता है।
Common Mistakes while using sizeof() Operator in Hindi
- Structure में padding के कारण actual size ज्यादा हो सकता है, इसलिए assumption पर न जाएं।
- Pointer के साथ sizeof() से pointed data की size नहीं मिलती, केवल pointer की size मिलती है।
- sizeof('A') को 1 मान लेना गलत है क्योंकि यह character constant है, और int में promote हो जाता है।
Final Notes on sizeof() Operator in Hindi
- sizeof() एक बहुत महत्वपूर्ण operator है जो आपको memory की सही जानकारी देता है।
- यह operator आपको efficient और सुरक्षित code लिखने में मदद करता है।
- यह beginners के लिए भी बहुत सरल और उपयोगी टूल है जो उन्हें memory concepts को समझने में सहायता करता है।