This question may be naive, but:
- is there
constkeyword in C? - since which version?
- are there any semantic and/or syntactic differences between
constin C and C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are no syntactic differences between C and C++ with regard to
constkeyword, besides a rather obscure one: in C (since C99) you can declare function parameters aswhich is equivalent to
declaration. C++ does not support such syntax.
Semantic differences exist as well. As @Ben Voigt already noted, in C
constdeclarations do not produce constant expressions, i.e. in C you can’t use aconst intobject in acaselabel, as a bit-field width or as array size in a non-VLA array declaration (all this is possible in C++). Also,constobjects have external linkage by default in C (internal linkage in C++).There’s at least one more semantic difference, which Ben did not mention. Const-correctness rules of C++ language support the following standard conversion
These initializations are illegal in C.
Generally, when dealing with multi-level pointers, C++ says that you can add const-qualification at any depth of indirection, as long as you also add const-qualification all the way to the top level.
In C you can only add const-qualification to the type pointed by the top-level pointer, but no deeper.
Another manifestation of the same underlying general principle is the way const-correctness rules work with arrays in C and C++. In C++ you can do
Trying to do the same in C will result in an error