Explain the output? [duplicate] - c

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
#include<stdio.h>
int main()
{
int a = 10;
printf("%d %d %d",++a,a++,a--);
return 0;
}
I edited the code a bit..now the outputs are : 11 9 10
It's more complex now..

It's up to the compiler in which order he evaluates the parameters of a function call.
If the compiler goes from left to right (that would explain your output):
a is 10
prä-increment which means a is incremented (the value 11 is passed as parameter)
post-decrement which means a is decremented later (the the value 11 is passed as parameter)
post-increment which means a is incremented later (the value 10 is passed as parameter)
But if I compile this e.g. with another compiler I could get different output.

Rewriting it as follows may make it easier to understand:
NOTE: I have made the assumption that the compiler will produce code to evaluate the parameters from left to right! This may be compiler specific.
#include<stdio.h>
int main()
{
int a = 10;
int param2, param3, param4;
param2 = ++a; // increments a to 11 and evaluates to 11
param3 = a--; // evaluates to current value of a then decrements a (11)
param4 = a++; // evaluates to current value of a then increments a (10)
printf("%d %d %d",param2,param3,param4);
return 0;
}

The place of increment(++) and decrement(--) operator is very important. So in case of ++a the value is incremented from 10 to 11 and then printed, for a-- the current value is printed i.e. 10 and then the a is incremented to 11. Similarly in last case a++ current value 11 is printed and it is incremented to 12.

Related

Assignment operator works fine as a condition in if Statement in C programs [duplicate]

This question already has answers here:
How the assignment statement in an if statement serves as a condition?
(4 answers)
Closed 2 years ago.
I am aware that when a non zero value is provided as a condition in an if statement, the condition is evaluated to be true.
Yet, I'm wondering how an assignment(=) is evaluated to be true here, instead of a comparison(==), and runs without any errors in the below C program.
int main(){
int a, b =5;
if(a=10) {
("This works");
}
if(b=1){
("This works too");
} }
The assignment operator always returns the value that was assigned. So in your case a=10 has the value 10 and the if statement is essentially if(10). Which is true.
a = 10 is an expression that assigns 10 to a and whose value is the result of the assignment (that is 10).
any value different from 0 is considered to be true
Try this:
if (b = 0) {
("This is never displayed");
}

--a vs a--, operator precedence [duplicate]

This question already has answers here:
What is the difference between ++i and i++?
(20 answers)
Closed 5 years ago.
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
void recursion (int a) {
if (a != 0) {
recursion(--a); //works
recursion(a--); //does not work
printf("%d\n", a);
}
}
int main (int argc, char *argv[]) {
printf("start\n");
recursion(10);
printf("finished\n");
return 0;
}
Why is there a segmentation fault when I recurse (a--) but works fine when I recurse (--a)?
I don't think recursion(a--) is wrong due to undefined behavior because there is only one side effect, which is to decrease a by 1. This side effect is exactly what I wanted. Thanks.
Both --a and a-- have the side effect of incrementing a. The difference is that the value of the expression --a is the value of a after decrementing, while the value of a-- is the value of a before decrementing.
So in the latter case the same value of a is passed recursively to the function. As a result, you have an infinite recursive loop which causes a stack overflow.
You need to use recursion(--a) for the recursive call in order for the decremented value of a to be passed to the function.

C - Output explanation of printf("%d %d\n",k=1,k=3); [duplicate]

This question already has answers here:
Explain the order of evaluation in printf [duplicate]
(5 answers)
Closed 6 years ago.
How to explain the output of the below code:
include <stdio.h>
int main(void) {
int k;
printf("%d %d\n",k=1,k=3);
return 0;
}
Ideone Link
My thinking was that 1 will be assigned to k variable and then 1 would be printed. Similarly 3 will be assigned to k and output will be 3.
Expected Output
1 3
Actual Output
1 1
I am extrapolating from
int a;
if (a = 3) {
...
}
is equal to
if (3) {
...
}
Please let me know where am I going wrong?
The problem is, the order of evaluation of function arguments are not defined, and there's no sequence point between the evaluation or arguments. So, this statement
printf("%d %d\n",k=1,k=3)
invokes undefined behavior, as you're trying to modify the same variable more than once without a sequence point in between.
Once a program invoking UB is run and (if) there's an output, it cannot be justified anyway, the output can be anything.
I expect the reason you're seeing 1 1 is because the two assignment statements are happening control is passed to printf.
printf("%d %d\n",k=1,k=3);
So in response to the down-votes, yes, this this is undefined behavior, and therefore you shouldn't count on this behavior continuing. However, in terms of identifying why the output was 1 1 and not 1 3, we could infer that the assignment of 3 may have been stomped on by a subsequent assignment of 1.
When printf is invoked, the call stack contains two entries containing the final value of k, which is 1.
You can test this out by replacing those with a function that prints something when it executes.
Sample Code:
#include <stdio.h>
int test(int n) {
printf("Test(%d)\n", n);
return n;
}
int main(void) {
int k;
printf("%d %d\n",k=test(1), k=test(3));
return 0;
}
Output:
Test(3)
Test(1)
1 1

Getting strange result while using increment operator(++) in printf fuction [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
I am executing this c program on gcc compiler and getting strange results.
So how is it possible
code:
#include<stdio.h>
int main()
{
int i;
i =10;
printf(" %d %d %d ",i++,i++,i); //output : 11 10 12
return 0;
}
as per me result should be 10 11 12 but I am getting 11 10 12.
How is it possible?
In C++, the order of evaluation of function arguments is undefined so if you use the increment operator multiple times in the argument to a particular function there is no 'correct' answer, they may be evaluated in any arbitrary order.
Please familiarize yourself with the concept of Sequence points. Only at such defined sequence points is guaranteed that all side effects of previous evaluations are performed. There are no sequence points between the list of arguments of a function. So, it leads to undefined behavior.

Decrementing an int while passing multiple copies in the same function call [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Sequence points when calling functions in C and undefined/unspecified behaviour
(2 answers)
Closed 9 years ago.
I am decrementing an integer while passing it to a function. But am seeing some unexpected behavior. I'm new to C, and not sure what to make of it:
#include <stdio.h>
void func(int, int);
int main(){
int i=3;
//func(i--, i); //Prints a=3, b=2
func(i, i--); //Prints a=2, b=3 ??
func(i, --i); //Prints a=2, b=2 ??
}
void func(int a, int b){
printf("a=%d\n", a);
printf("b=%d\n", b);
}
The first call to func works as expected, but what is the deal with the second and third calls?
The order of calculation of function arguments is not specified. E.g. GCC likes to calculate the argument values from right to left. When you have an operator that modifies a variable (or have any other side effect), there must be a sequence point between that operator and any other expression that uses that variable.
This is undefined bahavior, you can't expect either result.
Since parameters passes in a function from right, so in first call where you used post increment operator- it passes the same value (3) and update the value for 'i' left parameter (2).
while in second call, you used pre increment operator which changes itself for right parameter and sends updated values for left parameter.

Resources