In Fortran, data types are used to define the characteristics of variables such as their size and the type of data they can hold. Understanding Fortran data types is essential for writing efficient and error-free programs. In this article, we will explore the different data types available in Fortran and their usage.
Integer Data Types
Fortran provides various integer data types to represent whole numbers:
- INTEGER: This is the most commonly used integer data type in Fortran. It typically uses 4 bytes of memory and can store values ranging from -2,147,483,648 to 2,147,483,647.
- INTEGER*2: This integer data type uses 2 bytes of memory and can store values ranging from -32,768 to 32,767.
- INTEGER*1: The smallest integer data type in Fortran uses only 1 byte of memory.
It can store values ranging from -128 to 127.
- KIND: Fortran also allows you to specify the size of integers using the KIND parameter. For example, you can declare an integer variable as INTEGER(KIND=8) to use 8 bytes of memory.
Real Data Types
Fortran provides several real data types to represent floating-point numbers:
- REAL: This is the most commonly used real data type in Fortran. It typically uses 4 bytes of memory and can store single-precision floating-point numbers with about seven digits of precision.
- REAL*8: This real data type uses 8 bytes of memory and can store double-precision floating-point numbers with about fifteen digits of precision.
- REAL*16: Fortran also supports extended-precision real numbers using 16 bytes of memory.
These can provide even higher precision if needed.
- KIND: Similar to integers, you can use the KIND parameter to specify the size and precision of real numbers. For example, REAL(KIND=16) will declare a real variable with 16 bytes of memory.
Character Data Types
In Fortran, character data types are used to represent strings:
- CHARACTER: This data type is used to declare a fixed-length string. You need to specify the length of the string when declaring a CHARACTER variable.
- CHARACTER*(LEN): The LEN parameter allows you to declare a character variable with a specific length.
For example, CHARACTER*(10) declares a string variable that can hold up to 10 characters.
- CHARACTER*(*): This special data type is used for declaring strings with dynamic lengths. The length of the string can be changed during runtime based on requirements.
Logical Data Type
The logical data type in Fortran is used to represent boolean values:
- LOGICAL: This data type can take two possible values – .TRUE. or .FALSE.. It is typically used for logical comparisons and control flow in programs.
Derived Data Types
In addition to the basic data types mentioned above, Fortran also allows you to create derived data types by combining multiple variables into a single entity. These derived data types are created using the TYPE keyword.
TYPE :: Person CHARACTER*(20) :: Name INTEGER :: Age REAL :: Height END TYPE Person
The above code declares a derived data type named “Person” that consists of three variables – Name, Age, and Height. This allows you to define complex structures and organize your data more efficiently.
In Fortran, understanding the different data types is crucial for writing effective and efficient programs. By correctly choosing the appropriate data type for your variables, you can ensure proper memory allocation and avoid errors related to type mismatches. Take advantage of Fortran’s flexibility in specifying the size and precision of integers and real numbers using the KIND parameter, and leverage derived data types to create complex structures that suit your program’s needs.