C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • What is an Array?
  • Declaring an Array
  • Initializing an Array
  • Initializing during Declaration
  • Initializing Later
  • Accessing Array Elements
  • Example Program
  • Output
  • Practical Tips

Was this helpful?

  1. Arrays

Introduction to Arrays

Arrays are crucial data structures in C that enable the storage and manipulation of collections of elements of the same data type. This comprehensive guide will cover the fundamentals of creating, initializing, accessing, and working with arrays in C.

What is an Array?

An array is a systematic arrangement of elements, all of the same data type, stored in contiguous memory locations. Each element in the array is identified by an index or a subscript.

Declaring an Array

To declare an array in C, you need to specify the data type of its elements, followed by the array name and the size of the array in square brackets.

// Syntax: dataType arrayName[arraySize];
int numbers[5]; // Example: declaring an integer array of size 5

This declaration indicates that numbers is an array capable of holding five integers.

Initializing an Array

Arrays can be initialized during declaration or at a later stage in the program.

Initializing during Declaration

int numbers[5] = {1, 2, 3, 4, 5};

Here, an integer array named numbers is declared and initialized with values 1, 2, 3, 4, and 5.

Initializing Later

int temperatures[7];
temperatures[0] = 22;
temperatures[1] = 24;
// ... continue initializing other elements

In this example, an integer array named temperatures is declared first, and its elements are initialized later in the program.

Accessing Array Elements

Array elements are accessed using their indices. The index starts at 0 and goes up to (array size - 1).

int firstNumber = numbers[0]; // Accessing the first element
int secondNumber = numbers[1]; // Accessing the second element

In C, the syntax for accessing an array element is arrayName[index]. The index represents the position of the element in the array.

Example Program

Let's create a comprehensive example program that declares, initializes, and prints the elements of an integer array.

#include <stdio.h>

int main() {
    // Declare and initialize an integer array
    int numbers[5] = {1, 2, 3, 4, 5};

    // Print the elements of the array
    printf("Elements of the array: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Output

Elements of the array: 1 2 3 4 5

This program demonstrates the complete process of declaring, initializing, and accessing elements in an integer array.

Practical Tips

  1. Array Size:

    • The array size must be a constant expression or a constant variable.

    • Arrays cannot be resized once declared.

  2. Zero-Based Indexing:

    • Remember that array indices start from 0.

  3. Traversal:

    • Use loops for efficient traversal of array elements.

  4. Initialization:

    • Initialize arrays during declaration for concise code.

  5. Bounds Checking:

    • Exercise caution to avoid accessing elements beyond the array bounds.

Understanding arrays is foundational for efficient data handling in C programming. If you have specific questions or if there are additional topics you'd like to explore, feel free to ask. Happy coding!

PreviousArraysNextProgram: Prime Number Generator

Was this helpful?