C का sizeof Operator
In this page:
sizeof(data_type);
sizeof(variable_name);
printf("%zu", sizeof(variable_name));
sizeof Operator
sizeof operator एक type या variable द्वारा occupied bytes की संख्या return करता है, compile time पर evaluated, और यह portable code लिखने के लिए essential है जो किसी भी type के लिए एक fixed size नहीं मानता।
उदाहरण: The sizeof Operator
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%zu", sizeof(x));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Basic Types का Size
char, int, और double जैसे basic types पर sizeof apply करना दिखाता है कि current system पर हर एक कितनी memory इस्तेमाल करता है, जो अलग-अलग compilers और platforms में अलग हो सकता है।
उदाहरण: Size of Basic Types
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("char: %zu, int: %zu, double: %zu", sizeof(char), sizeof(int), sizeof(double));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrays का Size
एक array पर apply किया गया sizeof हर element द्वारा मिलकर इस्तेमाल की गई bytes की total संख्या return करता है, और इसे एक single element के size से divide करना C में array की length compute करने की standard technique है।
उदाहरण: Size of Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]);
// Print formatted text to the screen
printf("%d", length);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structures का Size
एक struct पर apply किया गया sizeof structure द्वारा occupy की गई total memory return करता है, जो alignment के लिए compiler द्वारा add की गई padding के कारण इसके individual members के sum से बड़ी हो सकती है।
उदाहरण: Size of Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point {
char label;
int x;
};
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%zu", sizeof(struct Point));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Expressions के साथ sizeof
sizeof सिर्फ एक type या variable की बजाय सीधे एक expression पर भी apply किया जा सकता है, runtime पर actually expression की value compute किए बिना यह return करते हुए कि वह expression किस type में evaluate होता।
उदाहरण: sizeof with Expressions
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
// Print formatted text to the screen
printf("%zu", sizeof(a + 1.0));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- किसी function के अंदर एक array parameter पर
sizeofइस्तेमाल करना, जहाँ यह array की बजाय एक pointer का size देता है। sizeofका result%zuकी बजाय%dसे print करना, जो 64-bit systems पर गलत हो सकता है।- यह मान लेना कि
sizeof(struct)इसके members का sum है, जब padding इसे बड़ा बना सकती है।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers