C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we will learn about arrays in the C programming language. Arrays are a collection of elements of the same data type, stored in contiguous memory locations. They are useful for organizing and manipulating data when dealing with multiple items of the same type.
To declare an array, specify the data type, followed by the name of the array and the size enclosed in square brackets. The size indicates the number of elements in the array.
int myArray[5]; // Declare an integer array of size 5
You can initialize an array when declaring it by providing values enclosed in braces.
int myArray[5] = {1, 2, 3, 4, 5};
If you don't specify the size, the compiler will automatically determine it based on the provided values.
int myArray[] = {1, 2, 3, 4, 5}; // The size of the array is 5
To access an array element, use the array name followed by the index enclosed in square brackets. The index starts from 0 and goes up to size - 1.
int firstElement = myArray[0]; // Access the first element of the array int thirdElement = myArray[2]; // Access the third element of the array
You can modify the value of an array element by assigning a new value to it using its index.
myArray[1] = 42; // Modify the second element of the array
Use a loop to iterate over an array. The most common approach is to use a for
loop.
#include <stdio.h> int main() { int myArray[5] = {1, 2, 3, 4, 5}; // Iterate over the array for (int i = 0; i < 5; i++) { printf("%d ", myArray[i]); } return 0; }
The output will be:
1 2 3 4 5
That's it for our C array tutorial. Arrays are a fundamental data structure for organizing and manipulating data when working with multiple items of the same type. Understanding how to declare, initialize, access, and modify array elements, as well as iterating over an array, will help you work effectively with large data sets in your C programs.
C array definition and declaration:
#include <stdio.h> int main() { // Declaration and definition of an array int numbers[5]; return 0; }
numbers
capable of holding 5 integers.How to initialize arrays in C:
#include <stdio.h> int main() { // Initializing an array int numbers[5] = {1, 2, 3, 4, 5}; return 0; }
numbers
with values 1 to 5.Accessing elements in C arrays:
#include <stdio.h> int main() { int numbers[5] = {1, 2, 3, 4, 5}; // Accessing elements printf("Element at index 2: %d\n", numbers[2]); return 0; }
Multi-dimensional arrays in C:
#include <stdio.h> int main() { // Declaration and initialization of a 2D array int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; return 0; }
Dynamic arrays in C programming:
#include <stdio.h> #include <stdlib.h> int main() { // Dynamic array allocation int *dynamicArray = (int *)malloc(5 * sizeof(int)); // Remember to free memory when done free(dynamicArray); return 0; }
malloc
and frees it later.C array size and memory allocation:
#include <stdio.h> int main() { int numbers[5] = {1, 2, 3, 4, 5}; // Size of the array int size = sizeof(numbers) / sizeof(numbers[0]); return 0; }
sizeof
operator.