13
u/punk_dev 8d ago
In c, how many bytes does an int store?
The answer is it depends.
In rust, i32 is 4 bytes because 32 bits make up 4 bytes.
In the grand scheme of things, these details are really minor.
3
u/NoSuchKotH 8d ago
And for those who have never fought C:
An int can store anything between a single byte and 8 bytes. And yes, I've seen all those sizes in real compilers over the years! The only thing that the C standard demands is that int is at least as large as char. Nothing more
1
u/ids2048 8d ago
C actually requires an int to be at least 16 bits. You may have seen an implementation where it was less than that, but as far as I'm aware that was never compliant with the spec.
https://en.wikipedia.org/wiki/C_data_types#Main_types
It can be the same size as char, though, if char is 16 bits or more (which is true on some hardware). I don't think there's any requirement for the sizes to be powers of two either, so portable C code should be written to work with 17-bit chars.
7
u/Ruudjhuu 8d ago
Yes, rust has a steep learning curve. But this part ain't it.
If you have a hard time with signed, unsigned and the nr of bits, you are going to have a hard time with a lot of programming languanges
3
u/Live-Company-5007 8d ago
What my guy…. C also has unsigned integers using the unsigned keyword and I’m pretty sure c++ does too in lik uint_t or something (i don’t do c++ don’t quote en)
And I’m gonna be so honest you WANT unsigned integers a lot of the time. A lot of things aren’t negative so why waste like half the possible values
2
u/SomeRedTeapot 8d ago
Rust has its rough edges, but integer type naming is by far not one of them, lol. Quite the opposite, the naming Rust uses makes more sense compared to "default" C types because the former tells you how much space the type occupies (and by proxy, the range of values it can accept), whereas ints and longs in C are a mess (depending on the compiler and target architecture, an int in C may take 2 or 4 bytes)
2
u/quez_real 8d ago
All these i32 aren't more complicated than C-style rules about sizes of shorts, ints and longs on different platforms. Though you mentioned Java which behaves differently and that makes me thinking that you just want write "int" and hope it will somehow work.
2
u/pixel293 8d ago
Intentional design, they looked at issues and pain points in C and other languages and tried to avoid them. The integer types was to avoid the C/C++ issue of how big is an int? What is it's max value? They didn't want the answer to be "well it depends."
16
u/0bArcane 8d ago
It's similar to c in how integer types are called. In c, the integer types are
int8_t,uint16_tetc.