HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to FortranVariablesOperatorsConditions (if)Loops (do)ArraysFunctions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
🔁 Condition & Loop
📋 Array
🧩 Functions & Subroutines
Fortran›Arrays

Arrays

1-indexed arrays, assignment with [ ] and whole-array operations like sum.

Simple enough for a 5-year-old to understand

Create an array and access elements

Fortran arrays are 1-INDEXED! The first element is a(1).

Example code
1
program h
2
integer :: a(3)
An array with 3 elements.
DataAn array with 3 elements.
3
a = [5, 10, 15]
Array assignment.
DataArray assignment.
4
print *, a(1)
First element: 5 (starts at 1!)
Output / InputFirst element: 5 (starts at 1!)
5
print *, sum(a)
Sum: 30
Output / InputSum: 30
6
end program h
DataOutput / Input
💡 Hint: Fortran counts from 1 due to scientific tradition: a(1) is the first, a(n) is the last element.

About this topic

Arrays are the heart of Fortran. The language treats arrays as first-class values: adding two arrays, assigning a value to an entire array, or taking a slice can all be done in a single statement.

In Fortran, array indices start at one by default. If you prefer, you can explicitly specify the lower and upper bounds in the declaration; it is possible to define arrays starting from zero or a negative value.

There is an allocatable array mechanism for arrays whose size is determined at runtime. Allocation and deallocation are done explicitly; an array that is not deallocated causes a memory leak, though the system reclaims it when the program terminates.

What you will learn

  • Declaring arrays and index bounds
  • All array operations
  • Taking a slice
  • Allocatable arrays
  • Layout order in memory

Who is this for?

  • Those doing matrix calculations
  • Those getting array bounds errors
  • Those working with dynamic size

When to use?

If the size is known at compile time, a fixed-size array is the fastest; if it varies based on input, an allocatable array is used.

All array operations are both shorter than hand-written loops doing the same thing and more open to compiler optimization.

Common mistakes

Wrong

program test
  implicit none
  integer :: dizi(5), i
  do i = 0, 4
    dizi(i) = i
  end do
  print *, dizi
end program test

By default, the indices range from one to five; the zeroeth index is out of bounds. If bounds checking is not enabled, the compiler won't throw an error, it corrupts neighboring memory and the program crashes somewhere unrelated. Writing with functions that query bounds is safe.

Correct

program test
  implicit none
  integer :: dizi(5), i
  do i = lbound(dizi, 1), ubound(dizi, 1)
    dizi(i) = i
  end do
  print *, dizi
end program test

Wrong

program test
  implicit none
  integer, allocatable :: dizi(:)
  dizi(1) = 10
  print *, dizi(1)
end program test

An allocatable array was accessed without being allocated; since the array does not exist yet, an access violation occurs. You need to allocate it by giving a size before using it, and deallocate it when you are done. There is also a function that queries whether memory has been allocated.

Correct

program test
  implicit none
  integer, allocatable :: dizi(:)
  allocate(dizi(5))
  dizi(1) = 10
  print *, dizi(1)
  deallocate(dizi)
end program test

Wrong

program test
  implicit none
  integer :: a(5), b(3)
  a = [1, 2, 3, 4, 5]
  b = [1, 2, 3]
  print *, a + b
end program test

All array operations can only be performed between arrays of the same shape; adding two arrays of different lengths is a compile-time or runtime error. If you want to operate on a common part, you need to match the shapes by taking a slice.

Correct

program test
  implicit none
  integer :: a(5), b(3)
  a = [1, 2, 3, 4, 5]
  b = [1, 2, 3]
  print *, a(1:3) + b
end program test

Frequently asked questions

How to take a slice?

Instead of an index, the start, end, and an optional step are written. To take a dimension entirely, just leave a colon. Slices can be used on both sides of an assignment, which makes it possible to write loop-free code.

How do I enable bounds checking?

By passing the appropriate option to the compiler. It must definitely be enabled during development; it catches out-of-bounds accesses and tells you where they are. It is disabled in production because it adds overhead to every access.

How do I find out the size of the array?

There are standard functions that give the total number of elements, the length in a specific dimension, and the lower and upper bounds. Since the lower bound is not always one, using these functions when writing loops is safer than hardcoding values.

Summary

Indices start at one by default.

Direct operations can be performed on the entire array.

Slices are taken using an index range.

Allocatable arrays require allocation first.

Bounds checking should be enabled during development.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code