Reputation: 1
I have one doubt. Please see the below C program
int main(void){
unsigned int consttest_var = 1;
unsigned int i;
for(i = 0; i<10; i++){
consttest_var++;
consttest_func(consttest_var);
}
return 0;
}
void consttest_func(const unsigned int consttest_var1){
printf("\n%d", consttest_var1);
}
I tried the above code and I got value for consttest_var1
as 2,3,4....10. Why consttest_var1
should print the value when it was declared as const. I was expecting it will throwing error as read only. Can anyone explain this?
Upvotes: 0
Views: 2845
Reputation: 11
The main reason is that each time your function is called, a new value is passed to it. And in your program it doesn't matter whether your variable 'consttest_var1' is constant or not, as your function is just displaying the value received. And a constant variable is a variable whose value cannot be modified once it is initialized. Follow the comments in the program...
int main(void)
{
unsigned int consttest_var = 0;
unsigned int i;
for(i = 0; i<10; i++){
consttest_var++;
consttest_func(consttest_var);
}
return 0;
}
void consttest_func(const unsigned int consttest_var1)
{
consttest_var1=3; //suppose if u write this line.
/* then this line will throw an error that u were talking about
i.e. assignment of read only parameter
as u are modifying the value of a constant variable */
printf("\n%d", consttest_var1);
}
Upvotes: 0
Reputation: 403
The const variable consttest_var1
is declared locally for the function consttest_func. Once the function ends, consttest_var1
goes out of scope.
When you call the function again, a new space in the stack is allocated for a new const consttest_var1
which after initialize will not be changed.
Upvotes: 0
Reputation: 12817
unsigned int consttest_var = 1;
here you declared it as non-const.
When you send it toconsttest_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
To sum things up - your functoin expects a const variable, and does not modify it - as it should
see this for further reading about const
Note: declaring a variable as const does not mean you can't modify (as argument) it elsewhere. for example ,this code will compile, even though I get a const and increment it, because although a is defined as const
what the function f gets is a copy of it, and the copy is modifiable.
#include <stdio.h>
int f(int a){
a++;
return a;
}
int main(){
const int a = 1;
printf("\n%d",f(a));
return 0;
}
Upvotes: 1
Reputation: 17668
I was expecting it will throwing error as read only.
void consttest_func(const unsigned int consttest_var1){
printf("\n%d", consttest_var1);
}
Your consttest_func
never really modifies parameter consttest_var1
, so no it won't throw any error as read only.
If you actually modify the parameter inside consttest_func
, such as with statement like consttest_var1++;
then it will throw read-only parameter
as you would expect.
Upvotes: 1