Thankfully, in addition to what MaxBarraclough helpfully pointed out, every (u)intN_t type provided by is guaranteed to use two's complement even in C99.
Summary of C/C++ integer rules
41–50 of 98 posts
Re: Summary of C/C++ integer rules
#42Earlier quoted context omitted.
> char, signed char, and unsigned char are distinct types, but that's only true of char. That's correct, I was going to bring that up too. This is particularly important because char and unsigned char are special in that they are an exception the aliasing rules. That is, in this function: float foo(char* cp, float* fp) { *fp = 7; return *(float*)cp; } /* ... */ float f = 2; float g = foo((char*)&f, &f); Then g should…
>So you could end up with (at least) 5 types that are 8-bit wide! Don't forget std::byte.
Re: Summary of C/C++ integer rules
#43Earlier quoted context omitted.
It depends on what you are doing. For some kinds of programs, C/C++ are going to be much faster than most "modern" languages.
I didn't mean C is not fast or not faster than other languages. It's still the fastest one I believe. What I meant is undefined behaviors allow compilers to optimize in a way that would not be possible otherwise. So, it might be a deliberate decision back then, to leverage performance. I don't know, just an idea.
Re: Summary of C/C++ integer rules
#44Earlier quoted context omitted.
Nowadays, it doesn't provide any performance gain. I didn't see those days but maybe it was important for performance back in 70s/80s/90s even it was risky? e.g null terminated string was chosen due to low space overhead.
It depends on what you are doing. For some kinds of programs, C/C++ are going to be much faster than most "modern" languages.
Re: Summary of C/C++ integer rules
#45It’s funny that you would end up with a similar conclusion for other parts of the language (e.g. operators) as well. Just a gigantic set of inane rules everywhere causing you to constantly be in danger of introducing bugs and portability issues.
But! And that's important -- it allows for great performance, so you can make ten/hundred times more mistakes per second than in other, "safer" languages.
This is false. For a long time C performance used to be inferior to Fortran, which is arguably safer than C. It's hilarious that the strict aliasing and `restrict` keyword was born out of making C on par with Fortran and UB became a major issue to C programmers as a result!
Re: Summary of C/C++ integer rules
#46This is one of the misconceptions: > sizeof(T) represents the number of 8-bit bytes (octets) needed to store a variable of type T. That's a misconception I had and I've never run into a problem. What's a platform where sizeof works differently? Also, what's the reasoning for sizeof to be an operator rather than a function?
See https://stackoverflow.com/questions/2098149/what-platforms-h... . As for `sizeof` being an operator, well, C doesn't have generics, so it has no choice but to make `sizeof` somehow special. If you don't want to bother supporting platforms where byte is not 8-bit (a reasonable choice I would say), use `int8_t`/`uint8_t` instead. Those types won't exist on platforms that don't have 8-bit bytes.
It could have used a different syntax though. Ada has a special syntax for compile-time inquiries like this, so there's no way to confuse them with function calls. Ada calls these attributes.
https://en.wikibooks.org/wiki/Ada_Programming/Attributes#Lan...
Re: Summary of C/C++ integer rules
#47Earlier quoted context omitted.
I didn't mean C is not fast or not faster than other languages. It's still the fastest one I believe. What I meant is undefined behaviors allow compilers to optimize in a way that would not be possible otherwise. So, it might be a deliberate decision back then, to leverage performance. I don't know, just an idea.
It used to be "folk knowledge" that only Fortran and hand-crafted ASM were faster. Not sure if that's still (or ever was) true.
Re: Summary of C/C++ integer rules
#48One thing I think should have been mentioned: size_t is guaranteed to be large enough to index all of memory, which is why it is the return type of size of.
Re: Summary of C/C++ integer rules
#49Earlier quoted context omitted.
It discouraging. If the language requires you actually know what you're doing you can't hire dirt-cheap easily-replaced code monkeys to bang out your ideas and the end result is you get to keep less of the investors' money for yourself.
It can feel good to imagine yourself an enlightened master among code monkeys, yet on practice, everybody can be a code monkey sometimes, and when this happens in C/C++, it will leave a ticking time bomb in the codebase, that will lay there until a customer blows up on it, no matter how many millions went into QA of the product. And on practice, C/C++ developers are among lower-paid programmers - probably because "ba…
Re: Summary of C/C++ integer rules
#50> Having signed and unsigned variants of every integer type essentially doubles the number of options to choose from. This adds to the mental burden, yet has little payoff because signed types can do almost everything that unsigned ones can. Unsigned types are quite useful when doing bit twiddling because they don't overflow or have a bit taken up by the sign.
> Unsigned types are quite useful when doing bit twiddling because they don't overflow or have a bit taken up by the sign. That's essentially their only application. The rest are stupid single-bit memory-size optimizations. As Jens Gustedt noted, it's one of the (many) misnomers in the C language. It should be better called "modulo" instead of "unsigned". Other such misnomers that I recall: unsigned -> modulo char ->…
What about when it doesn't make semantic sense to have negative values? Eg for counting things, indexing into a vector, size of things. If negative doesn't make sense, I use unsigned types. Its not about the memory-size in that case.