6 ms·
The reality is that C `int` is 32 bits in size. Sure, that's not true for 16 bit targets. But are you really going to port a 5Mb program to 16 bits? It's not w
by WalterBright 3y ago
The reality is that C `int` is 32 bits in size.
Sure, that's not true for 16 bit targets. But are you really going to port a 5Mb program to 16 bits? It's not worth worrying about. Your code is highly unlikely to be portable to 16 bits anyway.
The problem is with `long`, which is 32 bits on some machines and 64 bits on others. This is just madness. Fortunately, `long long` is always 64 bits, so it makes sense to just abandon `long`.
So there it is:
char - 8 bits
short - 16 bits
int - 32 bits
long long - 64 bits
Done!
(Sheesh, all the endless hours wasted on the size of an `int` in C.)
- deleted 3y ago[deleted]
- consp 3y agoWhich are minima, but in practice they represent the width.
- Findecanor 3y agoYet another issue is that `char` is signed on some platforms but unsigned on others. It is signed on x86 but unsigned on RISC-V. On ARM it could be either (ARM standard is unsigned, Apple does signed). I therefore use typedefs called `byte` and `ubyte` wherever the data is 8-bit but not character data. I also use the aliases `ushort`, `uint` and `ulong` to cut down on typing. On the other hand, the types in <stdint.h> are often recognised by syntax colouring in editors where user-defined types aren't.
- hasmanean 3y agoDefault signed/unsigned is not a platform convention but a compiler one. You can change it with a compiler switch, in the makefile.
- chmod775 3y agoThen you're better off using custom types - that way people will immediate know your type is non-default - as opposed to hiding your customization away in a makefile, pranking people who expect built-ins to behave a certain way.
- munch117 3y agoThe people who understand that it can be either, depending on a compiler switch, are exactly the people who use an explicit sign (typically via a typedef) to ensure their code always works. The people who say that char is de facto signed and everyone should just deal with it, are the people who end up writing broken code.
- WalterBright 3y agoUm, it was dependent on how the CPU handled it back in those days. That problem went away, though.
- ChrisRR 3y ago"might" be able to change it. It's fine in GCC based compilers, but some awkward/old compilers don't give you the luxury
- WalterBright 3y agoYes, the optional sign on char is also madness. C had a chance in 1989 to make it unsigned, and muffed it. (When C86 decided between value-preserving and sign-preserving semantics, they could have also said char was unsigned, and saved generations of programmers from grief.) D's `char` type is unsigned. Done. No more problems.
- self_awareness 3y agoIsn't "int" 64-bit on some (rare) architectures?
- colejohnson66 3y agoNot sure about 64-bit `int`, but it is 16-bit on some 16-bit micros, such as the AVR line (used by the original Arduino).
- galangalalgol 3y agoTi dsps have 48bits for long.
- colejohnson66 3y agoYep. DSPs always have weird architectures, but in most cases, one isn't compiling the same code for multiple DSP architectures. As an example, the C2000 line has a 16-bit `char`; There is no support for "bytes".
- i_am_a_peasant 3y agoyeah i had 48 bit pointers on one asic. upper 16 bits selected memory partition/region
- grotorea 3y agoNot mentioned on the table I know https://en.cppreference.com/w/cpp/language/types https://en.cppreference.com/w/cpp/language/types edit: Oh you're right > Other models are very rare. For example, ILP64 (8/8/8: int, long, and pointer are 64-bit) only appeared in some early 64-bit Unix systems (e.g. UNICOS on Cray).
- terracottalite 3y agoMy computer being one of those (rare?) architectures. Though I think it is not entirely dependent on the processor and the OS choice also affects this.
- rurban 3y agoC is also used on embedded. It's even there the default language.
- ultrarunner 3y agoYeah, almost the only time I'm writing C anymore is embedded, where I want to reason about type widths (while taking on as light a cognitive load as is possible). I have enough code that gets compiled to an 8, 16, or 32 bit target depending on context that having the bit width right on the tin is valuable. And it doesn't even cost me "hours and hours".
- ryandrake 3y agoAlso: Embedded is almost the only time you really, truly need to care about how many bits a type is, and only when you're interacting with actual hardware. For almost every other routine task in programming, I would argue that it really doesn't matter if your int is 32 bits wide or 64 bits wide. Why go through the trouble of insisting on int32_t or int64_t? It probably doesn't matter for the things you are counting. Some programmers will say "Well, we should use int64_t here because int32_t might overflow!" OK, so why weren't you checking for overflow if it was an expected case? int64_t might overflow too, are you checking after every operation? Probably not. "OK, let's use uint64_t then, now we get 2x as many numbers!" Now you have other overflow (and subtraction) problems to handle. Nowadays, I just use int and move on with my life. It's one of those lessons from experience: "When I was younger, I used int and char because I didn't know any better. When I was older, I created this complex, elaborate type system because I knew better. Now that I'm wise, I just use int and char."
- terracottalite 3y ago> 2x as many numbers Minor correction, 2^32x as many numbers. Though I agree with your point. Edit: added x to the number for consistency and clarity.
- ryandrake 3y ago
- slikrick 3y agoso... what I'm seeing is that C got it wrong relative to the way things actually work and get used. the fact that you had to have tribal knowledge about all of this is why C shouldn't stay for the long term and we should phase out languages into ones with stronger more correct defaults. would a new programmer use "long long"? would they notice immediately that things didn't work if they didn't use it? Rust got it correct by labeling the bits with the type directly
- lelanthran 3y agoYou realise that C had labeled types long before Rust was conceived?
- kazinator 3y agoRust's integer types are poorly abstracted. The use of specifically sized types for quantities that are not related to hardware is comically ridiculous. In the C world, only the goofballs do things like use char or int8_t for the number of children in a family, or wheels on a car. yet that is what Rust code looks like. Almost every Rust code sample I've ever seen sets off my bozon detector just for this reason.
- stephc_int13 3y agoLong Long is ridiculous and often confusing. Now, what about SIMD types? What about 16bits floats? Using the short size convention we have easy and logical answers. The reason why new languages like Rust and Zig are using those conventions is not random, types naming (and stdlib) is a weak point of C (and C++). Luckily they are not set in stone, we can choose different and reasonable conventions.
- sovande 3y ago> Long Long is ridiculous and often confusing It might be ridiculous, but it’s hardly confusing for a C programmer. But, yeah in and ideal world ‘long’ should just be defined as 64 bits
- WalterBright 3y agoI use `halffloat` for 16 bit floats. But be careful, there are several different encodings of 16 bit floats, so float16 isn't enough in and of itself. SIMD types in D are done with: __vector(byte[16]), __vector(int[8]) and an alias (typedef for the C folk) for this is commonly used, like `byte16` and `int8`.
- sovande 3y agoExactly this (plus floating point types and unsigned qualifier) and done. It’s standard C, there is no need to invent yet another unnecessary “type” system for standard C native types. I do like bool though.
- aidenn0 3y agoI swear I've seen 128-bit "long long" types.
- Am4TIfIsER0ppos 3y agoWait... where's plain "long"? I know, you probably know, but that is why you use explicit sizes where you can.
- WalterBright 3y agoI quit using "long" because sometimes a long is 32 bits and sometimes 64, and I can never remember which compiler does which. But "int" is 32 bits and "long long" is always 64 bits, so that's what I stick with. C's "long" should not be used in new code.
- kazinator 3y agoThe type that is 32 bits in C is int32_t, and the 64 bit one is int64_t; if you really want those specific widths, you can just use those types. The type long is the smallest ranking basic type that is at least 32 bits wide. Since int is only required to go to 32767, you use long if you need a signed type with more range than that. That made a lot of sense on platforms where int really did go up to just 32767, and long provided the 32 bit one. Now long, while at least 32 bits, is not required to be wider than 32; if you need a signed type that goes beyond 2147483647, then long long is it. Those are the portability rules. Unfortunately, those rules will sometimes lead you to choose types that are wider than necessary, like long when int would have worked. Where that matters, it's best to make your code tunable with your own typedefs. I don't mean typedefs like i32 but abstract ones, like ISO C's time_t or clock_t, or POSIX's pid_t. You can adjust your types without editing numerous lines of code.
- kazinator 3y agoThe Motorola 68K family has been targeted by C compilers configured with 16 bit int.
- WalterBright 3y ago"has been", i.e. obsolete
- kazinator 3y agoChoosing integer sizes in C is pretty easy. The standard guarantees certain minimum ranges. 1. Consider the char and short types only if saving storage is important. Do not declare "char number_of_wheels" for a car, just because no car has anywhere near 127 wheels, unless it is really important to get it down to one byte. 2. Prefer signed types to unsigned types, when saving storage is not important. Unsigned types bend the rules of arithmetic around zero, and mixtures of signed and unsigned arithmetic add complexity and pitfalls. Do use unsigned for bitmasks and bitfields. 3. Two's complement is ubiquitous: feel free to assume that signed char gives you -128, and short gives you -32768, etc. ISO C now requires two's complement. 3. Use the lowest ranking type whose range is adequate, in light of the above rules: rule out the chars and shorts, and unsigned types, unless saving space or working with bits. For instance, for a value that ranges from 0 to 65535, we would choose int. If it were important to save storage, then unsigned short. The ISO C minimum required ranges are: char 0..255, if unsigned; -128..127 if unsigned, therefore: 0..127 signed char -128..127 unsigned char 0..255 short -32768..32767 unsigned short 0..65535 int -32768..32767 unsigned int 0..65535 long -2147483648..2147483647 unsigned long 0..4294967295 long long 9223372036854775808..9223372036854775807 unsigned long long 0..18446744073709551615 If you're working with bitfields, and saving storage isn't important, start with unsigned int, and pick the type that holds all the bits required. For arrays of bitfields, prefer unsigned int; it's likely to be fast on a given target. It's good to leave that configurable the program. E.g. a good "bignum" library can easily be tuned to have "limbs" of different sizes: 16, 32 or 64 bit, and mostly hides that at the API level. If you're working with a numeric quantity, remove the unsigned types, shorts and chars, unless you need to save storage (and don't need negative values). Then pick the lowest ranking one that fits. E.g. if saving storage, and don't need negative values, search in this order: char, signed char, unsigned char, short, unsigned short, long, unsigned long, long long, unsigned long long. If saving storage, and negatives are required: signed char, short, int, long, long long. If not saving storage: int, long, long long. If the quantity is positive, and doesn't fit into long long, but does fit into unsigned long long, that's what it may have to be.
- epcoa 3y agoUnsigned doesn’t really bend any rules and their behavior around wraparound is well defined. Therefore there is another use case : circular buffer indices.
- ChrisRR 3y agoOh I don't even know where to start with this. Given that C is the lingua franca of embedded development, and each processor and compiler has different opinions of what an int is, I would never claim that an int is 32 bits. It's just so much less error prone to define a uint32_t. That's guaranteed to be the same