# Calling error stop from C

**URL:** <https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267>\
**Category:** Uncategorized\
**Created:** [May 20, 2021, 9:14pm UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267 "2021-05-20T21:14:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsnyder](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hsnyder/32/747_2.png) [@hsnyder](https://fortran-lang.discourse.group/u/hsnyder)\
**Post date:** [May 20, 2021, 9:14pm UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267/1 "2021-05-20T21:14:07Z")

</div>

I know I can write a bind(C) function that just takes in my error message and calls error stop, and use that as a way to call error stop from a C function in a mixed fortran/C program, but is there a more direct way? I didn’t see anything about error stop in ISO\_Fortran\_binding.h so I’m assuming there is no more direct/standardized way, but I thought I would at least ask.

---

<div class="post-metadata">

**Author:** ![eugene\_epshteyn](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@eugene\_epshteyn](https://fortran-lang.discourse.group/u/eugene_epshteyn)\
**Post date:** [May 21, 2021, 2:46am UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267/2 "2021-05-21T02:46:33Z")

</div>

Is the main program C or Fortran? You should use the exit method suitable to the language used to create the main program.

---

<div class="post-metadata">

**Author:** ![hsnyder](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hsnyder/32/747_2.png) [@hsnyder](https://fortran-lang.discourse.group/u/hsnyder)\
**Post date:** [May 21, 2021, 3:51am UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267/3 "2021-05-21T03:51:04Z")

</div>

The main program is Fortran in this case. I’m currently using abort() from C where needed. It works, I just feel like it would be more “elegant” to use error stop.

---

<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:** [May 21, 2021, 11:47am UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267/4 "2021-05-21T11:47:06Z")

</div>

Since “error stop” is a statement and not a function/subroutine, I do not think you can do this in another way than calling a wrapper routine.

(The problem is in a way similar to C macro that masquerade as functions - on the Fortran side you cannot call them directly.)

---

<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:** [May 21, 2021, 12:50pm UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267/5 "2021-05-21T12:50:11Z")

</div>

> [@hsnyder](#):
>
> The main program is Fortran in this case. I’m currently using abort() from C where needed. It works, I just feel like it would be more “elegant” to use error stop.

The safer option will be to have your Fortran main include an “error handler” procedure (say `fort_abort`) and your C side of code invoke that instead of C `abort()`.

Unfortunately nothing more “elegant” than this is on tap given how primitive the `Termination of Execution" section is in the Fortran standard for application needs going back to 1990 even, let alone year 2021.

---

<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:** [May 21, 2021, 2:01pm UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267/6 "2021-05-21T14:01:04Z")

</div>

Check out this example, and just replace the `print` with `error stop`. (start at 11:09)

[![](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/72027b6af7735873529ce7621cfa0926da3cb206.jpeg "Just Talk to Each Other: Getting Fortran and C to Work Together") ](https://www.youtube.com/watch?v=z5km039ZVYE)

---

<div class="post-metadata">

**Author:** ![Federchen](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@Federchen](https://fortran-lang.discourse.group/u/Federchen)\
**Post date:** [May 22, 2021, 2:05pm UTC](https://fortran-lang.discourse.group/t/calling-error-stop-from-c/1267/7 "2021-05-22T14:05:02Z")

</div>

ERROR STOP is to explicitly initiate error termination with coarray programms and thus, to stop execution of a coarray app on all images as soon as possible.

Advanced coarray programming, on the other hand, should rather seek to avoid error termination (by using stat= specifiers).
