Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Which is the advantage for the user of const in parameters of functions that are not pointers?

Tags:

c

constants

I would like to know if there is any advantage for the user when calling a function like

A) void func(int i);

or a function like

B) void func(const int i);

As inside the func the parameter i will be copied anyway to the stack (or to wherever the compiler chose in its optimizations), for the user who is calling this function there is no difference between A and B.

So if a implement something like :

A)

void func(int i)
{
    i = another_func(i);
    printf("%d", i);
}

Or using the const

B)

void func(const int i)
{
    int j = another_func(i);
    printf("%d", j);
}

My Question :

There is any advantage of implementation B ? The compiler can perform any kind of optimization ? A and B are just a simple example, these questions are valid for other situations.

I understand the advantage of using const in pointers (e.g const void *data), because we are telling the user that we are not going to modify its contents, but I do not understand the usage of const other than warn the programmer of the function that he is not supposed to modify it, but in my point of view using const in a API header file is useless.

Thanks for your help.

like image 446
Lilás Avatar asked Jun 16 '13 12:06

Lilás


People also ask

What is the one benefit of declaring the parameter as const?

Answer: The const qualifier Forbids the code to modify the argument, so the programmer can rest assured that the source object will remain unchanged.

What is the advantage of using constant in C?

Constants can make your program more readable. For example, you can declare: Const PI = 3.141592654. Then, within the body of your program, you can make calculations that have something to do with a circle. Constants can make your program more readable.

What is the benefit of const?

What is the benefit of const keyword in programming? Specifying a variable as const states that the variable's value should never change after the initial assignment. This allows the compiler to perform additional tests at compilation (validating your code).

Why do we use const in front of a formal parameter in a function header?

Always use const on function parameters passed by reference or pointer when their contents (what they point to) are intended NOT to be changed. This way, it becomes obvious when a variable passed by reference or pointer IS expected to be changed, because it will lack const .


4 Answers

Some people here say that you should write const if you won't change the variable - as you can declare any local variable const. (The main goal is to keep you from accidently changing the variable, but also may help the compiler to optimize your code)

Other people say that you shouldn't declare function parameter const, because it will expose some of the implementation in the API.

I think that they both are right! That is why you can omit the const in function declaration: You can write void func(int); in a header, but implement it void func(const int i) {} in the code file.

like image 116
asaelr Avatar answered Sep 28 '22 02:09

asaelr


It is a warning to the programmer, yes. But it is more than just a warning though. The compiler enforces const-ness by making sure that the programmer does not modify the value inadvertently. In that, it's a way to encode API and algorithm design information. And since the const-ness is assured across nested function calls, it is preserved across the entire call chain. Note that at some point in the call chain, someone may use a pointer to the const value too.

The compiler also uses the const-ness for optimizations. Here's a good answer.

like image 43
Ziffusion Avatar answered Sep 28 '22 01:09

Ziffusion


There is at least one reason why not to use const in non-pointer type parameters for external functions. With const your are exposing internals of the API which should be irrelevant for the API user.

The use of const can generally helps for optimizations but its main usage is to prevent errors and document the API.

like image 36
ouah Avatar answered Sep 28 '22 01:09

ouah


Declaring it const also prevents you, or other developers working on that same code, from modifying it when you're writing your function. It's a defensive programming technique that makes it clear that that parameter is not supposed to be modified, perhaps when you return to the code six months later, prevents you from employing arguably bad habits by lazily using an input parameter for other purposes within your function, helps prevent nasty errors which can arise from inadvertently confusing == and =, and so on.

like image 35
Crowman Avatar answered Sep 28 '22 02:09

Crowman