2024-06-21
This commit is contained in:
80
Writerside/topics/Variable.md
Normal file
80
Writerside/topics/Variable.md
Normal file
@@ -0,0 +1,80 @@
|
||||
# 변수
|
||||
|
||||
## 자료형
|
||||
|
||||
포트란에는 5 종류의 기본 자료형이 있습니다.
|
||||
|
||||
- integer
|
||||
|
||||
- real
|
||||
|
||||
- complex
|
||||
|
||||
- character
|
||||
|
||||
- logical
|
||||
|
||||
|
||||
### 정수형
|
||||
|
||||
### 실수형
|
||||
|
||||
```fortran
|
||||
program variables
|
||||
implicit none
|
||||
real :: value1
|
||||
end program variables
|
||||
```
|
||||
|
||||
아래와 같은 방식으로 원하는 부동 소수의 정밀도를 지정해서 사용할 수 있습니다.
|
||||
|
||||
```fortran
|
||||
program variables
|
||||
use, intrinsic :: iso_fortran_env, only: sp=>real32, dp=>real64
|
||||
! use, intrinsic :: iso_c_binding, only: sp=>c_float, dp=>c_double
|
||||
implicit none
|
||||
|
||||
real :: val0
|
||||
real(sp) :: val1 ! 단정도
|
||||
real(dp) :: val2 ! 배정도
|
||||
|
||||
val0 = 0.
|
||||
val1 = 0._sp
|
||||
val2 = 0._dp
|
||||
|
||||
print *, 'Max = ', huge(val0)
|
||||
print *, 'Max32 = ', huge(val1)
|
||||
print *, 'Max64 = ', huge(val2)
|
||||
|
||||
end program variables
|
||||
```
|
||||
|
||||
### 복소수형
|
||||
|
||||
복소수는 실수부와 허수부를 콤마로 구분하고 괄호로 묶어 표현합니다.
|
||||
|
||||
### 문자형
|
||||
|
||||
문자는 홑 따옴표 또는 쌍 따옴표로 묶어서 표현합니다.
|
||||
|
||||
### 논리형
|
||||
|
||||
`.true.` 또는 `.false.`값을 가질 수 있습니다.
|
||||
|
||||
## 변수 선언
|
||||
|
||||
변수는 `자료형 :: 변수명` 형식을 사용해서 정의합니다. 포트란은 대소문자를 구분하지 않으며, 이 규칙은 변수명에도 적용됩니다. 변수명의 길이는 최대 31자까지만 지정할 수 있습니다.
|
||||
|
||||
```fortran
|
||||
program variables
|
||||
implicit none
|
||||
|
||||
integer :: count
|
||||
logical :: isOk
|
||||
|
||||
end program variables
|
||||
```
|
||||
|
||||
### implicit none
|
||||
|
||||
기본적으로 포트란에서는, 선언되지 않은 변수를 사용하면 변수명의 첫 번째 문자에 따라 자동으로 변수를 선언합니다. `implicit none`을 선언하면 무조건 변수해야 하도록 지정합니다.
|
||||
Reference in New Issue
Block a user