4 ms·
> In Rust we'd use `const` for this but in C you need to use the C pre-processor language instead to make constants, which is kinda wild. I get that you're not
by tredre3 1y ago
> In Rust we'd use `const` for this but in C you need to use the C pre-processor language instead to make constants, which is kinda wild.
I get that you're not very familiar with C? Because in C we'd use const as well.
const int x = 2;
x = 3; // error: assignment of read-only variable 'x'
- superblas 1y agoPerhaps they’re conflating how you can’t use “const” as a compile time constant (e.g., you can’t declare the size of an array with a “const” variable). If so, C23 solves this by finally getting the constexpr keyword from c++
- tialaramex 1y agoThat's not a constant, that's an immutable variable which is why your diagnostic said it was read-only. const int x = 2; int *p = &x; *p = 3; // Now x is 3 And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago, I think we can rule out "unfamiliar with C" as a symptom.
- tredre3 1y agoNow x is 3 but you also get a compiler warning telling you not to do that. In my opinion it's a bit disingenuous to argue that it isn't a const just because you can ignore the compiler and shoot yourself in the foot. If you listen to the compiler, it is reflected in the assembly that it is a constant value the same as #define x 2. Is Rust better at enforcing guarantees? Of course. Is `const` in C `const` if you don't ignore compiler warnings and errors? Also of course. > And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago Ditto!