ZeroToSky
C Programming Tutorial 81 of 206
Introduction & Setup
  • Introduction to C
  • History of C
  • Features of C
  • Applications of C
  • Advantages and Limitations of C
  • C Standards and Implementations
  • How C Programs Run
  • Setting Up C Development Environment
  • Your First C Program
  • Basic Syntax of C
  • Structure of a C Program
  • Compilation Process in C

All 12 lessons →

Tokens & Basics
  • Tokens in C
  • Keywords in C
  • Identifiers in C
  • Constants in C
  • Variables in C
  • Scope Rules in C
  • Storage Classes in C
  • Comments in C

All 8 lessons →

Data Types & I/O
  • Data Types in C
  • Primitive Data Types
  • Derived Data Types
  • Type Modifiers
  • Type Conversion in C
  • Type Casting in C
  • sizeof Operator
  • Input and Output in C
  • printf() in C
  • scanf() in C
  • Character I/O: getchar and putchar
  • String I/O: gets/fgets and puts
  • Format Specifiers in C

All 13 lessons →

Operators & Expressions
  • Operators in C
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Conditional Operator
  • Comma Operator
  • sizeof and Address Operators
  • Operator Precedence and Associativity
  • Expression Evaluation in C
  • Short-Circuit Evaluation

All 13 lessons →

Control Flow
  • Decision Making in C
  • if Statement
  • if-else Statement
  • else-if Ladder
  • Nested if
  • switch Statement
  • Loops in C
  • for Loop
  • while Loop
  • do-while Loop
  • Nested Loops
  • break Statement
  • continue Statement
  • goto Statement

All 14 lessons →

Functions
  • Functions in C
  • Function Declaration and Definition
  • Function Arguments
  • Return Values
  • Call by Value
  • Call by Reference Using Pointers
  • Parameter Passing Techniques
  • Main Function in C
  • Recursion in C
  • Types of Recursion
  • Inline Functions
  • Nested Functions
  • Function Pointers
  • Variadic Functions

All 14 lessons →

Arrays
  • Arrays in C
  • Declaration and Initialization of Arrays
  • Accessing Array Elements
  • Traversing an Array
  • Passing Arrays to Functions
  • Array Parameters and Pointers
  • One-Dimensional Arrays in C
  • Two Dimensional Arrays
  • Multidimensional Arrays in C
  • Passing 2D Arrays to Functions
  • Array of Pointers
  • Variable Length Arrays

All 12 lessons →

Strings
  • Strings in C
  • String Declaration and Initialization
  • String Length
  • String Traversal
  • String Comparison
  • String Copy
  • String Concatenation
  • Substring Search
  • String Tokenization
  • String Reverse
  • Palindrome String in C
  • String Pattern Searching
  • String Literals

All 13 lessons →

Pointers
  • Pointers in C
  • Address and Dereference Operators
  • Pointer Arithmetic
  • Pointers and Arrays
  • Pointer to Pointer
  • Void Pointers
  • Null Pointers
  • Dangling Pointers
  • Wild Pointers
  • Constant Pointers and Pointer to Constant
  • Function Pointers
  • Array of Pointers
  • Pointers to Functions
  • Pointers to Structures
  • Pointer Type Casting

All 15 lessons →

Structures & Unions
  • Structures in C
  • Structure Members
  • Array of Structures
  • Nested Structures
  • Pointers to Structures
  • Passing Structures to Functions
  • Structure Alignment and Padding
  • Self-Referential Structures
  • Bit Fields in Structures
  • Unions in C
  • Structure vs Union
  • Enumeration in C
  • typedef in C

All 13 lessons →

Dynamic Memory & Memory
  • Program Memory Layout in C
  • Stack and Heap in C
  • malloc()
  • calloc()
  • realloc()
  • free()
  • Dynamic Arrays
  • Memory Leaks
  • Dangling Memory
  • Double Free
  • Use After Free
  • Memory Safety Best Practices

All 12 lessons →

Preprocessor & Headers
  • C Preprocessor
  • Header Files in C
  • #include Directive
  • #define and Macros
  • Function-like Macros
  • Conditional Compilation
  • #ifdef, #ifndef and #endif
  • Predefined Macros
  • Pragma Directive
  • Include Guards

All 10 lessons →

File Handling
  • File Handling in C
  • File Pointers
  • fopen() and fclose()
  • Reading a File
  • Writing to a File
  • Appending to a File
  • fgetc() and fputc()
  • fgets() and fputs()
  • fprintf() and fscanf()
  • Binary File Handling
  • EOF, getc() and feof()
  • Read/Write Structures to Files
  • Delete a File
  • File Error Handling

All 14 lessons →

