Constant variable in C - c

I'm running a simple code to test my understanding, as of below.
The output that I got from this is actually 10.
I thought that the output should gave me a compile error, as "b" couldn't be add to x, as x is a const variable.
Can someone help to clear my understanding on this?
int aFunction(const int x){
return (x+10);
}
int main(){
int b =0;
b = aFunction(b);
printf("%d\n",b);
return 0;
}

This line
return (x+10);
Doesn't add 10 to x and return it. It forms the value that is x+10 and returns that but leaves x itself unchanged.
If you wrote
int c;
c=x+10;
Would you expect x to be increased by 10 after that second line executes?
Try
int aFunction(const int x){
int c=(x+10);
printf("aFunction: x=%d c=%d\n",x,c);
return c;
}

Basically, applying the const qualifier to a variable prevents the value of the variable from being modified.
In your function, aFunction(), the variable x is given a const qualifier which prevents the value of x from being modified in your function. There is no reason why your code shouldn't be able to compile since all the function is doing is returning a value which is 10 more than x. It is not in any way modifying the value of x.
If you did something like x += 10 which is short for x = x + 10, then you will get some compiler errors.

const int x only means that x itself cannot be changed inside the function. It has nothing to do with the return value.

Simple:
x is not changed until or unless you re-initialize it, look into these lines :
const int x : means X will never be changed.
x+10 : means just add 10 in X but never save the status.
x = x +10 " means overwrite the value of x which will Crash: and you will get desired Error

Related

Why different values inside called function and inside calling function?

The values which are output from inside the called function and from inside the calling function are not identical.
Why are values of y and value of m not the same?
screenshot of code and output, showing "Value of m 6" and "value of y 13"
#include<stdio.h>
int pass(a);
int main()
{
int x,y;
printf("Hello world\n");
printf("Enter the x ");
scanf("%d",&x);
y = pass(x);
printf("value of y %d",y);
return 9;
}
pass(m)
{
m = m + 5;
printf("Value of m %d\n",m);
// return 5;
}
Output:
Hello World
Enter the x 1
Value of m 6
value of y 13
Strictly, this should not even attempted to be explained, because of undefined behaviour, compare Reaching end of function without return statement
but ...
Assuming that my guess on the pattern of returned values (a constant plus the number of digits in "input + 5") is correct:
By chance, the default assumptions of the compiler when seeing the incomplete prototype int pass(a); (should be int pass(int a);), allow it to associate the later provided implementation of pass(m) (should be int pass(int m);, or more consistently int pass(int a)).
So when you call y = pass(x);, y gets the value returned by that implementation.
The implementation then lacks a clean return statement (it has one, but inactive by being a comment).
So at the end of executing that function, the most recently determined result is returned, another default of compilers which you should better not rely on for clarity and readability of code.
The most recent result is the return value of the call to printf().
That return value is the number of successfully printed characters.
You might want to read up on printf() and its return value in its specification, and about the concept of return values, prototypes and data types of parameters and return values in general.
For the output you show in your picture of text,
Value of m 6\n (thanks for making me type that...) that is, let me count
^ ^ ^ ^
1 5 10 13,
13, including the newline at the end of the output from inside the function.
Obviously, this is completely unrelated to the value of the local variable m, which is seen in the picture of text.
For more details on how to achieve what you might try to do see the comment David C. Rankin.

What is the different effect of assigning to vs. initialising static variables in a function?

#include <stdio.h>
int print1(void)
{
static int x= 10;
x+=5;
return x;
}
int print2(void)
{
static int x;
x = 10;
x+= 5;
return x;
}
int main()
{
int x;
x = print1();
x+= print1();
x+= print2();
x+= print2();
printf("%d", x);
return 0;
}
My solution:
First in print1 statement the value of the will increment by 5 , x will be 15 .
Then upon calling print1 function for the 2nd time it will be 15+5=20, since it will hold the previous value.
Now for the function print2, it will again initialise the x value with 10, then increment it by 5. Value of x will be 15.
for the last statement value of x will be 15+5=20.
x=15+20+15+20=70
But the answer is 65. Please correct me where did I go wrong? I even wrote it in my compiler, it is giving output 65, but if change the position of x inside Print2 statement like static int x=10, it is giving different output?
Why this is happening?
The init of a static variable like static int x=10; only happens once per program execution. And that is not like a statement being executed, it is during setup of variables for the program.
The assignment statement x = 10; is executed each time the function is executed.
This means the two code versions you discuss are different and especially have different effects on variable values; which in turn explains the different output.

