# Underscore at the end of a Fortran name

**URL:** <https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749>\
**Category:** Help\
**Created:** [February 8, 2022, 2:40pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749 "2022-02-08T14:40:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [February 8, 2022, 2:40pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/1 "2022-02-08T14:40:54Z")

</div>

Quick question: Can there be problems when a Fortran name ends with an underscore like `example_var_`?  
I was told to avoid underscores at the end of names, because compilers might add underscores internally, but I couldn’t find anything about this. Even in the [j3-fortran document](https://j3-fortran.org/doc/year/18/18-007r1.pdf) (side question, how do you call this document normally?) there is an example with a trailing underscore (6.2.2 / R603) and `stdlib` uses such variables, too. So I assume it is safe to name variables like this, but maybe it was problematic in an earlier version of 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:** [February 8, 2022, 2:45pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/2 "2022-02-08T14:45:02Z")

</div>

No, it is safe to do so. Fortran does not allow underscores at the beginning of a name, but any trailing underscores the compiler may add should not be visible on the Fortran side. (This is a different matter for C, where names internal or special to the compiler may have a bunch of underscores and these would be visible on the C side)

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [February 8, 2022, 2:47pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/3 "2022-02-08T14:47:28Z")

</div>

Okay, since interoperability with C is a common thing, is it still safe to use trailing underscores in Fortran source code or could it result in problems with the C code?

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [February 8, 2022, 2:54pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/4 "2022-02-08T14:54:02Z")

</div>

Not really, Fortran symbols usually get additionally mangled with the module name, unless you add `bind(c)`. You could try to guess the symbol name from C for non-module procedures, but again this is not guaranteed to work unless they have `bind(c)`.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [February 8, 2022, 2:57pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/5 "2022-02-08T14:57:16Z")

</div>

I don’t think there is any problem with doing so and often create a local variable with the name of an optional argument appended by an underscore, for example

```auto
if (present(foo)) then
   foo_ = foo
else
   foo_ = foo_default
end if
! use foo_ in the rest of the procedure

```

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [February 8, 2022, 3:06pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/6 "2022-02-08T15:06:37Z")

</div>

What do you mean by ‘guess the symbol name from C’? I don’t want to try to hack around C-Fortran interoperability. I just want to be 100% sure that nothing unexpected will ever happen, caused by a trailing underscore.  
I use them a lot, too (e.g. `result_`). But when it comes to teaching Fortran, I have to be extra careful, because errors I teach multiply…

---

<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:** [February 8, 2022, 3:06pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/7 "2022-02-08T15:06:45Z")

</div>

Precisely, in the old days, best forgotten, you had to guess the name mangling (one underscore, two underscores, all capitals) and use the result on the C side. But with the bind(C) facility that is no longer required.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [February 8, 2022, 3:11pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/8 "2022-02-08T15:11:33Z")

</div>

I don’t think this will cause issues for local variables.

You could try to inspect your object files with `nm` and look whether you actually find some symbols which might resemble the `result_` variable from your source code. I found it quite insightful to see which symbols are actually exported from a module and which are not visible or just heavily mangled.

---

<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:** [February 8, 2022, 3:42pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/9 "2022-02-08T15:42:37Z")

</div>

> [@Carltoffel](#):
>
> Quick question: Can there be problems when a Fortran name ends with an underscore like `example_var_` ?

TL;DR: the answer is **no**.

No worries here, you can use trailing underscores as needed in your code.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [February 8, 2022, 3:55pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/10 "2022-02-08T15:55:20Z")

</div>

Thanks for the tip about `nm`, which I decided to experiment with. For the the code

```auto
module twice_mod
implicit none
private
integer :: nobs, ncol
real, parameter :: pi = 3.14
private :: twice_scalar, thrice, ncol
public :: twice, twice_vec, quad, pi, nobs
interface twice
   module procedure twice_vec
end interface twice
contains
function twice_scalar(i) result(j)
implicit none
integer, intent(in) :: i
integer :: j
j = 2*i
end function twice_scalar
!
function twice_vec(i) result(j)
implicit none
integer, intent(in) :: i(:)
integer :: j(size(i))
j = 2*i
end function twice_vec
!
function thrice(i) result(j)
implicit none
integer, intent(in) :: i
integer :: j
j = 3*i
end function thrice
!
function quad(i) result(j)
implicit none
integer, intent(in) :: i
integer :: j
j = 4*i
end function quad
end module twice_mod

```

nm run on the .o file created by gfortran gives

```auto
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
0000000000000000 B __twice_mod_MOD_ncol
0000000000000004 B __twice_mod_MOD_nobs
0000000000000000 T __twice_mod_MOD_quad
0000000000000021 t __twice_mod_MOD_thrice
00000000000001a5 t __twice_mod_MOD_twice_scalar
0000000000000045 T __twice_mod_MOD_twice_vec

```

It appears that public procedures such as `quad` and `twice_vec` are labeled `T` and the private ones such as `thrice` and `twice_scalar` are labeled `t`. Both `ncol` and `nobs` are labeled `B`, although only `nobs` is public. The named constant `pi` does not appear.

---

<div class="post-metadata">

**Author:** ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)\
**Post date:** [February 8, 2022, 8:43pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/11 "2022-02-08T20:43:51Z")

</div>

keep in mind that the name mangling is compiler dependent:

```auto
maws01 ➜ /tmp ifort -c test.f90 
maws01 ➜ /tmp nm test.o
0000000000000000 T twice_mod._
0000000000000004 C twice_mod_mp_ncol_
0000000000000004 C twice_mod_mp_nobs_
0000000000000180 T twice_mod_mp_quad_
0000000000000170 T twice_mod_mp_thrice_
0000000000000010 T twice_mod_mp_twice_scalar_
0000000000000020 T twice_mod_mp_twice_vec_
maws01 ➜ /tmp gfortran -c test.f90
maws01 ➜ /tmp nm test.o
0000000000000000 B __twice_mod_MOD_ncol
0000000000000004 B __twice_mod_MOD_nobs
0000000000000000 T __twice_mod_MOD_quad
0000000000000019 t __twice_mod_MOD_thrice
0000000000000183 t __twice_mod_MOD_twice_scalar
0000000000000035 T __twice_mod_MOD_twice_vec

```

---

<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:** [February 9, 2022, 3:02pm UTC](https://fortran-lang.discourse.group/t/underscore-at-the-end-of-a-fortran-name/2749/12 "2022-02-09T15:02:29Z")

</div>

If you’re worried about C, Fortran has very specific rules about what happens when a global symbol has the BIND(C) attribute (Fortran name is downcased, no Fortran-specific decoration). If you need further control, add NAME=, in which case the string you give is treated exactly as if you had specified that in the “companion C processor”.
