104

If you want to use Qt, you have to embrace quint8, quint16 and so forth.

If you want to use GLib, you have to welcome guint8, guint16 and so forth.

On Linux there are u32, s16 and so forth.

uC/OS defines SINT32, UINT16 and so forth.

And if you have to use some combination of those things, you better be prepared for trouble. Because on your machine u32 will be typedefd over long and quint32 will be typedefd over int and the compiler will complain.

Why does everybody do this, if there is <stdint.h>? Is this some kind of tradition for libraries?

17
  • 8
    @Mehrdad in microcontroller programming you can have all sorts of things. On AVR Mega's for example (and consequently on famous Arduino) int is 16 bit. That may be a nasty suprise. In my opinion, 'unsigned short' requires more typing effort. And it always made me sad using 'unsigned char' for <s>byte</s> octet. Unsigned character, really?
    – Amomum
    Jul 25, 2016 at 0:42
  • 2
    @Mehrdad The point is that you can't really be sure. That's exactly why stdint.h was invented.
    – glglgl
    Jul 25, 2016 at 9:07
  • 1
    @glglgl: Here's another way to look at the problem: aren't you asking precisely the wrong question? If you're targeting multiple systems, why arbitrarily hard-code the number of bits into the code in the first place? i.e., why not just say sizeof(int) * CHAR_BIT (for example) and use that? If your int is too small to represent your range (e.g. an array index), then you almost certainly shouldn't be using int anyway, but something like size_t. Why would int32 make any more sense? The only time fixed width makes sense is for communication between systems (e.g. file/network format)...
    – user541686
    Jul 25, 2016 at 10:06
  • 1
    @Mehrdad No. Sometimes I have values (such as from an ADC or whatever) that I need to store. I know they are 16 bit wide. So the best ting to use is uint16_t (or maybe its fast or least variant). My point being: These types are convenient to use and have their reason of existence.
    – glglgl
    Jul 25, 2016 at 10:35
  • 1
    @Mehrdad: I would suggest that – assuming it seems worth your effort to produce quality code – you should define your own functional typedefs meaning the way to interact with my API / the rest of my code, and define these on technical grounds in terms of “technical” typedefs like size_t and/or uint64_t.
    – PJTraill
    Aug 1, 2016 at 12:23

4 Answers 4

81

stdint.h didn't exist back when these libraries were being developed. So each library made its own typedefs.

9
  • 4
    And okay, I suppose the lack of stdint.h is a good reason, but why even today these typedefs are over int, long and so for and not over stdint types? It would make them interchangeable atleast
    – Amomum
    Jul 24, 2016 at 13:14
  • 25
    @Amomum "Why Glib (that was developed on Linux) didn't use Linux typedefs?" While the "home base" of glib certainly is Linux it is as certainly by design a portable library. Defining its own types ensures portability: One only has to adapt a tiny header which matches the library types to the proper respective target platform types. Jul 24, 2016 at 13:27
  • 3
    @Amomum Why Glib (that was developed on Linux) ... No, it wasn't. Glib was created way before the Linux kernel.
    – andy256
    Jul 25, 2016 at 5:01
  • 5
    @andy256 "GLib" is not short for "glibc". It's a library that branched off from gtk. It's not older than Linux.
    – user2404501
    Jul 25, 2016 at 12:05
  • 2
    @andy256: glib is 1998, linux is 1991. IOW, GLib was created way after Linux.
    – MSalters
    Jul 25, 2016 at 13:18
40

For the older libraries, this is needed because the header in question (stdint.h) didn't exist.

There's still, however, a problem around: those types (uint64_t and others) are an optional feature in the standard. So a complying implementation might not ship with them -- and thus force libraries to still include them nowadays.

22
  • 14
    The uintN_t types are optional, but the uint_leastN_t and uint_fastN_t types are not. Jul 24, 2016 at 13:29
  • 7
    @Kusalananda: Sadly those are of limited usefulness. Jul 24, 2016 at 14:42
  • 17
    Of course the reason that they are optional is that you are not guaranteed that there are integer types with exactly that number of bits. C still supports architectures with rather odd integer sizes.
    – celtschk
    Jul 24, 2016 at 16:04
  • 5
    @LightnessRacesinOrbit: How are they of limited usefulness? I must admit that apart from hardware interfaces, I don't see why you would need an exact number of bits, rather than just a minimum, to ensure all your values fit in.
    – celtschk
    Jul 24, 2016 at 16:07
  • 24
    @Amomum The typedefs are required if the implementation has types meeting the requirements: "However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names." (quote from N1570, 7.20.1.1 "Exact-width integer types") So if the standard library doesn't have them, a third-party library couldn't either, it seems. Jul 25, 2016 at 0:54
13

stdint.h has been standardised since 1999. It is more likely that many applications define (effectively alias) types to maintain partial independence from the underlying machine architecture.

They provide developers confidence that types used in their application matches their project specific assumptions on behavior that may not match either the language standard or compiler implementation.

The practice is mirrored in the object oriented Façade design pattern and is much abused by developers invariably writing wrapper classes for all imported libraries.

When compliers were much less standard and machine architectures could vary from 16-bit, 18-bit through 36-bit word length mainframes this was much more of a consideration. The practice is much less relevant now in a world converging on 32-bit ARM embedded systems. It remains a concern for low-end microcontrollers with odd memory maps.

5
  • 1
    Granted, stdint.h has been standardized since 1999, but how long has it been available in practice? People drag their feet implementing and adopting new standards, and during that long transitional period, old methods still are a must.
    – Siyuan Ren
    Jul 25, 2016 at 3:07
  • 1
    One nasty gotcha with stdint.h is that even on platforms where e.g. long and int32_t have the same size and representation, there's no requirement that casting an int32_t* to long* will yield a pointer that can reliably access a int32_t. I can't believe the authors of the Standard thought it obvious that layout-compatible types should be alias-compatible, but since they didn't bother saying so the authors of gcc and IIRC clang think the language would be improved by ignoring aliasing even in cases where it's obvious.
    – supercat
    Jul 25, 2016 at 15:49
  • 2
    @supercat -- that's probably worth submitting as an erratum to the C committee...because that is gratuitously dumb, to put it mildly
    – LThode
    Jul 25, 2016 at 16:27
  • @LThode: For the C committee to acknowledge that as a mistake would require that it officially declare the behavior of clang and gcc as obtuse. Do you think that's going to happen? The best that could be hoped for (and IMHO the logical way to proceed) would be to define ways for programs to specify "aliasing modes". If a program says specifies that it can accept very strict aliasing rules, then a compiler could use that to allow optimizations beyond what's presently possible. If a program specifies that it requires rules that are somewhat more programmer-friendly than the present ones...
    – supercat
    Jul 25, 2016 at 16:47
  • ...but would still allow many useful optimizations, then a compiler could generate code that was much more efficient than would be possible with -fno-strict-alias, but which would still actually work. Even if there weren't an existing code base, no single set of rules could strike the optimal balance of optimization and semantics for all applications, because different applications have different needs. Add in the existing codebase and the need for different modes should be clear.
    – supercat
    Jul 25, 2016 at 16:49
3

So you have the power to typedef char to int.

One "coding horror" mentioned that one companies header had a point where a programmer wanted a boolean value, and a char was the logical native type for the job, and so wrote typedef bool char. Then later on someone found an integer to be the most logical choice, and wrote typedef bool int. The result, ages before Unicode, was virtually typedef char int.

Quite a lot of forward-thinking, forward compatibility, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.