An array in C is a collection of elements of the same type stored in contiguous memory locations. It allows you to store multiple values of the same data type under a single variable name.
Important Points of Array
1. All elements in an array must be of the same type (e.g., all integers, all characters, etc.).
2. The array size must be fixed when it is declared (though it can be a constant).
3. Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.
Syntax: to declare a Array
type arrayName[size];
Explanation:
- type: The data type of the array elements (e.g., int, float, char).
- arrayName: The name of the array.
- size: The number of elements the array will hold.
Syntax: to access a Array element
// declare a index of Array element
// First array element start from 0 index and so on increment by 1.
arrayName[index];
Example:
#include <stdio.h>
int main() {
// Declare an array of integers with 5 elements
int arr[5] = {10, 20, 30, 40, 50};
// Access and print each element
printf("First element: %d\n", arr[0]); // Output: 10
printf("Second element: %d\n", arr[1]); // Output: 20
printf("Third element: %d\n", arr[2]); // Output: 30
printf("Fourth element: %d\n", arr[3]); // Output: 40
printf("Fifth element: %d\n", arr[4]); // Output: 50
return 0;
}
Explanation:
- int arr[5]: Declares an array of integers with 5 elements.
- {10, 20, 30, 40, 50}: Initializes the array with values.
- To Accesses individual elements of the array: by index like arr[0], arr[1], arr[2], arr[3], arr[4]
C Array – Questions and Answers
Q 1: What is an array in C?
Ans: An array is a collection of elements of the same data type stored in contiguous memory.
Q 2: How do you declare an array in C?
Ans: int arr[5];
Q 3: What is array indexing?
Ans: Accessing elements using index numbers starting from 0.
Q 4: Can array size be changed at runtime?
Ans: No, static arrays have fixed size.
Q 5: How are arrays passed to functions?
Ans: By passing the base address of the array.
C Array – Objective Questions (MCQs)
Q1. What is an array in C?
Q2. Which of the following is the correct way to declare an integer array of size 5?
Q3. What is the index of the first element in a C array?
Q4. How can you access the third element of an array arr?
Q5. What will happen if you try to access an element outside the array size?