Error Handling & Debugging
  • Error Handling in C
  • perror()
  • strerror()
  • errno
  • Assertions in C
  • Debugging C Programs
  • Common C Runtime Errors
  • Divide by Zero Handling
  • goto for Error Handling

All 9 lessons →

Date, Time & Misc
  • Date and Time in C
  • time() Function
  • clock() Function
  • Random Numbers in C
  • Linkage in C
  • External and Static Linkage
  • Storage Duration
  • Integer Overflow and Undefined Behavior
  • Implementation-Defined vs Undefined Behavior

All 9 lessons →

Advanced C
  • Generic Selection (_Generic)
  • Variadic Functions
  • Input/Output System Calls
  • Signals in C
  • Signal Handling
  • Socket Programming in C
  • Multithreading in C
  • Threads and Synchronization
  • Atomic Operations
  • Memory-Mapped Files
  • Low-Level Systems Programming

All 11 lessons →

Practice & Projects
  • C Programming Examples
  • C Exercises
  • C Fundamental Practice Problems
  • C Functions Practice Problems
  • C Compound Data Type Practice Problems
  • C Pattern Programs
  • C Array Programs
  • C String Programs
  • C Pointer Programs
  • C Structure and Union Programs
  • C File Handling Projects
  • C Mini Projects
  • C Interview Questions
  • Data Structures and Algorithms in C

All 14 lessons →

One-Dimensional Arrays in C

Learn one-dimensional arrays in C, including declaration, initialization, accessing elements, updating values, and traversing arrays with loops.

Gaurav 2 min read Sign in to save
On this page

What Is a One-Dimensional Array?

A one-dimensional array is a collection of elements of the same data type stored under a single variable name.

For example, instead of creating five separate variables:

int n1 = 10;
int n2 = 20;
int n3 = 30;
int n4 = 40;
int n5 = 50;

You can use an array:

int numbers[5] = {10, 20, 30, 40, 50};

This makes it easier to store and process multiple related values.


Declaring an Array

The basic syntax is:

data_type array_name[size];

Example:

int numbers[5];

This creates an integer array that can hold 5 elements.

The elements are indexed from 0 to 4:

numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]

Initializing an Array

You can initialize an array when declaring it:

int numbers[5] = {10, 20, 30, 40, 50};

You can also let the compiler determine the size:

int numbers[] = {10, 20, 30, 40, 50};

In this case, the compiler creates an array with 5 elements.


Accessing Array Elements

Use the index to access an element.

int numbers[5] = {10, 20, 30, 40, 50};

printf("%d", numbers[2]);

Output:

30

Remember that array indexing starts at 0:

numbers[0] → 10
numbers[1] → 20
numbers[2] → 30
numbers[3] → 40
numbers[4] → 50

Updating an Array Element

You can change an element using its index.

int numbers[5] = {10, 20, 30, 40, 50};

numbers[2] = 100;

printf("%d", numbers[2]);

Output:

100

Only the element at index 2 is changed.


Traversing an Array

A for loop is commonly used to access every element.

#include <stdio.h>

int main()
{
    int numbers[5] = {10, 20, 30, 40, 50};

    for (int i = 0; i < 5; i++)
    {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Output:

10 20 30 40 50

The loop starts at index 0 and continues until index 4.


Taking Array Input

You can use a loop to read values from the user.

#include <stdio.h>

int main()
{
    int numbers[5];

    for (int i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
    }

    return 0;
}

Each input value is stored at the corresponding index.


Finding the Array Length

When the array is declared in the same scope, you can calculate the number of elements using:

int numbers[] = {10, 20, 30, 40, 50};

int size = sizeof(numbers) / sizeof(numbers[0]);

printf("%d", size);

Output:

5

sizeof(numbers) gives the total size of the array in bytes, while sizeof(numbers[0]) gives the size of one element.


Common Mistakes

Accessing an Invalid Index

For:

int numbers[5];

the valid indexes are:

0, 1, 2, 3, 4

This is invalid:

numbers[5] = 100;

It accesses memory outside the array's valid range and can cause undefined behavior.

Forgetting That Indexing Starts at 0

The first element is:

numbers[0]

not:

numbers[1]

Common Uses

One-dimensional arrays are commonly used for:

  • Storing lists of numbers
  • Searching for values
  • Sorting data
  • Calculating sums and averages
  • Storing test scores
  • Implementing basic DSA algorithms

Key Takeaway

A one-dimensional array stores multiple values of the same type using a single variable name.

Remember:

Declaration → int numbers[5];

Indexing    → 0 to 4

Access      → numbers[index]

Traversal   → Usually with a loop

Arrays are one of the most important building blocks for learning data structures and algorithms in C.

Found a problem on this page, or something that could be clearer?

Goes straight to the author. Not published anywhere.