# C Is Not a Low-level Language

**URL:** <https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464>\
**Category:** Uncategorized\
**Created:** [December 29, 2021, 2:26pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464 "2021-12-29T14:26:54Z")\
**Posts on this page:** 12\
**Page:** 3

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [August 22, 2023, 2:44pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/41 "2023-08-22T14:44:23Z")

</div>

> [@tyranids](#):
>
> This is especially true when not following said rules leads to different results.

Outside of the rules, any result is as good as any other.

Count yourself lucky, because now you know you did not follow the rules. Count yourself doubly lucky if this has prompted you to invest in better tools for the next time you will inadvertently not follow the rules.

If a handyman turned up to do a job for you but only brought whatever tools were lying around on the street and then makes a hash of the job, would you hire him again?

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [August 22, 2023, 4:40pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/42 "2023-08-22T16:40:16Z")

</div>

> [@themos](#):
>
> The claim ([NAG Fortran Compiler, Release 7.1: Compiler Options](https://www.nag.com/nagware/np/r71_doc/manual/compiler_2_4.html#OPTIONS)) is that
> 
> > The _-C=alias_ option will produce a runtime error when it is detected that assignment to a dummy argument affects another dummy argument. At this release this is only detected for scalar dummy arguments.

@thermos,

Would you know whether `NAG Fortran` compiler alerts the paying customer re: the scenario below when `-C=alias` option is specified? Not sure whether you have continued access to that compiler.

```fortran
module m
contains
   subroutine sub1(arg1, arg2)
      real, intent(in) :: arg1
      real, intent(out) :: arg2
      arg2 = arg1*10.0
   end subroutine
   subroutine sub2()
      real, target :: t
      real, pointer :: p1, p2
      p1 => t ; p2 => t
      call sub1( p1, p2 )
   end subroutine
end module

```

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [August 22, 2023, 5:21pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/43 "2023-08-22T17:21:17Z")

</div>

I am temporarily without access to that compiler.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [August 22, 2023, 5:38pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/44 "2023-08-22T17:38:25Z")

</div>

> [@themos](#):
>
> I am temporarily without access …

Ok.

The website suggests it is a run-time option and for scalar received arguments which is quite limiting.

The point by @tyranids then remains valid the compilers cannot offer much of any compile-time help here but which is understandable.

The counterargument to @tyranids is, as always with many things in Fortran, it is kind of like a honor code signed up to by the program author to not alias and thus the processor is permitted to attempt further code optimizations - take it or leave it.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [August 22, 2023, 6:24pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/45 "2023-08-22T18:24:03Z")

</div>

> [@FortranFan](#):
>
> Would you know whether `NAG Fortran` compiler alerts the paying customer re: the scenario below when `-C=alias` option is specified? Not sure whether you have continued access to that compiler.
> 
> ```auto
> module m
> contains
> subroutine sub1(arg1, arg2)
> real, intent(in) :: arg1
> real, intent(out) :: arg2
> arg2 = arg1*10.0
> end subroutine
> subroutine sub2()
> real, target :: t
> real, pointer :: p1, p2
> p1 => t ; p2 => t
> call sub1( p1, p2 )
> end subroutine
> end module
> 
> ```

nagfor gives no compile time diagnostic for this code, but calling `sub2` when compiled with the `-C=alias` option does produce a runtime error. i.e.

```auto
use m
call sub2
end

```

```plaintext
(base) [stray:~/scratch] nagfor -C=alias random.f90
NAG Fortran Compiler Release 7.1(Hanzomon) Build 7127
[NAG Fortran Compiler normal termination]
(base) [stray:~/scratch] ./a.out 
Runtime Error: random.f90, line 6: Assignment to ARG2 affects dummy argument ARG1
Program terminated by fatal error
zsh: IOT instruction (core dumped) ./a.out

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [August 22, 2023, 6:54pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/46 "2023-08-22T18:54:54Z")

</div>

> [@everythingfunctional](#):
>
> … but calling `sub2` when compiled with the `-C=alias` option does produce a runtime error …

Thanks. But a run-time error and scalar arguments is rather limiting. Those who are unable or unwilling to procure a commercial product in `NAG Fortran` can figure out a preprocessor based option to first replicate and then automate across large codes the same facility, say along the following lines:

```fortran
module m
contains
   subroutine sub1(arg1, arg2)
      integer, intent(in) :: arg1(:)
      integer, intent(out) :: arg2(:)
#ifdef DEBUG
      if ( aliased(arg1, arg2 ) ) error stop "sub1: arg1 and arg2 are aliased; assignment to one affects the other" 
#endif
      arg2 = arg1*10.0
#ifdef DEBUG
   contains
      function aliased( a1, a2 ) result(r)
         use, intrinsic :: iso_c_binding, only : c_loc, c_associated
         ! Argument list
         type(*), intent(in), target :: a1(..)
         type(*), intent(in), target :: a2(..)
         ! Function result
         logical :: r
         r = c_associated( c_loc(a1), c_loc(a2) )
      end function 
#endif
   end subroutine
   subroutine sub2()
      integer, target :: t(2)
      integer, pointer :: p1(:), p2(:)
      t = [1, 2]
      p1 => t ; p2 => t
      call sub1( p1, p2 )
      print *, u
   end subroutine
end module
   use m
   call sub2()
end

```

```auto
C:\temp>gfortran -cpp -ffree-form -DDEBUG p.f -o p.exe

C:\temp>p.exe
ERROR STOP sub1: arg1 and arg2 are aliased; assignment to one affects the other

Error termination. Backtrace:
...

```

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [August 24, 2023, 10:53am UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/47 "2023-08-24T10:53:33Z")

</div>

I wonder about this difference in semantics. I know some of the issues but only superficially. Is there any reference for this? I am interested to know more, out of basic curiosity, nothing else.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [August 24, 2023, 12:58pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/48 "2023-08-24T12:58:49Z")

</div>

A reference to differences between C and Fortran semantics? I don’t know of one. What I recall is efforts by the Fortran team to work around C-centric concepts baked into the back-end (this happened with the migration of DEC Fortran to Intel Fortran too, discarding the DEC-developed GEM multi-language back-end and switching to the Intel C-centric IL0 backend.) With LLVM, I recall in the early days that it didn’t even have the concept of a complex datatype. Fortran COMMON is another thing that isn’t exactly like C extern. Sometimes the back-end needs additional IL to support Fortran.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [August 24, 2023, 1:07pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/49 "2023-08-24T13:07:07Z")

</div>

Pity. Well, thanks anyway. (Re complex: the absence of such a data type has always amazed me, even though I do not use it myself all that often).

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [August 24, 2023, 4:02pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/50 "2023-08-24T16:02:55Z")

</div>

Re: “Fortran COMMON is another thing that isn’t exactly like C extern”, but `BIND(C)` clause indeed makes it like a C `extern` to a `struct` datatype.

```auto
#include <stdio.h>

typedef struct {
   int n;
} foo_c;
extern foo_c foo;

// Fortran function
void Fsub();

int main()
{
   Fsub();
   printf("In C main: foo.n = %d\n", foo.n);
   return 0;
}

```

```fortran
subroutine Fsub() bind(C, name="Fsub")
   use, intrinsic :: iso_c_binding, only : c_int
   integer(c_int) :: n
   common / foo / n
   bind(C, name="foo") :: / foo /
   n = 42
   return 
end subroutine 

```

```auto
C:\temp>gfortran -ffree-form c.c s.f -o c.exe
cc1.exe: warning: command-line option '-ffree-form' is valid for Fortran but not for C

C:\temp>c.exe
In C main: foo.n = 42

```

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [August 24, 2023, 5:34pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/51 "2023-08-24T17:34:15Z")

</div>

With some restrictions, yes, but most uses of COMMON are not BIND(C) and there are additional quirks. Consider blank common, for example. My general point was that back ends written initially to target C usually need some work to accommodate Fortran.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [August 29, 2023, 2:59am UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/52 "2023-08-29T02:59:44Z")

</div>

Yes, common can be quite tricky in the backend. There are several ways to implement it, but my experience generally mirrors what @sblionel wrote as well.

[Previous page](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464.md?page=2)
