C/C++ Low Level Curriculum Part 7: More Conditionals
altdevblogaday.com
altdevblogaday.com
const int bytesPerPixel = haveAlphaChannel() ? 4 : 3;
This is useful for optimizating compilers - the more values in a function you can make const the better it can do. And is especially useful for parallel code like OMP/TBB
Adding const there requires the value to be a compile-time constant, and guarantees the optimizer can inline the value rather than reading from memory. Though it still can inline the value in many cases if it can prove the variable isn't modified.
Of course, this precludes conditional constructs such as the one proposed by excuse-me; that only works as a local var and the const is again irrelevant to the optimizer.
If it's const at the file scope it's notably NOT static in the 'visible in the file scope only' sense of the word.
Try it, declare a const int at file scope in a C file and use it in a different C file with a extern prototype.