An array is a collection of elements of the same type, stored in contiguous memory locations. It provides a convenient way to store and access multiple values of the same data type using a single variable name. The elements in an array are accessed using an index, starting from 0 for the first element.

Arrays in C have several characteristics that are important to understand:

Fixed Size: Arrays have a fixed size, which is determined at the time of declaration. The size represents the number of elements the array can hold, and it cannot be changed during the program's execution.

Contiguous Memory: Array elements are stored in contiguous memory locations. This means that each element in the array occupies a consecutive block of memory, making it efficient for accessing elements using index-based notation.

Zero-based Indexing: Array indices in C start from 0. The first element is accessed using index 0, the second element with index 1, and so on. The last element is accessed using the index size - 1, where size is the size of the array.

Homogeneous Elements: Arrays can hold elements of the same data type. For example, an array of integers can only store integer values, an array of characters can store individual characters, and so on.

Random Access: Arrays allow random access to their elements. This means that any element in the array can be accessed directly by its index, allowing for efficient retrieval and modification of values.

Size Calculation: The size of an array can be calculated using the sizeof operator. The total memory occupied by an array can be determined by multiplying the size of each element by the number of elements in the array.

Initialization: Arrays can be initialized at the time of declaration by providing a comma-separated list of values enclosed in curly braces {}. The number of values provided should match the size of the array.

Out-of-bounds Access: Care should be taken to avoid accessing array elements outside the valid index range. Accessing elements beyond the array boundaries can lead to undefined behavior and can cause program crashes or incorrect results.

Understanding these characteristics is crucial for effectively working with arrays in C and ensuring proper memory management and data access.

Declaring and accessing arrays:

Declaring and accessing arrays

To declare an array in C, you specify the data type of the elements in the array, followed by the name of the array and the size of the array. The general syntax for declaring an array is as follows:

datatype arrayName[arraySize];

Here's an example of declaring arrays of different data types:

To access the elements of an array, you use the array name followed by the index of the element you want to access within square brackets [ ]. Remember that array indices start from 0.


Element 0: 10

Element 2: 30

Modified Element 3: 60

Multidimensional arrays

In addition to one-dimensional arrays, C also supports multidimensional arrays, which are arrays with multiple dimensions or indices. They can be thought of as arrays of arrays, forming a matrix-like structure. Common examples include 2D arrays (matrices) and 3D arrays (cubes).

To declare a multidimensional array in C, you specify the data type of the elements, followed by the name of the array, and the size of each dimension. The general syntax for declaring a multidimensional array is as follows:

datatype arrayName[size1][size2]...[sizeN];


Element at (0, 0): 1

Element at (1, 2): 7

Element at (2, 3): 12

In the above example, we declared a 2D array named "matrix" with dimensions 3x4. We initialized the array elements with values, and then accessed and printed specific elements using the index notation matrix[rowIndex][columnIndex].

Similarly, you can declare and work with higher-dimensional arrays, such as 3D arrays or arrays with more dimensions. The concept remains the same, with each additional dimension adding an extra set of indices.

int cube[2][3][4];  // 3D array of size 2x3x4

To access elements in higher-dimensional arrays, you need to specify the indices for each dimension.

cube[0][1][2] = 42;  // Accessing and assigning value to a 3D array element

By using multidimensional arrays, you can represent and manipulate structured data in a tabular or matrix-like format in C.

Array initialization and manipulation:

Initialization at declaration:

In the above examples, arrays are declared and initialized with values enclosed in curly braces {}. The size of the array can be explicitly specified (e.g., int numbers[5]) or automatically determined based on the number of elements provided (e.g., float grades[]).

Manipulation after declaration:

In this example, an array numbers is declared without initialization. Values are assigned to individual elements using the assignment operator (=) and the index notation.

Array manipulation using loops:

In this example, a loop is used to access and modify each element of the numbers array. The loop iterates over the indices of the array, allowing you to perform operations on each element.

Array manipulation using library functions:

C provides several library functions that can be used for array manipulation, such as memcpy, memset, strcpy, strcat, etc. These functions allow you to copy arrays, set values, concatenate strings, and perform other operations efficiently.

In this example, the memcpy function is used to copy the contents of the source array to the destination array. The strlen function is used to determine the size of the source array dynamically.

Array traversal and searching:

Array traversal refers to the process of accessing and examining each element of an array. Array searching involves finding a specific value or condition within an array. 

Here are examples of array traversal and searching in C:


10 20 30 40 50

In this example, the for loop is used to iterate over each element of the numbers array. The loop starts from index 0 and continues until the index reaches the size of the array. Each element is printed using the index notation numbers[i].

Array Searching (Linear Search):

In this example, a linear search is performed to find the target value in the numbers array. The loop iterates over each element, comparing it with the target value using the == operator. If a match is found, the loop is terminated using the break statement, and the found flag is set to 1. Finally, based on the value of the found flag, an appropriate message is displayed.

These examples demonstrate basic array traversal and searching techniques in C. Depending on your requirements, you can apply more advanced searching algorithms like binary search or use library functions such as memcmp for comparing arrays.

Output:- Target found in the array.

Conditional (Ternary) Operator:

The conditional operator ?: provides a shorthand way of writing an if-else statement in a single expression. It allows you to make a decision based on a condition and assign a value accordingly.

These control flow structures provide the ability to make decisions, repeat code, and control the flow of execution in a program. By utilizing these structures effectively, you can create more dynamic and efficient code.