On 21/08/18 23:25, Anton Shepelev wrote:
> David Brown:
>
>> And if I were designing a language that actually replaced
>> C, I would probably define the equivalent of "int" as
>> "int_fast16_t" - define the fixed types first, and have
>> the abstract types being implementation-dependent typedefs
>> for the most appropriate concrete type.
>
> Fixed-size types are abstract, because decoupled from the
> underlying implementation and machine-level support and
> embodying a mathematical concept of a number within certain
> limits and with certain properties. One could, for example,
> implement a 64-bit integer on a 32-bit machine.
>
Abstract/concrete is not a binary feature. Fixed size types like
"int32_t" are /more/ concrete than variable types like "int" because
more of their implementation details are fixed - the size, range and
representation. They are still more abstract than an assembly-level
signed integer as they have different overflow characteristics from the
(usual) underlying hardware, and they may not match the register or cpu
size.
> Variable-sized types are concrete, because they correspond
> to machine-level types and are not meant to embody any
> mathematical concept independently of the CPU.
Incorrect.
"int" is supposed to be a type that is efficient to implement on a given
machine - it does not correspond directly or specifically to any
machine-level type or characteristic, its specifications are vaguer, and
it /does/ embody mathematical concepts that are independent (indeed,
directly contrary) to those of the cpu.
In particular, "int" will generally be 16-bit on 8-bit processors - such
cpus do not have direct support for 16-bit values, registers, loads,
stores, or arithmetic. They will generally be 32-bit on 64-bit
machines, even though they do not match the width of the registers or
ALU on the systems, and the cpu may not be able to do atomic loads or
stores of 32-bit objects.
And "int" embodies mathematical principles such as adding a positive
number to an int will always give you a larger int - compilers use such
rules for optimisations, even though the underlying hardware does not
guarantee it.
> They seem
> useless unless one is so hard put to it by considerations of
> efficiency that one is ready to sacrifice portability and
> stability, i.e. the guarrantee that the behavior of a
> program is determined by its code and does not depend on a
> machine where it is compiled as long as it does not run into
> a resource limit.
Some people find variable sizes - int, long, etc. - suit their coding
better than fixed sizes - int32_t, uint64_t, etc. Others have the
opposite view. That's fine - C gives you both, and you can choose.
Other languages give you the ability to create range types, which are
also somewhat abstract and which I find can be very handy. C doesn't
have these, but I can live without them.
>
>> But we are not designing a new language -- we are using an
>> existing language -- C.
>
> Would not it be possible, using macros, smoothly to pass to
> a type system based on types with fixed size and properties,
> e.g. to express int conditionally via i16, i32, and i64, &c?
> Why do you think to C this way is closed?
It would be entirely possible to do this - but the language of C is
slightly too limited for that. Given some fundamental fixed-size types
like those you could define "int", "short" and "long" in those terms
with typedefs or macros. But you could not define "unsigned int", "long
long int", etc. - C's language does not let you make types that require
multiple specifiers.
In a new language, you might avoid the issue entirely by having types
like "long_int" or "unsigned_short" (if you thought they were nice).
>
> People keep proposing modificatins to C, most of which
> increase bloat through addition of new features or extension
> of existing ones, but rarely does anybody suggest the
> counterbalance of removal of features or hardening of
> constraints -- things that help a language stay slim and not
> explode like C++.
I haven't been proposing modifications or extensions to C here - just
having a discussion about alternatives that could have existed. (There
certainly /are/ a few things I would like to add to C.)
And I have seen quite a number of suggestions for removing features or
hardening constraints in this group. It's not long since there was a
thread about removing the obsolete non-prototype function declarations
and function definitions.
>
>> And the types in standard C work fine.
>
> I was shocked when I read that char can be either signed or
> unsigned, and that obtaining a normal abstract fixed-with
> type is in classic C a non-trivial task.
As long as people realise that "char" is a type for holding a character,
and that anything numerical should use "signed char" or "unsigned char"
explicitly, there really is no problem.
Getting fixed-width types in standard C90 (is that what you mean by
"classic C" ?) is not only difficult, it can be impossible. If your
implementation supports the required types as char, short, int, and long
then you can do it with a bit of mess of conditional compilation using
<limits.h>. If it doesn't have the types, then you can't make them.
Prior to C99, the usual method of getting your fixed-size types was an
implementation-specific header. With C99, you used the standardised
<stdint.h> so that your compiler supplier delivered the
implementation-specific header and as a normal programmer, you no longer
had to make it yourself.
>
> As you probably understand, I know very little of C, and
> reading the standard from the middle made me dizzy. It must
> take a tremendous effort to read from cover to cover. And
> what about this crazy idea of having an open standard hidden
> behind a paywall? It defeats the very purpose of an open
> standard.
>
The C standard is not a tutorial or a good way to learn about C - it is
a reference document. It takes a /long/ time, and a special interest,
to be familiar with it. And there is a lot in it that could have been
explained in far easier language and with more logical organisation. If
you want a good reference website, I recommend
<
https://en.cppreference.com/w/c>. The site covers both C and C++, and
is very accurate but a bit more accessible than the standards.
"Open standard" simply means that anyone can get the document, and
anyone can implement it. It doesn't mean you can get the document for
free. (Nor does it mean you get the "source" of the document and can
change it yourself!) But all the draft documents for C and C++
standards are available freely, and generally there are no more than
tiny layout changes between final drafts and published standards. So
for C, the document you want is "N1570" - the final draft of the C11
standard. It is what practically everyone uses, rather than buying the
official C11 standard document.