You may be looking for the declaration of additional blocks as in
#include<stdio.h>
int main() {
int i=1;
printf("before %d\n",i);
{
int i=0;
printf("within %d\n",i);
}
printf("after %d\n",i);
return(0);
}
which when executed yields
before 1
within 0
after 1
From the comment I grasp that a function invocation may be preferable which may be like
#include<stdio.h>
int div_10_count(long long val) {
int count=0;
while(val>0) {
val /= 10;
count++;
}
return(count);
}
int main() {
long long val=10000000000L;
printf("Before: val=%lld\n",val);
printf("Returned: %d\n",div_10_count(val));
printf("After: val=%lld\n",val);
return(0);
}
shows
Before: val=10000000000
Returned: 11
After: val=10000000000
so the variable val remains unchanged even though the function works with it. This would be different when passing the parameter by reference, i.e. as indicated with the “&”.
2
solved C – Is there a way to leave a variable unchanged outside of the loop?