Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

fortran

What are variables in Fortran?

Rehan Butt

Overview of variables in Fortran

A variable is just like a container that contains a specific value. The memory chunk, that a particular variable occupies, has a specific name called the variable name.

A variable in memory

In Fortran:

  • Variables must be declared before use.
  • Values of variables can be changed at any instance during execution.

Data types

Fortran provides five intrinsic data types. However, we can derive our own data types as well.

Following are a few data types of variables in Fortran:

  • Integer: A variable with an integer data type stores only integer numbers.
  • Real: A variable with a real data type stores a floating number.
  • Character: A variable with character data type stores characters and strings, and its length depends upon the length specifier.
  • Complex: A variable with complex data type stores complex numbers.
  • Logical: A variable with a logical data type holds only two values, either true or false.

Syntax

Following is the syntax for declaring a variable:

integer :: age
age = 25
Syntax of declaring a variable

Example

Program print
integer :: age
age = 90

Print *, "Output:", age

End Program print
Initializing and displaying the value of an integer variable

Explanation

  • Line 1: We name the program.
  • Line 2: We declare an integer type variable named age.
  • Line 3: We initialize the variable, age, with a value of 90.
  • Line 5: We display the value of the variable, age, on the console.
  • Line 7: We end the program.

RELATED TAGS

fortran

CONTRIBUTOR

Rehan Butt
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring