This question already has answers here:
unexpected output in C (recursion)
(3 answers)
Why Output is 0000 and How? [duplicate]
(2 answers)
Closed 5 years ago.
#include <stdio.h>
int main() {
static int i = 5;
if (--i) {
main();
printf("%d ", i);
}
}
The output is
0 0 0 0
I wonder how printf is executed in this program .
Well, i is static meaning that is shared among all calls to main.
You never print anything before i gets down to 0 and since i is shared to all calls at the point of the print all calls see the same value: 0
i=5 if(4) main()
i=4 if(4) main()
i=3 if(2) main()
....
i=0 now. All other calls will see this value. Resursions stops
print 0 cause i is static
print 0 cause i is static
print 0 cause i is static
To see what I think is what you expect print before the recursive call as follows:
int main() {
static int i = 5;
if (--i) {
printf("%d ", i);
main();
}
}
You need to put printf("%d ",i); above the inner call to main().
You're on thin ice here:
An implicit return 0; is inserted at the end of main if missing.
You can call main from itself in C; in C++ the behaviour is undefined.
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
stumbled upon such a puzzle:
What will be shown on the screen?
#include <stdio.h>
void main()
{
int x = 10;
printf("x = %d, y = %d", x--, x++);
}
Curiously enough, but shown at the screen this: x = 11, y = 10;
But how??
Argument Evaluation Order is undefined in both C and C++. It's important to avoid any code that passes expressions dependent on each other that has to be evaluated before the function is called. It's a strict no.
int f1() { printf("F1") ; return 1;}
int f2() { printf("F2" ) ; return 1;}
printf("%d%d", f1(), f2()) ;
You can check out by adding several functions that contain a print statement and pass it to a function to observe this in action. You don't know what's coming, the C standard doesn't specify it, it depends on what compiler you use and how it optimizes your code.
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.
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
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 8 years ago.
#include<stdio.h>
int add(int,int);
main()
{
int a=2,b=3;
printf("%d %d %d",a,b,add(a,b));
getch();
}
int add(int a,int b)
{
int c;
c=a+b;
}
Ok fine this gives me output 2 3 5 ..But for below program
#include<stdio.h>
int add(int,int);
main()
{
int a=2,b=3;
printf("%d %d %d",a,b,add(a,b));
getch();
}
int add(int a,int b)
{
int c;
c=a+b;
c=0;
}
Still it is giving 2 3 5 as output.. as we have no return statement final statement c=0 not initializing .. it should give 2 3 0 but it is giving 2 3 5 only.
It's undefined behavior, anything could happen, you can't rely on it.
Probably what happened is, in the function add(), the value of c is calculated, and left in the stack, in the printf() call, what's in that particular address of stack is printed. Again, you can't rely on undefined behavior.
This is a very good question.
Inside the function add ()
the expression
c=a+b;
is evaluated
In this expression the right hand side has to be evaluated first.
So it returns the value of a+b and that value is stored in your return register and that value is finally stored in c.
In the next expression
c=0;
It is just initialising the value of 0 to c.
It does not need to return any value.
So the value of the return register is still 5.
This question already has answers here:
Values obtained in case of a recursive function
(3 answers)
Closed 9 years ago.
Shouldn't this program run infinitely because main is being called every time? And why it's output is 0 0 0 0? I know it's a noob question but I am not able to get it. What --i do and what is the effect of declaring i as static?
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
The static int is not reinitialized each time. Thus, each time main() is called, i is one lower.
when (--i) is zero, the recursive loop terminates.
Thus, it starts off with i=5, and calls a new copy. This one has i=4, which again calls a new copy. This continues until i=0, at which point the function just terminates. Control flow is then returned up the call stack, and each copy of main prints i, which is now 0. 4 copies means 4 zeroes.
The reason it does not run forever is because at some point i becomes 0 and no longer calls main. The trick is static which references the same memory location and hence is decremented each time main is recursively called.