Does adding values to a variable in printf() change the overall value of x?

int main() {
x=5;
printf("%d",x+3);
}
x could be either 5 or 8 after this example (I know that the output on the screen would be 8.)
The value at the address of x remains unchanged in this example. Inside the printf, we first get the value at the address of x and then add it to 3 and output it to the screen.
This is why we use statements like x=x+3 to change the value.
This kind of thing doesn't exist. What's best to do is to perform the operation outside of the printf() function:
#include <stdio.h>
int main() {
int x = 5;
x += 3; // x = x + 3
printf("%d\n", x);
}
According to Tomo Ceferin, x won't change because of the language's logic.
This works in C99. Not sure if any other version does.
#include <stdio.h>
int main() {
int x = 5;
printf("%d\n", x += 3);
}
Your program does not compile because there is no definition for x.
You should also include <stdio.h> to declare printf properly.
The statement printf("%d", x + 3); does produce 8 on standard output, but it does not modify the variable x, assuming x is defined as int x; somewhere.
You could modify x inside the call to printf: this has always been supported by the C language, although it is considered confusing.
#include <stdio.h>
int main() {
int x = 5;
printf("%d\n", x += 3); // prints 8 with a side effect
// x now has a value of 8
printf("%d\n", x); // prints 8 again
return 0;
}
The value of x remains the same, because you do not change it at all. Also, you should take into account to declare what type of data is your variable.
For example:
int main(){
int x = 5; //this is the variable x, declared as an integer. Its value is 5.
printf("%d", x + 3) //here you print an integer variable which has the value of (x+3),
//(5 + 3 = 8), to be more precise. (this instruction has nothing to
//do with the value of x, because you're not changing x in any way.)
}
So you will get on the screen the value of 8.
Hope it helped.

Why the Output is 15 15 instead of 15 20?

#include<stdio.h>
void print();
int main(){
print();
print();
return 0;
}
void print(){
static int x;
x=10;
x+=5;
printf("%d\t",x);
}
Output 15 15
You have code that says:
x = 10;
x = 15;
and that then prints x.
Then you call that function two times.
Why again do you think that the second print should result in a different outcome; compared to the first one?!
You see, you start by assigning: 10. Plus 5. Why should that ever result in 20?
Hint - try changing your code to:
static int x = 10;
x += 5;
Long story short: re-assigning is not the same thing as re-initializing!
I think you are getting confused with Reassigning and Re-intializing of static variable. You must have learnt somewhere that static variable dosn't re-initialize itself whenever the function gets called. Hence you are expecting answer 15 & 20.
But the thing is that you are re-assigning the value of x with 10 each and every time. If you modify your code like following then you can achieve what you've expected.
static int x=10;
x+=5;
Here, x gets initialized with 10 first time only. After that, for each and every function call, it will get incremented by 5 only.
Hope this will help and make your doubt clear.
Here static variable declared(initialized) as x, then value assigned every time 10 not initialize.
So, output of your program 15 and 15 displayed.
initialization and assignment both are different. please read this stack overflow question.
I hope this will help you and make your doubt clear.

Scope of return value of a function in C

int Mul (int x)
{
int y =0;
y = x *3;
return y;
}
int Main(void)
{
int var =0;
scanf ("%d", &var);
int result =0;
result = Mul(var);
printf ("%d", result);
}
Now my question is:
The variable y is created on the stack and when the function Mul returns, it gets cleared. Then how it is assigned to result?
result = Mul(var);
C considers the function "Mul" to have this value upon return, as though it were a variable. Note that Mul is declared as an int. Using "return" puts the value of y on the stack to be used in the assignment to "result". This value is popped from the stack which in effect clears it.
The statement
return y;
Does not return the value of y but a copy of the value of y. This copy is stored in the result variable. When you execute the code again with new values for x and y, the old values are overwritten.
You are returning the value of the variable (y) not the variable it self. Also variable "y" is local to mul function , so you cant return "y" as its scope is local to that function.
Whenever there is function call other than the void data type, the function call statement is replaced by the value that function is returning.
For eg.
In your case if
x=3
then
y= 3*3 evaluates to 9
thus
y=9
and
return y
simply means return 9 just like you return 0.
so the expression
result=mul(var) //is replaced by result=9.
I think this is enough of explanation

Resources