comma operator: keep away?
Kemp at DOCKMASTER.NCSC.MIL
Kemp at DOCKMASTER.NCSC.MIL
Sun Apr 30 08:50:46 AEST 1989
>>C is designed to be a low-level language...
Henry Spencer replies:
> Opinions vary on this. How do you write "*p++ = *q++" in Fortran?
> Which is the higher level language, based on this comparison? (or
> any comparison, apart from the marginal issue of complex numbers.)
Much as I fear disagreeing with Henry, I have to respond to this :-)
In Fortran, one writes "p(i) = q(i)", at least inside a loop, which is
where such a construct is normally found. This is "higher level" in
that it is more like the mathematical notion of "copy array q to array
p" than the machine level description "fetch data pointed to by q,
increment q, copy to location pointed to by p, increment p".
While opinions may (and do) vary on everything, some opinions correspond
to reality more closely than others. If you define "high level" as
meaning "a higher level of data abstraction" then Fortran is clearly
higher level than C for the types of data it was designed to handle
(numbers, arrays, strings, and structures thereof). If you equate higher
level with "fewer characters needed to define an algorithm", then opinions may
vary. For anything involving explicit pointer manipulation (trees, queues,
etc) Fortran is entirely unsuitable, and C is thus "higher level".
-----------------------
Arrays:
Although C has an array notation, it is not used very effectively. I
don't know of any C compilers that do array bounds checking (which does
not mean that none exist, but they are not on the machines I use).
Fortran programmers take such checking for granted.
In addition, C programmers use the *p++=*q++ construct to copy an array
because historically C compilers have been too stupid to optimize
"p[i] = q[i]".
Fortran-8x allows an even higher level representation of array
operations, for example:
Fortran: do i = 1,100 C: p = array_p;
p(i) = q(i) q = array_ q;
end do for (i=0; i<100; i++)
*p++ = *q++;
is replaced by one line:
p(1:100) = q(1:100)
----------------------------
I/O:
In Fortran, one can write an array:
write(6,1) "p", p
1 format(/a,":",1000(5f6.1/))
producing:
p:
4.2 14.3 27.8 13.9 25.6
41.7 32.5 83.3 54.2 71.0
92.3 7.2 42.7 89.3 43.6
.
.
.
How "high level" is the C code to do the same thing?
Fortran includes I/O within the language definition, which allows
the compiler to detect errors that C compilers can't. For example,
write(6,"(i4,3f6.2)") a, b, c
will produce a "data/format mismatch error" if a is not an integer,
otherwise it executes correctly. What will the equivalent C code:
printf("%4d%6.2f%6.2f%6.2f\n", a, b, c);
do if a is not an int, or when there is not a fourth variable to
print? Which is the "higher level" behavior?
Fortran has list-directed i/o (default formatting) and implied loops:
write(6,*) "a=", a, " i=", i, " p=", (p(j), j=1,4)
Isn't this "higher level" than printf?
------------------------
Strings:
Fortran strings are represented by a pointer and a length; C strings
are just a pointer, with a null byte representing the end of the string.
Now what kind of a scuzzy, data-dependent, low-level hack is that?
Strings without bounds are what caused the chfn() security hole. How
many other instances do you know of where the use of str???() instead
of strn???() caused problems? Why does C need bzero(), bcopy(), etc
in addition to strcpy(), strcmp(), etc?
In Fortran, one writes instead of
color = "red" strcpy(color, "red");
Which is "higher level"?
How much code would it take to write the following in C (where for example
dir = "/home/dpk" and file = "junk")?
open(1, dir(:lnblnk(dir))//"/"//file(:lnblnk(file))//".f")
How high level is the equivalent code in C?
----------------------------
There indeed may be no objective reality to the term "high level", but
IMHO there are numerous cases (besides those involving complex numbers)
where Fortran is easier to write, easier to read, and easier for the
computer to compile into efficient code.
Dave Kemp <Kemp at dockmaster.ncsc.mil>
More information about the Comp.unix.questions
mailing list