# Character, c\_char, byte

**URL:** https://fortran-lang.discourse.group/t/character-c-char-byte/7782
**Category:** Help
**Created:** [April 7, 2024, 8:49am UTC](https://fortran-lang.discourse.group/t/character-c-char-byte/7782 "2024-04-07T08:49:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)
#### Post date: [April 7, 2024, 8:49am UTC](https://fortran-lang.discourse.group/t/character-c-char-byte/7782/1 "2024-04-07T08:49:05Z")

</div>

Hello,

Is it safe to assume that the storage size of a Fortran `character` is 1 byte? In practice yes, but I think that nothing guarantees it in the standard, am I correct? Is there any solution to get a 1-byte object (whatever the type) other than using `iso_c_binding` and `character(kind=c_char)`?

Related question: in `iso_c_binding` is there an integer kind that interoperates with the C `char` type?

BTW, in C I’m always confused by the fact that a `char` can be used almost indifferently as a character or an integer…

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [April 7, 2024, 10:19am UTC](https://fortran-lang.discourse.group/t/character-c-char-byte/7782/2 "2024-04-07T10:19:26Z")

</div>

The only thing that seems certain is in the C standard:

> number of bits for smallest object that is not a bit-field (byte)  
> CHAR\_BIT 8

It is the only C integer whose size is fixed in `<limits.h>`. Others depends on the OS / CPU / compiler…

In `iso_c_binding`, there is the character kind `c_char`.

In Fortran, I think you are just certain that a character can contain an ASCII code (0 \<= n \<=127). You need at least 7 bits, but any greater number could do the job…

I guess that with `c_char`, the Fortran compiler is compelled to use a byte to assure interoperability in the two directions…

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [April 7, 2024, 10:28am UTC](https://fortran-lang.discourse.group/t/character-c-char-byte/7782/3 "2024-04-07T10:28:27Z")

</div>

In the C standard (here 2018), you have also `int8_t`

> 7.20.1.1 Exact-width integer types  
> 1 The typedef name intN\_t designates a signed integer type with width N, no padding bits, and a two’s complement representation. Thus, int8\_t denotes such a signed integer type with a width of exactly 8 bits.

And in `iso_c_binding`, you have `c_int8_t`.

---

<div class="post-metadata">

### Author: ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)
#### Post date: [April 7, 2024, 10:52am UTC](https://fortran-lang.discourse.group/t/character-c-char-byte/7782/4 "2024-04-07T10:52:19Z")

</div>

> [@vmagnin](#):
>
> I guess that with `c_char`, the Fortran compiler is compelled to use a byte to assure interoperability in the two directions…

More than a guess, it’s even granted 🙂 … I was just wondering if there was a way in pure Fortran (i.e. without C interoperability) to select some 1-byte object (but since the standard doesn’t mention bytes, I doubt it’s possible; anyway, using the C interoperability is fine to me).

> [@vmagnin](#):
>
> In the C standard (here 2018), you have also `int8_t`
> 
> And in `iso_c_binding`, you have `c_int8_t`.

Yep… But strictly speaking it doesn’t say it’s a byte (OK, it’s a byte on virtually all existing machines nowadays, but as you said in another topic, on the long term, who knows…)

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [April 7, 2024, 11:01am UTC](https://fortran-lang.discourse.group/t/character-c-char-byte/7782/5 "2024-04-07T11:01:22Z")

</div>

> [@PierU](#):
>
> I was just wondering if there was a way in pure Fortran (i.e. without C interoperability) to select some 1-byte object (but since the standard doesn’t mention bytes, I doubt it’s possible; anyway, using the C interoperability is fine to me).

Indeed, the word `byte` appears only in the C interoperability section…

In gtk-fortran, to pass pixel buffers (1D arrays of RGB values coding a picture), we have always (since 2011) used that kind of declaration:

```fortran
character(kind=c_char), dimension(:), pointer :: pixel

```

Until now, we never had a problem with Linux (32 and 64 bits), Windows, macOS, FreeBSD, and even Raspberry Pi (ARM CPU).

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [April 7, 2024, 1:04pm UTC](https://fortran-lang.discourse.group/t/character-c-char-byte/7782/6 "2024-04-07T13:04:54Z")

</div>

> [@PierU](#):
>
> but as you said in another topic, on the long term, who knows…

With etching techniques being already around a few nanometers (smallest details in a transistor), who knows what kind of electronics components will be present in a microprocessor in 25 years? Will some quantum effects be used? Will transistors still be used with only two states (0, 1)? Will there be some specialized quantum circuits inside CPUs? Or using light? Just real questions, I have nothing special in mind and I am not Nostradamus. What is at the end of the road? Let’s be prepared to be surprised.

> **[Imec Presents Sub-1nm Process and Transistor Roadmap Until 2036: From...](https://www.tomshardware.com/news/imecs-sub-1nm-process-node-and-transistor-roadmap-until-2036-from-nanometers-to-the-angstrom-era)**
>
> Imec plots a course to 1nm chips, and beyond
