Fortran 77固定格式程序编译报错:实型转整型参数类型不匹配
Hey there, let's break down and fix this gfortran compilation error you're facing. That Type mismatch in argument n at 1 passed REAL(8) to integer (4) message is telling you exactly what's wrong—you're passing a double-precision real variable (REAL(8)) to a subroutine argument n that expects a 4-byte integer (integer(4)), and Fortran's strong typing rules won't let that slide.
Here's how to resolve this step by step:
1. Verify Your Subroutine's Argument Declaration
First, make sure your READ_VEC subroutine explicitly declares n as an integer. In Fortran 77 fixed-format code, declarations must come before any executable statements, and being explicit avoids implicit type surprises. Example:
SUBROUTINE READ_VEC(N, VEC) C Declare arguments explicitly INTEGER N ! N is the integer dimension of the vector REAL*8 VEC(N) ! VEC is a double-precision column vector of length N C Rest of your subroutine logic here... END
Note: In fixed format, line numbers go in columns 1-5, continuation lines mark column 6 with a non-blank character, and code lives in columns 7-72.
2. Check the Subroutine Call Site
Next, look at where you're calling READ_VEC. If you're passing a real variable (even if it holds an integer value), that's the problem. For example, this is incorrect:
REAL*8 DIMENSION_VAL REAL*8 MY_VEC(100) CALL READ_VEC(DIMENSION_VAL, MY_VEC) ! ERROR: DIMENSION_VAL is REAL(8)
Fix it by using an integer variable instead:
INTEGER DIMENSION_VAL REAL*8 MY_VEC(100) DIMENSION_VAL = 25 ! Assign an integer value to the dimension CALL READ_VEC(DIMENSION_VAL, MY_VEC) ! CORRECT: Passing an integer
3. Handle Real-to-Integer Conversions (If Needed)
If you're reading the dimension value as a real (e.g., from user input formatted as real), explicitly convert it to an integer before passing it to the subroutine with the INT() function:
REAL*8 INPUT_REAL INTEGER DIMENSION_VAL REAL*8 MY_VEC(100) C Read a real value from input READ(*,*) INPUT_REAL C Convert to integer explicitly DIMENSION_VAL = INT(INPUT_REAL) CALL READ_VEC(DIMENSION_VAL, MY_VEC)
Pro Tip for Fortran 77
Add IMPLICIT NONE at the top of your subroutine and main program if your compiler supports it (most modern ones like gfortran do). This disables Fortran's implicit typing rules (where variables starting with I-N are integer by default, others are real), which will catch type mismatches early and save you headaches later. Example:
SUBROUTINE READ_VEC(N, VEC) IMPLICIT NONE ! Turn off implicit typing INTEGER N REAL*8 VEC(N) C ... rest of code ... END
内容的提问来源于stack exchange,提问作者bsarmez

