Declaring and Using Arrays in C

Learn how to declare and use arrays in C programming with simple examples. Perfect for beginners using Debian 12 and Vim.

Declaring and Using Arrays in C

An array in C is a collection of elements of the same type stored in contiguous memory locations.

Declaring Arrays

Syntax to declare an array:

type arrayName[arraySize];

Example:

int numbers[5];

Initializing Arrays

You can initialize an array during declaration:

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

Accessing Array Elements

Access elements using indices starting from 0:

printf(\\"%d\\", numbers[0]);  // prints 1

Example Program

#include 

int main() 
    int numbers[5] = 10, 20, 30, 40, 50;
    for (int i = 0; i < 5; i++) 
        printf(\\"Element %d: %d\\\
\\", i, numbers[i]);
    
    return 0;

Compiling and Running

gcc arrays.c -o arrays
./arrays

Expected Output

Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

Summary

Arrays store multiple values of the same type and allow easy access by index.

Subscribe to Our YouTube for More

Download as PDF

https://blog.arashtad.com/updates/declaring-and-using-arrays-in-c/?feed_id=12993&_unique_id=689233d9d728b

Comments

Popular posts from this blog

Why Smart Contracts Are Revolutionary? (Smart Contracts Use Cases)

How to make a skybox in Three.js: A perfect guide

A Complete Roadmap for Becoming a Back-end Developer