Sujet : Re: Suggested method for returning a string from a C program?
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 22. Mar 2025, 18:00:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrmqba$933o$3@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : Mozilla Thunderbird
On 20/03/2025 23:18, Keith Thompson wrote:
bart <bc@freeuk.com> writes:
On 20/03/2025 19:10, Keith Thompson wrote:
bart <bc@freeuk.com> writes:
[...]
stdint.h et al are just ungainly bolt-ons, not fully supported by the
language.
No, they're fully supported by the language. They've been in the ISO
standard since 1999.
>
I don't think so. They are add-ons that could have been created in
user-code even prior to C99 (user-defined typedefs for 64 bits would
'need long long').
Sure, they could; see Doug Gwyn's q8, for example.
All that's happened is that 'stdint.h' has been blessed.
I.e., it was made part of the language, specifically the standard
library that's part of the language standard. Which is what I said,
but for some reason you disagreed.
Yes, the format specifiers are a bit awkward. Boo hoo.
There's a further problem here:
-------------------------------------
#include <stdio.h>
#include <stdint.h>
#define strtype(x) _Generic((x),\
default: "other",\
char: "char",\
signed char: "signed char",\
short: "short",\
int: "int",\
long: "long",\
long long: "long long",\
unsigned char: "unsigned char",\
unsigned short: "unsigned short",\
unsigned int: "unsigned int",\
unsigned long: "unsigned long",\
unsigned long long: "unsigned long long",\
int8_t: "int8",\
int16_t: "int16",\
int32_t: "int32",\
int64_t: "int64",\
uint8_t: "uint8",\
uint16_t: "uint16",\
uint32_t: "uint32",\
uint64_t: "uint64"\
)
int main(void) {
uint64_t x;
puts(strtype(x));
}
-------------------------------------
Many of the types are aliases of each other. Which ones will be aliases, can vary by platform.
This is another reason that this was a poor solution.
(My language also has some aliases, but they are defined as such.
The built-in types are the set 'i8 ... u64', with aliases defined on top such as 'byte' for 'u8', and 'int' for 'i64' on a particular implementation.
This is the opposite of how it works in stdint.h, where specific-width types are defined on top of non-specific-width ones! It's just backwards.
It also doesn't need _Generic for this purpose; this will work:)
u64 x
puts(x.typestr)