From: e p chandler on
Someone sent me a program fragment to look at. It appears frequently in
programs that he encounters.
[typeless octal constants as bit patterns]

character*1 ff,lf,cr /"014,"012,"015/

Yes, I know that combining a declaration with a data statment like this is
non-standard.

Aside from using the Fortran 95 (some compilers don't support it):

character :: ff=achar(12),lf=achar(10),cr=achar(13)

or replacing each ff, lf and cr in the program text with the appropriate
achar(), is there a decent way of initializing these non-printing characters
in a DATA statement?

I don't particularly want to use Unix style character escapes. (\f , \n,
\r).

Elliot




From: Richard Maine on
e p chandler <epc8(a)juno.com> wrote:

> character*1 ff,lf,cr /"014,"012,"015/
>
> Yes, I know that combining a declaration with a data statment like this is
> non-standard.
>
> Aside from using the Fortran 95 (some compilers don't support it):
>
> character :: ff=achar(12),lf=achar(10),cr=achar(13)
>
> or replacing each ff, lf and cr in the program text with the appropriate
> achar(), is there a decent way of initializing these non-printing characters
> in a DATA statement?
>
> I don't particularly want to use Unix style character escapes. (\f , \n,
> \r).

I might suggest that you could use better compilers, and I'm slightly
curious what compilers you are using that have trouble with this. If you
said exactly what it was that the compiler objected to, it would be
easier to suggest a workaround. As it is, I can't tell whether it is the
achar intrinsic, the use of functions in initialization expressions, the
f90 style initializer, or what. I suppose f77 compilers would be a
problem. And I seem to recall, though not for sure, that F might
disallow achar in favor of char.

Those backslash escape things aren't at all portable anyway.

If you have an f77 compiler that doesn't support achar, you could use
char instead. In theory, it isn't as universal, but it should be a lot
more portable than things like backslash escapes (and that initial
syntax you showed is fairly rare).

It is true that you can't use either char or achar in a DATA statement.

As a last resort, you can set a variable value using an executable
assignment statement (and achar or char) that is executed during program
startup.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: e p chandler on
>>"Richard Maine" wrote
>e p chandler wrote:
>
>> character*1 ff,lf,cr /"014,"012,"015/
>>
>> Yes, I know that combining a declaration with a data statment like this
>> is
>> non-standard.
>>
>> Aside from using the Fortran 95 (some compilers don't support it):
>>
>> character :: ff=achar(12),lf=achar(10),cr=achar(13)
>>
>> or replacing each ff, lf and cr in the program text with the appropriate
>> achar(), is there a decent way of initializing these non-printing
>> characters
>> in a DATA statement?
>>
>> I don't particularly want to use Unix style character escapes. (\f , \n,
>> \r).

> I might suggest that you could use better compilers, and I'm slightly
> curious what compilers you are using that have trouble with this. If you
> said exactly what it was that the compiler objected to, it would be
> easier to suggest a workaround. As it is, I can't tell whether it is the
> achar intrinsic, the use of functions in initialization expressions, the
> f90 style initializer, or what. I suppose f77 compilers would be a
> problem. And I seem to recall, though not for sure, that F might
> disallow achar in favor of char.

I made the mistake of suspecting but not testing. I thought that DVF 5.0 had
a problem with initialization
expressions. It compiles the statement above with no problem. But it does
not allow

C:\Users\epc\temp>df vv.f90
DIGITAL Visual Fortran Optimizing Compiler Version: V5.0
Copyright (c) 1997 Digital Equipment Corp. All rights reserved.

vv.f90
vv.f90(1) : Error: This intrinsic function is invalid in constant
expressions.
[ATAN]
real :: pi=4.0*atan(1.0)
---------------^
vv.f90(1) : Error: This is not a valid initialization expression.
real :: pi=4.0*atan(1.0)
--------------^

I thought erroneously that the compiler does not allow intrinsic functions
there. So I'm sorry about leading you down the wrong path

[snip]

As you say, achar and char are not allowed inside of data statements.

I actually asked the question from the point of view of someone who is stuck
with a load of old programs that do use these octal literals in this way.

Thanks
-- Elliot

From: Richard Maine on
e p chandler <epc8(a)juno.com> wrote:

> I thought erroneously that the compiler does not allow intrinsic functions
> there.

It depends on the particular intrinsic function. The list of intrinsic
functions allowed in initialization expressions by the standard has been
gradually increasing over time (and particular compilers have sometimes
allowed more than the standard).

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Tobias Burnus on
e p chandler wrote:
> vv.f90(1) : Error: This is not a valid initialization expression.
> real :: pi=4.0*atan(1.0)
> --------------^

Using ATAN in initialization expressions is allowed since Fortran 2003,
which one cannot expect from a "(c) 1997" compiler.

Tobias
 |  Next  |  Last
Pages: 1 2
Prev: Why not 0?
Next: calling a real from C