G-Fact 13

const Behaviour in C and C++

In C, the const qualified identifiers will have external linkage, where as in C++ it will have internal linkage. For example,

In C++, the following statement



filter_none

edit
close

play_arrow

link
brightness_4
code

float const interest_rate = 9.25;

chevron_right


is implicitly defined as

filter_none

edit
close

play_arrow

link
brightness_4
code

static float const interest_rate = 9.25;

chevron_right


i.e. the scope of interest_rate is limited to the block in which it is defined.

In C, the above statement will have external linkage when defined at file scope, i.e. it will be visible outside the current translation unit (source file).

The internal linkage of const qualified variables have some advantages in C++. We will cover them in next article.

Thanks to Venki for writing the above fact. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.