简体   繁体   中英

What does o'377' mean in Fortran 77?

What does it mean o'377' in Fortran 77? When I tried to print it outputs 255.

print*,"result", o'377'

which returns

result 255

It's an octal (base 8) representation. 377 octal is 255 decimal or FF hex.

This is what they call a boz-literal-constant :

A binary, octal, or hexadecimal constant ( boz-literal-constant ) is a sequence of digits that represents an ordered sequence of bits. Such a constant has no type .

R764 boz-literal-constant is binary-constant , octal-constant or hex-constant
R765 binary-constant is B ' digit [ digit ] ... ' or B " digit [ digit ] ... "
C7107 (R765) digit shall have one of the values 0 or 1 .
R766 octal-constant is O ' digit [ digit ] ... ' or O " digit [ digit ] ... "
C7108 (R766) digit shall have one of the values 0 through 7 .
R767 hex-constant is Z ' hex-digit [ hex-digit ] ... ' or Z " hex-digit [ hex-digit ] ... "
R768 hex-digit is digit or A through F

C7109 (R764) A boz-literal-constant shall appear only as a data-stmt-constant in a DATA statement, or where explicitly allowed in 16.9 as an actual argument of an intrinsic procedure.

source: Fortran 2018 Standard Section 7.7

As is seen from the Standard, a boz-literal constant has no type and can only appear in data-statements or some implicit functions. This implies that the notation:

print*,"result", o'377'

is invalid code since the octal representation has no type. The correct code would have read:

print *, "result", INT(o'377')

However, in Fortran 90 this would also have been invalid as one could only use boz-literal constants in DATA-statements. The only valid way would have been:

INTEGER :: constant
DATA constant /o'377'/
print *, "result", constant

note: Some compilers allow the usage of boz-literal-constants outside of the DATA statement. Solaris-studio converts them to the type required by the context. Other compilers might have different opions on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM