What is the difference between static const and const?

Lior picture Lior · Nov 1, 2012 · Viewed 91k times · Source

What is the difference between static const and const? For example:

static const int a=5;
const int i=5;

Is there any difference between them? When would you use one over the other?

Answer

Joe picture Joe · Nov 1, 2012

static determines visibility outside of a function or a variables lifespan inside. So it has nothing to do with const per se.

const means that you're not changing the value after it has been initialised.

static inside a function means the variable will exist before and after the function has ended.

static outside of a function means that the scope of the symbol marked static is limited to that .c file and cannot be seen outside of it.

Technically (if you want to look this up), static is a storage specifier and const is a type qualifier.