0

I am new to C++ and have come across the const keyword. I looked it up online and read that The main use is to prevent the changing the value throughout the program. I have these two snippets below

const int size = 56;

Compared to using

int size = 56;

Why should I use one over the other? What is the idea behind using it.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 3
    I am confused about your question. You have answered your last sentence in your second sentence. – GSerg Aug 22 '20 at 12:43
  • 2
    Possible duplicate: [Use const wherever possible in C++?](https://stackoverflow.com/questions/22285773/use-const-wherever-possible-in-c) – rustyx Aug 22 '20 at 12:50
  • After defining and initialising size, do an assignment (in a function that has visibility of the definition of the variable) like `size = 42`. You will see that the compiler complains bitterly in one case but not the other. Now, consider cases where you would want the compiler to prevent you from changing the value of `size` - or, if it was allowed, your program could malfunction. – Peter Aug 22 '20 at 12:50
  • Once you are comfortable with using const variables, look into const member functions and the broader concept of const correctness to get an idea of why this is extremely powerful. – Roshan Aug 22 '20 at 12:53
  • If the variable needs to be mutable, then do not make it `const`. If the variable is not supposed to change after being initialized, then consider making it `const`. – Eljay Aug 22 '20 at 12:57
  • https://isocpp.org/wiki/faq/const-correctness – Jesper Juhl Aug 22 '20 at 13:12
  • @Aryan Parekh, welcome to stackoverflow. Please be aware that you should try to figure out the answer to your question before you post it. This question is easily answered by googling, example search: why use const c++ – Elliott Aug 22 '20 at 13:23

3 Answers3

5

Yes, you should make all variables const if you are never going to change its value after initialization. This prevents bugs where you accidentally change a value you're not supposed to. You seem to be aware of this already.

In addition, there is something that you might not be aware of, which is that making an int variable const also makes it a constant-expression, so long as the initializer itself is also a constant-expression, e.g. the int literal 56. This allows you to use it in contexts where you need a constant-expression, e.g. as the dimension of a static array:

const int size = 56;
int a[size]; // ok
int size = 56;
int a[size]; // error
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Your second point is only true when the const variable is known at compile time though. For example, `int main(int argc, char* argv[]) { const int size = argc; int a[size]; }` is a compile time error (unless the compiler supports dynamical-sized arrays) – Ted Klein Bergman Aug 22 '20 at 13:19
  • @TedKleinBergman True, clarified that the initialializer must be a constant-expression as well. – cigien Aug 22 '20 at 13:21
2

In c++ CONST is used when you want to declare something which will not change its value throughout the program. But if you accidentally try to rewrite its value, the compiler will through an error. Most of the time it is recommended to declare a variable of int type so you can redefine its value when it's required. CONST is used when you want to declare a static array i.e.

const int a = 100;
int sampleAray[a];

And int is used when you, the value of the variable will be modified at some point i.e.

int a = 12;

int arr[4] = {11,22,33,554};

for (int i=0; i<4; i++){
    if(arr[i]%2 == 0){
         a+=arr[i];
     }
}

1

When you have a const variable holding something that should not change and accidentally write code that modifies the variable anyway, you get a nice compiler error and can correct your mistake easily. If the variable is non-const, the compiler cannot help you and you now have a bug that may be hard to find.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70