In C, arrays are a fundamental data structure used to store multiple values of the same type in a contiguous memory block. They allow for easy access and manipulation of a collection of related data. Understanding how arrays work and the different types of arrays, such as character arrays and static vs automatic local arrays, is essential for efficient programming in C.
Let’s break down the key concepts of arrays in C.
An array in C is defined by specifying the type of elements it will store, followed by the name of the array, and the number of elements it can hold (its size).
type arrayName[arraySize];
int, float, char).#include <stdio.h>
int main() {
// Define an array of 5 integers
int numbers[5] = {1, 2, 3, 4, 5};
// Access and print array elements
for (int i = 0; i < 5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
return 0;
}
numbers of size 5 is defined.{1, 2, 3, 4, 5}.numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
numbers[3] = 4
numbers[4] = 5
In C, a character array is a special kind of array used to store strings. Since strings in C are represented as arrays of characters, a character array is used to hold a sequence of characters, with the last character always being a null terminator ('\0').
A string can be defined by initializing a character array with a string literal, which is automatically terminated by a null character.
char stringName[arraySize];
Alternatively, you can define the array as:
char stringName[] = "Hello";
#include <stdio.h>
int main() {
// Define a character array (string)
char name[] = "John";
// Print the string
printf("Hello, %s!\n", name);
return 0;
}
"John" is stored in a character array name. In memory, it will look like this: {'J', 'o', 'h', 'n', '\0'}.%s format specifier is used in printf to print the string (i.e., the characters up to the null terminator).Hello, John!
"Hello" requires an array of at least 6 characters (5 characters for "Hello" and 1 for '\0').In C, arrays can be declared as static or automatic local arrays, which determines their lifetime, memory allocation, and scope.
#include <stdio.h>
void printNumbers() {
int numbers[3] = {1, 2, 3}; // Automatic array
for (int i = 0; i < 3; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
}
int main() {
printNumbers(); // Call function with automatic array
return 0;
}
static keyword.#include <stdio.h>
void printNumbers() {
static int numbers[3] = {1, 2, 3}; // Static array
// Modify the array value
numbers[0] += 10;
for (int i = 0; i < 3; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
}
int main() {
printNumbers(); // First call
printNumbers(); // Second call
return 0;
}
printNumbers() is called, the array is initialized with {1, 2, 3}.printNumbers() is called, the array retains its previous state (numbers[0] = 11), because it is a static array.numbers[0] = 11
numbers[1] = 2
numbers[2] = 3
numbers[0] = 11
numbers[1] = 2
numbers[2] = 3
| Feature | Automatic Local Arrays | Static Local Arrays |
|---|---|---|
| Memory Allocation | Allocated and deallocated when the function is called and returns. | Allocated once when the program starts and persists throughout the program. |
| Lifetime | Only exists while the function is executing. | Exists for the lifetime of the program. |
| Initialization | Initialized every time the function is called. | Initialized only once, the first time the function is called. |
| Persistence Between Calls | Values are lost after the function exits. | Values persist between function calls. |
Defining Arrays:
Character Arrays:
'\0').Automatic Local Arrays:
Static Local Arrays:
Understanding these array types helps manage memory effectively and allows for better organization of data in your programs.
Open this section to load past papers