Character, c_char, byte

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…

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…

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.

More than a guess, it’s even granted :slight_smile: … 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).

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…)

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:

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).

1 Like

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.

2 Likes