- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Arrays
![]() Share with a Friend |
C Programming - C Arrays
C Arrays
An array in C is a collection of elements of the same data type, stored in contiguous memory locations. It allows you to store multiple values in a single variable, which can be accessed by using an index or subscript. Arrays are essential for handling large sets of data efficiently and are widely used in various applications.
Key Characteristics of C Arrays:
- Homogeneous Data Type: All elements in an array must be of the same data type (e.g., all integers, all floats).
- Fixed Size: The size of an array is fixed when it is declared, meaning you cannot change its size after initialization.
- Indexed Access: Array elements are accessed using an index or subscript, with indexing starting from 0.
- Contiguous Memory Allocation: Array elements are stored in consecutive memory locations, which allows for efficient access.
Syntax for Declaring Arrays in C:
C
data_type array_name[array_size];
Where:
- data_type is the type of data to be stored in the array (e.g., int, float, char).
- array_name is the name of the array.
- array_size is the number of elements the array will hold.
Example: Basic Array Declaration
C
#include <stdio.h>
int main() {
int arr[5]; // Declare an integer array of size 5
// Initializing the array
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// Accessing array elements and displaying them
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0;
}
Explanation:
- An integer array arr of size 5 is declared.
- The array is manually initialized with values 10, 20, 30, 40, 50.
- The for loop is used to print all the elements in the array using their indices.
Array Initialization
Arrays can be initialized at the time of declaration, either with specific values or automatically.
- Explicit Initialization:
C
int arr[5] = {1, 2, 3, 4, 5}; // Initialize with specific values
- Partial Initialization:
If you specify fewer values than the array size, the remaining elements are automatically initialized to 0 (for numeric types).
C
int arr[5] = {1, 2}; // Initializes arr[0] = 1, arr[1] = 2, arr[2..4] = 0
- Implicit Size:
If the size of the array is not specified, the compiler will automatically determine it based on the number of elements in the initializer list.
C
int arr[] = {1, 2, 3, 4, 5}; // The size of the array will be 5
Accessing Array Elements
Array elements are accessed using their index, starting from 0 to n-1, where n is the size of the array.
C
int x = arr[2]; // Accessing the 3rd element of the array (index 2)
Multidimensional Arrays
C allows arrays with more than one dimension, such as 2D arrays (matrices), which can be represented as an array of arrays.
- 2D Array Declaration:
C
data_type array_name[rows][columns];
- Example of a 2D Array:
C
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Printing elements of the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("arr[%d][%d] = %d\n", i, j, arr[i][j]);
}
}
return 0;
}
Explanation:
- A 2D array arr of size 3x3 is declared and initialized.
- A nested for loop is used to iterate through the rows and columns of the array.
Arrays and Pointers
In C, arrays and pointers are closely related. The name of an array can be thought of as a pointer to the first element of the array. Therefore, you can use pointer arithmetic to access array elements.
- Array Name as a Pointer:
C
int arr[] = {1, 2, 3};
int *ptr = arr; // Pointer to the first element of the array
- Accessing Array Elements via Pointer:
C
printf("%d\n", *(ptr + 1)); // Accessing the second element of the array
Array Passing to Functions
In C, arrays are passed to functions by reference, meaning that the function receives the address of the first element of the array, and any changes made to the array inside the function affect the original array.
- Passing Array to a Function:
C
#include <stdio.h>
void modifyArray(int arr[]) {
arr[0] = 100; // Modify the first element of the array
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
modifyArray(arr);
printf("arr[0] = %d\n", arr[0]); // Output: arr[0] = 100
return 0;
}
Explanation:
- The array arr is passed to the modifyArray function by reference, allowing it to modify the original array.
Limitations of Arrays in C
- Fixed Size: The size of an array must be specified at compile time, and cannot be changed dynamically during runtime (unless using dynamic memory allocation).
- Lack of Bound Checking: C does not automatically check if an array index is out of bounds, so accessing an invalid index can lead to undefined behavior.
- Homogeneity: Arrays can only store elements of the same type.
Summary of Arrays in C
- Definition: An array is a collection of elements of the same data type stored in contiguous memory locations.
- Declaration: The syntax is data_type array_name[array_size].
- Indexing: Array elements are accessed using indices, starting from 0.
- Initialization: Arrays can be initialized at the time of declaration with specific values.
- Multidimensional Arrays: C supports arrays with more than one dimension, such as 2D arrays.
- Arrays and Pointers: Arrays are closely related to pointers, and you can use pointers to access array elements.
- Passing to Functions: Arrays are passed by reference to functions.
- Limitations: Arrays in C have a fixed size, lack automatic bound checking, and can only store elements of the same type.
Arrays are a powerful and essential tool in C programming, enabling efficient storage and manipulation of multiple values in a single variable.
