Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

program compiled when function parameter does not match the const parameter declaration in c

I have a piece of code like this:

#include <stdio.h>

int add(const int x, const int y);

int main()
{
    printf("%d", add(9, 8));

    return 0;
}

int add(int x, int y)
{
    return x + y;
}

I forward declared the function "add" with const parameters after that i defined it without the const parameter, and when i compile it the compiler gives no complain.
The out put of the program is: 17. Why does this happen ?

like image 672
Kain Avatar asked Dec 15 '21 19:12

Kain


People also ask

Should function parameters be const?

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 .

How is a parameter declared?

Parameter types In a function declaration, or prototype, the type of each parameter must be specified. In the function definition, the type of each parameter must also be specified. In the function definition, if the type of a parameter is not specified, it is assumed to be int .

What is a const parameter?

A constant parameter, declared by the keyword const , is a read-only parameter. This means that we can not modify the value of the constant parameter in the function body. Using the const keyword tells the compiler that the value of that parameter will not be changed inside the function.

What a parameter declaration in c++?

The function declarator includes the list of parameters that can be passed to the function when it is called by another function, or by itself. In C++, the parameter list of a function is referred to as its signature. The name and signature of a function uniquely identify it.

How to avoid this problem when declaring a const parameter?

This problem can be avoided by declaring n as a const parameter as shown below (the function body is omitted to save space). Now the compiler reports an error whenever the value of n is modified in the function body. Note that we can pass either a const or a non-const variable as an argument where a const parameter is expected.

Is it possible to change the argument of the consttest_func function?

When you send it to consttest_func (consttest_var), the function expects const unsigned int as declared: void consttest_func (const unsigned int consttest_var1) the function itself is not allowed to change the argument, because it is const, but outside of the function's scope, the variable isn't const, hence, can be modified

How can a function not modify the value of a parameter?

Sometimes we may want that a function should not modify the value of a parameter passed to it, either directly within that function or indirectly in some other function called form it. This can be achieved using const parameters. Consider, for example, the function given below to calculate the sum of the first n integer numbers.

Can We pass a non-const variable as an argument to a const?

Note that we can pass either a const or a non-const variable as an argument where a const parameter is expected. However, the compiler gives a warning when we pass a const variable where a non-const parameter is expected.


Video Answer


1 Answers

Well, the function declaration and function definition have to be compatible, we all know that. So the same name and same return type and type of parameters. So we know from C11 6.7.6.3p15:

For two function types to be compatible, [...] corresponding parameters shall have compatible types. [...]

But, there is an explicit backdoor, later in that text:

(In the determination of type compatibility [...] each parameter declared with qualified type is taken as having the unqualified version of its declared type.)

The type-qualifier is for example const. And it is ignored. You can put any type-qualifier and it is just ignored when checking if the function declarations are the same with each other.

int func(int n);
int func(volatile int n);
int func(const int n);
int func(const volatile int n);
int func(int n) {
    return n;
}
like image 119
KamilCuk Avatar answered Oct 30 '22 21:10

KamilCuk