← Back to C Course | Chapter 6: Arrays & Strings | Lesson 2 of 8

C Array का Size

एक array का size declaration पर fixed होता है, और sizeof को एक element के size से divide करना यह compute करने का standard तरीका है कि इसमें कितने elements हैं, खासकर इसे किसी function को pass करने से पहले।
Syntax
c
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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

Input के लिए Arrays Size करना

user input या data की variable मात्राएं रखने के लिए बने Arrays को फिर भी एक fixed maximum capacity दी जाती है, अक्सर एक named constant से define की गई, और program अलग से track करता है कि उन slots में से कितने actually इस्तेमाल में हैं।

उदाहरण: Sizing Arrays for Input

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक function के अंदर sizeof(arr) इस्तेमाल करना जहाँ array pointer में decay हो चुका है, length की बजाय 8 देते हुए।
  2. element count पाने के लिए sizeof(arr) को sizeof(arr[0]) से divide करना भूल जाना।
  3. पुराने C89 code में एक non-constant size इस्तेमाल करना, जो allowed नहीं है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.