What is the difference between static const and const?
Asked Answered
M

4

110

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?

Meenen answered 1/11, 2012 at 21:28 Comment(4)
Related: https://mcmap.net/q/196403/-const-vs-static-constDemonic
Voted to reopen. The "duplicate" is asking an entirely different question, specific to memory usage. This question asks a more general question about the difference.Hafner
I think you ask this question when you don't understand the meaning of static. So, this explanation might be helpful.Steere
@Steere please don't use geeksforgeeks for anything C or C++ language related. cppreference is a much more precise resource for C and C++.Manning
D
74

The difference is the linkage.

// At file scope
static const int a=5;  // internal linkage
const int i=5;         // external linkage

If the i object is not used outside the translation unit where it is defined, you should declare it with the static specifier.

This enables the compiler to (potentially) perform further optimizations and informs the reader that the object is not used outside its translation unit.

Diploid answered 1/11, 2012 at 21:32 Comment(5)
+1 It'd be great if you could also add what it means if those declarations are within a function.Stellate
Are you sure that const int i = 5; has external linkage?? In C++ it doesn't...Constanta
@KerrekSB at file-scope, yes. (C99, 6.2.2p5) "If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external."Diploid
@KerrekSB: C and C++ are not the same language. In particular, C const has nothing to do with C++ const.Subtropics
Do compilers really optimize based on static? Seems like it's not safe to assume that a static object is not used outside of its translation unit since it can still be passed out by non-static function or pointed to by a non-static global pointer.Bluepencil
W
148

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.

Windbound answered 1/11, 2012 at 21:32 Comment(1)
My upvote for a more simpler explanation. But you probably want to see @ouah’s explanation and how it could help advanced programmers and teachers.Eipper
D
74

The difference is the linkage.

// At file scope
static const int a=5;  // internal linkage
const int i=5;         // external linkage

If the i object is not used outside the translation unit where it is defined, you should declare it with the static specifier.

This enables the compiler to (potentially) perform further optimizations and informs the reader that the object is not used outside its translation unit.

Diploid answered 1/11, 2012 at 21:32 Comment(5)
+1 It'd be great if you could also add what it means if those declarations are within a function.Stellate
Are you sure that const int i = 5; has external linkage?? In C++ it doesn't...Constanta
@KerrekSB at file-scope, yes. (C99, 6.2.2p5) "If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external."Diploid
@KerrekSB: C and C++ are not the same language. In particular, C const has nothing to do with C++ const.Subtropics
Do compilers really optimize based on static? Seems like it's not safe to assume that a static object is not used outside of its translation unit since it can still be passed out by non-static function or pointed to by a non-static global pointer.Bluepencil
L
1

It depends on whether these definitions are inside of a function or not. The answer for the case outside a function is given by ouah, above. Inside of a function the effect is different, illustrated by the example below:

#include <stdlib.h>

void my_function() {
  const int foo = rand();         // Perfectly OK!
  static const int bar = rand();  // Compile time error.
}

If you want a local variable to be "really constant," you have to define it not just "const" but "static const".

Litigant answered 9/5, 2017 at 0:58 Comment(4)
It perfectly compiles for me... But I know it's stupid to have a static const variable in this case.Proliferation
@DrumM it's not stupid. In the case of foo the variable is re-initialised every time my_function() is called, resulting in a different random value being assigned. In the case of bar the variable initialised only once, the first time my_function() is called resulting in the same value being assigned for the lifetime of the program. Hence static storage duration.Zhdanov
Actually, on further reflection, it depends on if we're using C or C++. The question is tagged C, in which case we get a compile error for the assignment of bar due to rand() not being a compile-time constant.Zhdanov
@Litigant can you clarify what you mean by "the effect is different" and "really constant" with reference to storage specification and type qualification?Zhdanov
I
0
const int i=5;  

i value you can modify by using a pointer if i is defined and declared locally, if it is static const int a=5; or const int i=5; globally , you can not modify since it is stored in RO memory in Data Segment.

    #include <stdio.h>
   //const int  a=10;              /* can not modify */
   int main(void) {
   // your code goes here

   //static const int const a=10;   /* can not modify */
   const int  a=10; 
   int *const ptr=&a;
   *ptr=18;
   printf("The val a is %d",a);
   return 0;
} 
Individual answered 17/9, 2019 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.