# How to get/query the defined length of a character-type variable

**URL:** <https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395>\
**Category:** Uncategorized\
**Created:** [September 23, 2022, 1:17pm UTC](https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395 "2022-09-23T13:17:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FLNewbiee](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/flnewbiee/32/2155_2.png) [@FLNewbiee](https://fortran-lang.discourse.group/u/FLNewbiee)\
**Post date:** [September 23, 2022, 1:17pm UTC](https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395/1 "2022-09-23T13:17:07Z")

</div>

With reference to the following code:

```fortran
  character(len=128) :: arg
  character(len=128), dimension(:), allocatable :: args

  arg_counter = command_argument_count()
  allocate(args(arg_counter))

  do i = 1, arg_counter
    call get_command_argument(i, args(i), arg_length)
    if (arg_length > 128) then
      ...
    end if
  end do

```

- I have seen that `character(len=n)`, with `integer(2) :: n = 128` earlier defined, is not accepted. In addition, a statement `arg_length > 128` is used. Is there any workaround to avoid writing `128` three times?
- Is there a way to get/query the `character` type `len` property of `args` variable so as I can simply write: `arg_length > func(args)`(= 128) ?  
_Tia_

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [September 23, 2022, 1:26pm UTC](https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395/2 "2022-09-23T13:26:24Z")

</div>

You have declared `args` as an array of characters. All elements in that array will have the same length so assuming that you know the array has been allocated you can check `len(args(1))`.

Often one does not want all strings to be of the same length. I think you might benefit from using a type along these lines:

```fortran
type :: string_t
    character(len=:), allocatable :: chars
end type

```

and

```fortran
type(string_t), allocatable :: args(:)

```

There’s also numerous libraries which implement variable length string types like this with various utility functions. See e.g. Fortran stdlib: [GitHub - fortran-lang/stdlib: Fortran Standard Library](https://github.com/fortran-lang/stdlib)

---

<div class="post-metadata">

**Author:** ![FLNewbiee](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/flnewbiee/32/2155_2.png) [@FLNewbiee](https://fortran-lang.discourse.group/u/FLNewbiee)\
**Post date:** [September 23, 2022, 1:52pm UTC](https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395/3 "2022-09-23T13:52:57Z")

</div>

So, we have `arg_length > len(args(1))`. @plevold _Tal!_

---

<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:** [September 23, 2022, 2:21pm UTC](https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395/4 "2022-09-23T14:21:25Z")

</div>

To read an command line argument in a deferred length character you can first query its length, than allocate and finally retrieve the value. This is for example used in a helper routine in _test-drive_:

> <https://github.com/fortran-lang/test-drive/blob/2127360/src/testdrive.F90#L1617-L1639>

Storing into a stdlib `string_type` or similar allows you to also have them as array without having to worry about padding.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [September 23, 2022, 3:10pm UTC](https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395/5 "2022-09-23T15:10:49Z")

</div>

The other comments about using allocatable strings is probably the best solution to your problem. However, to answer your original question, the following will eleminate typing the 128 magic number multiple times:

```auto
  integer, parameter :: lmax = 128
  character(len=lmax) :: arg
  character(len=lmax), dimension(:), allocatable :: args

  arg_counter = command_argument_count()
  allocate(args(arg_counter))

  do i = 1, arg_counter
    call get_command_argument(i, args(i), arg_length)
    if (arg_length > lmax) then
      ...
    end if
  end do

```

You are free to use any parameter name, of course, there are no reserved keywords in fortran. In these declarations, `lmax` must be a parameter (known at compile time), not a variable. In other cases, e.g. with automatic variables, then a variable length, say a dummy argument or a module variable, would also be allowed. If you decide later to change the max length to, say 256, then you just change that one line of code.

---

<div class="post-metadata">

**Author:** ![FLNewbiee](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/flnewbiee/32/2155_2.png) [@FLNewbiee](https://fortran-lang.discourse.group/u/FLNewbiee)\
**Post date:** [September 23, 2022, 3:33pm UTC](https://fortran-lang.discourse.group/t/how-to-get-query-the-defined-length-of-a-character-type-variable/4395/6 "2022-09-23T15:33:15Z")

</div>

Moreover, `status` argument would also be used instead of `arg_length > lmax` so that

```fortran
  integer, parameter :: lmax = 128
  character(len=lmax) :: arg
  character(len=lmax), dimension(:), allocatable :: args

  arg_counter = command_argument_count()
  allocate(args(arg_counter))

  do i = 1, arg_counter
    call get_command_argument(i, args(i), arg_length, status)
    if (status == -1) then
      ...
    end if
  end do

```
