sizeof एक कंपाइल-टाइम ऑपरेटर है (अधिकांश मामलों में)। इसके ऑपरेंड का **मूल्यांकन नहीं किया जाता** - केवल इसका टाइप जांचा जाता है ताकि आकार निर्धारित हो सके।
उदाहरण
#include <stdio.h>
double my_function() {
printf("This is a test function\n");
return 123456789;
}
int main() {
int x;
x = sizeof(printf("Hello World")); // printf निष्पादित नहीं होगा
printf("The size: %d\n", x);
x = sizeof(my_function()); // my_function निष्पादित नहीं होगा
printf("The size: %d\n", x);
return 0;
}
आउटपुट
The size: 4 The size: 8
व्याख्या
sizeof(printf("Hello World")):printfका रिटर्न टाइपintहै (4 बाइट्स)। स्ट्रिंग प्रिंट नहीं होती।sizeof(my_function()): रिटर्न टाइपdoubleहै (8 बाइट्स)। फंक्शन कॉल नहीं होता।
अपवाद: वेरिएबल लेंथ ऐरे (VLA) - C99
int n = 10;
sizeof(int[n++]); // यहां n++ **निष्पादित होता है** क्योंकि VLA आकार रनटाइम पर निर्धारित होता है
printf("%d", n); // 11 प्रिंट होगा
महत्वपूर्ण निष्कर्ष
sizeofअभिव्यक्ति के **साइड इफेक्ट्स** नहीं होते (VLA को छोड़कर)।- यह कंपाइल टाइम पर हल होता है (VLA के अलावा)।
sizeof variable(बिना पैरेंथेसिस) भी वैध है जब ऑपरेंड टाइप नाम न हो।