Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

c - What is the difference between static const and const?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 executed.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...