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

C Arrays परिचय

एक array boxes की एक row है जो same तरह की value रखते हैं, हर एक का number 0 से शुरू होता है। यह number से खोले जाने वाले lockers की एक row जैसा है।
Syntax
c
data_type array_name[size];

data_type array_name[size] = {value1, value2, value3};

array_name[index];  // access an element

Array क्या है?

क्योंकि सारे elements एक contiguous memory block में बैठते हैं, compiler array के starting address और element के index से किसी भी element का address instantly compute कर सकता है, यही वजह है कि array access O(1) है, एक search की ज़रूरत नहीं।

उदाहरण: What is an Array?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int numbers[3] = {10, 20, 30};
	// Print formatted text to the screen
	printf("%d", numbers[0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Arrays Declare और Initialize करना

अगर आप इसे explicitly छोड़ दें तो braces के अंदर values की संख्या array का size decide करती है, जैसे int nums[] = {1,2,3} exactly 3 elements का एक array बनाता है।

एक size दिए जाने पर elements को uninitialized छोड़ना उन्हें unpredictable garbage values देता है, zero नहीं।

उदाहरण: Declaring and Initializing Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int nums[] = {1, 2, 3};
	// Print formatted text to the screen
	printf("%d", nums[2]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Array Elements Access करना

C कोई bounds checking नहीं करता, इसलिए एक 5-element array पर arr[10] पढ़ना या लिखना ठीक compile होता है लेकिन nearby memory corrupt करता है या runtime पर crash करता है -- अगर आपको bounds खुद check करने हों तो हमेशा array के declared size का track अलग से रखें।

उदाहरण: Accessing Array Elements

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};
	// Print formatted text to the screen
	printf("%d", arr[2]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Array Elements Modify करना

arr[i] = value assign करना सिर्फ उस single memory slot को overwrite करता है; बाकी array untouched रहता है। इस तरह आप array contents को incrementally बनाते हैं, जैसे एक loop के अंदर एक बार में एक entry user input से array भरना।

उदाहरण: Modifying Array Elements

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	arr[1] = 99;
	// Print formatted text to the screen
	printf("%d", arr[1]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Arrays के जरिए Loop करना

एक typical traversal loop index 0 से size-1 तक inclusive चलता है -- i < size की बजाय i <= size तक loop करना C में सबसे common off-by-one bugs में से एक है, क्योंकि यह array के actual bounds से एक element आगे पढ़ता है।

उदाहरण: Looping Through Arrays

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};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 5; i++) {
		// Print formatted text to the screen
		printf("%d ", arr[i]);
	}
	// 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. 5 size के array में arr[5] पढ़ना या लिखना, जब valid indexes 0 से 4 हैं।
  2. एक uninitialized local array इस्तेमाल करना और इसे print करना, जो garbage दिखाता है।
  3. एक array को दूसरे को a = b; से assign करना, जो 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.