CAFE

Fortran

(f77) Reference 16. Statements 3 - CHARACTER, CLOSE, COMMON

작성자올란도|작성시간11.09.26|조회수85 목록 댓글 0

CHARACTER

The CHARACTER statement specifies the type of a symbolic constant, variable,
array, function, or dummy function to be character.
Optionally, it initializes any of the items with values and specifies array
dimensions.


Syntax

CHARACTER [* len [,]] v [* len /c/]] …
v       Name of a symbolic constant, variable, array, array declarator, function, or
        dummy function
len    Length in characters of the symbolic constant, variable, array element, or
         function
c       List of constants for the immediately preceding name


Description
Each character occupies 8 bits of storage, aligned on a character boundary.
Character arrays and common blocks containing character variables are packed
in an array of character variables. The first character of one element follows the
last character of the preceding element, without holes.
The length, len must be greater than 0. If len is omitted, it is assumed equal to
1.
For local and common character variables, symbolic constants, dummy
arguments, or function names, len can be an integer constant, or a
parenthesized integer constant expression‎.
For dummy arguments or function names, len can have another form: a
parenthesized asterisk, that is, CHARACTER*(*), which denotes that the
function name length is defined in referencing the program unit, and the
dummy argument has the length of the actual argument.
For symbolic constants, len can also be a parenthesized asterisk, which
indicates that the name is defined as having the length of the constant. This is
shown in Example 5 in the next section.
The list c of constants can be used only for a variable, array, or array declarator.
There can be only one constant for the immediately preceding variable, and
one constant for each element of the immediately preceding array.


Examples
Example 1: Character strings and arrays of character strings:

CHARACTER*17 A, B(3,4), V(9)
CHARACTER*(6+3) C


The above code is exactly equivalent to the following:

CHARACTER A*17, B(3,4)*17, V(9)*17
CHARACTER C*(6+3)


Both of the above two examples are equivalent to the nonstandard variation: ©

CHARACTER A*17, B*17(3,4), V*17(9)   ! nonstandard


There are no null (zero-length) character-string variables. A one-byte character
string assigned a null constant has the length zero.


Example 2: No null character-string variables:

CHARACTER S*1
S = ''


During execution of the assignment statement, the variable S is precleared to
blank, and then zero characters are moved into S, so S contains one blank;
because of the declaration, the intrinsic function LEN(S) will return a length of
1. You cannot declare a size of less than 1, so this is the smallest length string
variable you can get.


Example 3: Dummy argument character string with constant length:

SUBROUTINE SCHLEP ( A )
CHARACTER A*32


Example 4: Dummy argument character string with length the same as
corresponding actual argument:


SUBROUTINE SCHLEP ( A )
CHARACTER A*(*)


Example 5: Symbolic constant with parenthesized asterisk:

CHARACTER *(*) INODE
PARAMETER ( INODE = 'Warning: INODE clobbered!' )


The intrinsic function LEN(INODE) returns the actual declared length of a
character string. This is mainly for use with CHAR*(*) dummy arguments.
Example 6: The LEN intrinsic function:


CHARACTER A*17
A = "xyz"
PRINT *, LEN( A )
END


CLOSE

The CLOSE statement disconnects a file from a unit.

Syntax


CLOSE( [ UNIT=] u [, STATUS= sta] [, IOSTAT= ios] [, ERR= s ] )
u     Unit identifier for an external unit. If UNIT= is not used, then u must be first.
       sta Determines the disposition of the file—sta is a character expression‎ whose
       value, when trailing blanks are removed, can be KEEP or DELETE. The
       default value for the status specifier is KEEP. For temporary (scratch) files, sta
       is forced to DELETE always. For other files besides scratch files, the default
       sta is KEEP.
ios   I/O status specifier—ios must be an integer variable or an integer array
       element.
s      Error specifier—s must be the label of an executable statement in the same
        program containing the CLOSE statement. The program control is transferred
        to this statement in case an error occurs while executing the CLOSE
        statement.


Description
For tape, it is more reliable to use the TOPEN() routines.
The options can be specified in any order.
The DISP= and DISPOSE= options are allowable alternates for STATUS=, with
a warning, if the -ansi flag is set.
Execution of CLOSE proceeds as follows:
1. The specified unit is disconnected.
2. If sta is DELETE, the file connected to the specified unit is deleted.
3. If an IOSTAT argument is specified, ios is set to zero if no error was
encountered; otherwise, it is set to a positive value.


Comments
All open files are closed with default sta at normal program termination.
Regardless of the specified sta, scratch files, when closed, are always deleted.
Execution of a CLOSE statement specifying a unit that does not exist, or a unit
that has no file connected to it, has no effect.


Execution of a CLOSE statement specifying a unit zero (standard error) is not
allowed, but you can reopen it to some other file.
The unit or file disconnected by the execution of a CLOSE statement can be
connected again to the same, or a different, file or unit.


Examples
Example 1: Close and keep:

CLOSE ( 2, STATUS='KEEP')


Example 2: Close and delete:

CLOSE ( 2, STATUS='DELETE', IOSTAT=I )


Example 3: Close and delete a scratch file even though the status is KEEP:

OPEN ( 2, STATUS='SCRATCH')

CLOSE ( 2, STATUS='KEEP', IOSTAT=I )



COMMON

The COMMON statement defines a block of main memory storage so that
different program units can share the same data without using arguments.

Syntax

COMMON [/[ cb ]/] nlist [[,]/[ cb ] / nlist ] …
cb          Common block name
nlist        List of variable names, array names, and array declarators


Description
If the common block name is omitted, then blank common block is assumed.
Any common block name including blank common can appear more than once
in COMMON statements in the same program unit. The list nlist following each
successive appearance of the same common block name is treated as a
continuation of the list for that common block name.
The size of a common block is the sum of the sizes of all the entities in the
common block, plus space for alignment.
Within a program, all common blocks in different program units that have the
same name must be of the same size. However, blank common blocks within a
program are not required to be of the same size.
Restrictions
Formal argument names and function names cannot appear in a COMMON
statement.
An EQUIVALENCE statement must not cause the storage sequences of two
different common blocks in the same program unit to be associated. See
Example 2.
An EQUIVALENCE statement must not cause a common block to be extended
on the left-hand side. See Example 4.


Examples
Example 1: Unlabeled common and labeled common:

DIMENSION V(100)
COMMON V, M
COMMON / LIMITS / I, J


In the above example, V and M are in the unlabeled common block; I and J are
defined in the named common block, LIMITS.
Example 2: You cannot associate storage of two different common blocks in the
same program unit:


COMMON /X/ A
COMMON /Y/ B
EQUIVALENCE ( A, B) ! ¬ Not allowed


Example 3: An EQUIVALENCE statement can extend a common block on the
right-hand side:

DIMENSION A(5)
COMMON /X/ B
EQUIVALENCE ( B, A)


Example 4: An EQUIVALENCE statement must not cause a common block to be
extended on the left-hand side:

COMMON /X/ A
REAL B(2)
EQUIVALENCE ( A, B(2))  !    Not allowed











다음검색
현재 게시글 추가 기능 열기

댓글

댓글 리스트
맨위로

카페 검색

카페 검색어 입력폼