Early versions of C (well before the first ANSI standard in 1989) did not support const
at all. So functions in the standard library, like fopen()
were originally specified without the const
qualifier.
In some professional settings, using old code and old compilers is still sometimes required for reasons of backward compatibility (although such cases are getting rarer as time advances, they still do exist, and for various reasons will continue to exist for some time). This requires that libraries used in those settings remain backward compatible.
Unless there is a specific constraint like this, it is preferable to write modern code using const
appropriately (for example, to indicate that a function does not change an argument passed by pointer). That provides more opportunities to detect programmer error (e.g. a function that changes an argument, which the caller assumes is unchanged).
A common reason for code that doesn't use const
is programmer (or, worse, customers) caring more about quick delivery - getting code delivered that compiles and passes a few test cases without error is priority number 1, but getting the code working properly in every possible contingency is considered much less important (or someone else's problem). In such a setting const
causes more compilation errors (e.g. due to a function changing something tagged const
) and therefore requires more effort and time to get the code compiling. Whereas omitting const
means the code compiles more easily. Removing all instances of const
in a code base (unless it uses third party libraries that are not available as source) is quite easy with modern text editors and IDEs - and can superficially cause the number of compilation errors to drop. Conversely, it is more difficult to identify all the places in code where const
is useful, and the most likely immediate reward for increasing usage of const
in a code base is needing to do more work to fix code that then doesn't compile.
In other words, the benefits of const
are not immediate, so do not matter to developers/managers/customers with a short term "deliver or die" mentality, who will not have to subsequently fix the problems they leave behind.