One Dimensional Array in C — Complete Tutorial
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

One Dimensional Array in C Programming

📑 On this page:
  • What is an Array?
  • What is One Dimensional Array?
  • Array Declaration
  • Array Initialization
  • Accessing Array Elements
  • Memory Representation
  • Real-World Examples
  • Advantages & Disadvantages
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • What is an array in C programming
  • What is one dimensional (1D) array
  • How to declare and initialize 1D arrays
  • How to access elements using index
  • Memory representation of 1D arrays
  • Real-world examples and use cases

What is an Array in C Language?

In C programming, an array is a collection of similar elements or data items of the same type stored at contiguous memory locations.

Arrays are used to organize the same type of data. In C, arrays are a derived data type that can store primitive data type values such as int, float, char, double, etc.

Example: If we want to store the marks obtained by a student in 5 different subjects, instead of creating 5 separate variables, we can use a single array to store all the marks.

💡 Key Concept: Arrays store values in contiguous memory locations, meaning the elements are stored one after another in memory. This allows efficient access using index numbers.

What is One Dimensional (1D) Array?

A One Dimensional (1D) Array in C is a variable that stores a group of values of the same data type in contiguous memory locations. It has only one dimension — a single row of elements.

Examples of 1D Arrays:

  • Marks: 60, 70, 99, 83, 12, 56, 73, 45, 65, 44
  • Student Roll Numbers: 11, 34, 12, 33, 12, 15, 14, 17, 18, 19
  • Employee Salaries: 85000, 45600.54, 87098.45, 23445, 888767.98, 4556.78

📌 Note: A 1D array is like a single row in a spreadsheet where each cell stores one value, and you can access each cell using its position (index).

Array Declaration in C

Before using an array in C, you must declare it like any other variable. The declaration tells the compiler about:

  • Data Type: The type of elements (int, char, float, etc.)
  • Array Name: The name of the array variable
  • Size: The number of elements the array can hold

Syntax of Array Declaration:

data_type array_name [size];

Example:

int marks[5]; // Array to store 5 integer values
float salary[10]; // Array to store 10 floating-point values
char name[20]; // Array to store 20 characters

Array Initialization

You can initialize an array at the time of declaration or later in the program.

1. Compile-Time Initialization:

int marks[5] = {60, 70, 99, 83, 12};
int marks[] = {60, 70, 99, 83, 12}; // Size auto-calculated

2. Run-Time Initialization:

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

Accessing Array Elements

Array elements are accessed using index numbers. In C, array indices start from 0.

Array Index 0 Index 1 Index 2 Index 3 Index 4
marks[5] marks[0] = 60 marks[1] = 70 marks[2] = 99 marks[3] = 83 marks[4] = 12

Example:

marks[0] = 60; // Assign 60 to first element
int x = marks[2]; // Access third element (value 99)

Memory Representation of 1D Array

When an array is declared, the compiler allocates a contiguous block of memory to store all elements.

Example: int marks[5];

  • Base Address: Address of the first element (marks[0])
  • Memory Size: Each int occupies 2 bytes (16-bit) or 4 bytes (32-bit/64-bit)
  • Total Memory: size × number_of_elements
Address at 0 index is : 1000
Address at 1 index is : 1002 (or 1004)
Address at 2 index is : 1004 (or 1008)
Address at 3 index is : 1006 (or 1012)
Address at 4 index is : 1008 (or 1016)

📝 Note: The address of an array is the address of its first element. The memory occupied depends on the data type and system architecture.

Real-World Examples & Use Cases

1. Matrix Operations: Arrays are used extensively in matrix calculations and linear algebra.

2. Database Management: RDBMS systems use arrays to store and manage data.

3. Lookup Tables: Arrays can be used to create efficient lookup tables.

4. Sorting and Searching: Arrays are fundamental in sorting and searching algorithms.

5. Data Structures: Arrays are used to implement stacks, queues, and graphs.

6. Data Mining: Arrays represent large datasets for analysis.

7. Robotics: Arrays represent object positions in 3D space.

💡 Versatility: Arrays can store integers, characters, floating-point numbers, pointers, and even complex data structures like structures and objects.

Advantages & Disadvantages of Arrays

✅ Advantages

  • Easy to access elements using index position
  • Values can be searched very easily
  • Stores multiple data items of the same type using a single name
  • Improves performance and code efficiency
  • Enables iteration through elements using loops

❌ Disadvantages

  • Array is static in nature (fixed size)
  • Must know the number of elements in advance
  • Insertion and deletion is difficult
  • Cannot store elements of different data types
  • Memory wastage if not fully used

Frequently Asked Questions

1. What is a one-dimensional array in C?

A one-dimensional array is a collection of elements of the same data type stored in contiguous memory locations. It has only one dimension and can be accessed using a single index.

2. What is the syntax to declare a 1D array?

data_type array_name[size];
Example: int marks[10];

3. What is array index in C?

Array index is an integer that identifies the position of an element within the array. Index starts from 0 for the first element and goes up to size-1 for the last element.

4. How are arrays stored in memory?

Arrays are stored in contiguous memory locations. The address of the first element is the base address, and subsequent elements are stored at consecutive memory addresses.

5. What is the difference between compile-time and run-time initialization?

Compile-time: Values are assigned during compilation.
Run-time: Values are assigned during program execution using input functions like scanf().

💡 Tip: Always remember that array index starts from 0 in C, not 1. This is a common mistake beginners make.

📖 Related Tutorials

  • Declaring 1D Arrays
  • Initialization of 1D Arrays
  • Accessing Array Elements
  • Read and Display 1D Arrays
  • Two Dimensional Arrays

Previous Topic: -->> difference while do..while & for   ||   Next topic: -->> Declaring 1D Arrays


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python If-Else FAQ Python Loops FAQ Python Lists & Dictionaries FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Control Flow & Operators FAQ Java Methods FAQ Java OOP Best Practices FAQ Java Threads & Concurrency FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.