C Array का Size
In this page:
data_type array_name[size] = {value1, value2};
int length = sizeof(array_name) / sizeof(array_name[0]);
Declaration पर Fixed Size
एक array का size उस moment fixed हो जाता है जब यह declare होता है, square brackets के अंदर एक number की तरह specify किया गया, और कुछ दूसरी languages के उलट, एक standard C array इसके बाद बढ़ या सिकुड़ नहीं सकता।
उदाहरण: Fixed Size at Declaration
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[5];
// Print formatted text to the screen
printf("%zu", sizeof(arr));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
sizeof से Array का Size ढूंढना
एक array पर apply किया गया sizeof operator इसके occupy किए गए total bytes return करता है, और इसे sizeof(arr[0]) इस्तेमाल करके एक single element के size से divide करना यह compute करने का standard तरीका है कि इसमें कितने elements हैं।
उदाहरण: Finding an Array's Size with sizeof
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Declare length and set it to sizeof(arr) / sizeof(arr[0])
int length = sizeof(arr) / sizeof(arr[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
Array Size बनाम Elements की संख्या
एक array का total byte size और इसका element count related हैं लेकिन अलग हैं: total bytes element count गुणा हर individual element के size के बराबर है, जो एक byte से बड़े types के लिए मायने रखता है।
उदाहरण: Array Size vs Number of Elements
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
double arr[4];
// Print formatted text to the screen
printf("Bytes: %zu, Elements: %zu", sizeof(arr), sizeof(arr) / sizeof(arr[0]));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions को Pass किए गए Arrays का Size
जब एक array को एक function parameter की तरह pass किया जाता है, यह एक pointer में decay हो जाता है, इसलिए उस function के अंदर sizeof original array की बजाय pointer का size report करता है, element count को अलग से pass करना ज़रूरी बनाते हुए।
उदाहरण: Size of Arrays Passed to Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
void showSize(int arr[]) {
// Print formatted text to the screen
printf("%zu", sizeof(arr));
}
// Program execution starts in main()
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
showSize(numbers);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Input के लिए Arrays Size करना
user input या data की variable मात्राएं रखने के लिए बने Arrays को फिर भी एक fixed maximum capacity दी जाती है, अक्सर एक named constant से define की गई, और program अलग से track करता है कि उन slots में से कितने actually इस्तेमाल में हैं।
उदाहरण: Sizing Arrays for Input
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX_SIZE (replaced by the preprocessor)
#define MAX_SIZE 10
// Program execution starts in main()
int main() {
int data[MAX_SIZE];
// Print formatted text to the screen
printf("%d", MAX_SIZE);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक function के अंदर
sizeof(arr)इस्तेमाल करना जहाँ array pointer में decay हो चुका है, length की बजाय 8 देते हुए। - element count पाने के लिए
sizeof(arr)कोsizeof(arr[0])से divide करना भूल जाना। - पुराने C89 code में एक non-constant size इस्तेमाल करना, जो allowed नहीं है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: