This question already has answers here:
Main calling main [duplicate]
(2 answers)
Why Output is 0000 and How? [duplicate]
(2 answers)
How does this program execute? [duplicate]
(5 answers)
understanding static int execution [duplicate]
(2 answers)
unexpected output in C (recursion)
(3 answers)
Closed 3 years ago.
why is the output contains 4 time 0's.The main call again and again until if condition become false and then it should be exit from if block.
#include <stdio.h>
int main()
{
static int i=5;
if(--i)
{
main();
printf("%d ",i);
}
}
Note the following.
the int i is static.
you are calling main reursively.
In the if condition you have a pre decrement of i
Every time you call main, the value of i will be the same as the previous call. So i will decrement each time. Since this is --i it will be 4 the first time and go to 0.
After the innermost main function returns(i==0), the printf of the main before that will be executed.
But i is static and has a value of 0. So you get 4 zeros printed for each of the main functions.
Related
This question already has answers here:
Function returning address of local variable error in C
(3 answers)
returning a local variable from function in C [duplicate]
(4 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 4 months ago.
I know, there are multiple questions about this on StackOverflow but I couldn't find a fitting solution for my problem.
I want to generate product codes (e.g. 12a, important: numbers and letters) and return them and printf. But when I do it in my main I get no result. When I printf("%s\n", pcode) in my generator function it works, but after return pcode I won't get the result.
char* generate(int num) {
char pcode[3];
... // generating code
printf("%s\n", pcode); // correct output
return pcode;
}
int main(void) {
printf("%s\n", generate(12)); // wrong output
}
This question already has answers here:
Printing pointer to integer causes segmentation fault. Why?
(7 answers)
What is a segmentation fault?
(17 answers)
What is the meaning of "wild pointer" in C?
(11 answers)
Closed 2 years ago.
I am trying to follow this basic program involving pointer into the memory.
At first We define counter to be 0 (outside main) then we make p_int to point at the same address as a counter.
But when i go into the loop for some reason it compares the register with 21 instead of 2.
after that when i have tried to change the adress and value of the pointer to a tottaly different vaue and address, it exits in an error,although it compiled well.
Whele did i go wrong?
Thanks.
int counter=0;
int main()
{
int *p_int;
p_int=&counter;
while (*p_int <2)
{
(*p_int)++;
}
p_int=(int*)0x20000002U;
*p_int=0xDEADBEEF;
return 0;
}
enter image description here
enter image description here
enter image description here
enter image description here
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
#include <stdio.h>
int main ()
{
int a=10;
printf("%d %d %d",a,a++,a);
return 0;
}
The output I am getting is "11 10 11".
I thought the output would be "10 10 11".
why a is incrementing like this?
Because there is no guarantee about the order in which a C compiler evaluates the arguments. The only thing guaranteed (by the standard) is that they are all evaluated before doing the call. Therefore, you should never count on the order of evaluation of the arguments. Just consider it as random.
Hence, in general, avoid using auto-increment if the same variable exists more than once in an argument list.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
I wrote the following simple program
#include<stdio.h>
int main()
{
int i;
i=1;
printf("%d %d %d",i,i++,++i);
return 0;
}
The above program gave 3 2 3 as output which I am not able to interpret the output. I am using gcc-4.8.1
You have undefined behavior here!!
When there are multiple increments to the same variable in the printf() you can't predict the output.
The order of execution within the printf() is not defined.
This question already has answers here:
multiple assignment statements in printf in c [duplicate]
(2 answers)
Closed 8 years ago.
Suppose i have the following code:
#include <stdio.h>
main()
{
int a,b,c;
b=1;
c=2;
printf("%d\n",10,b=20,b=30,c=50,c=100);
printf("%d\n",b);
printf("%d\n",c);
}
o/p-10,20,50
how did the value of b became 20,not 30 ..and also the same for c?
The order of evaluation of argument expressions and their pushing on the stack are different things.
The order of evaluation of argument expressions are unspecified in C. So it might be that at first b = 20 will be evaluated and then b = 30 or vice versa.
The order of placing arguments in the stack is the following: the right most argument is placed first.