Why?
-
int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:
---------------------------- 286? WOWW!:-O
I've never seen such code before and don't know if it even compiles. But it uses the conditional operator ( ?: ) in the return statement and continues to call the main method until i has been decremented below 0, and since the main method prints the value of i and the decrements it, the effect is to print 321. Very bizarre code.
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn Rand -
int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:
---------------------------- 286? WOWW!:-O
-
int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:
---------------------------- 286? WOWW!:-O
_8086 wrote:
Does this print 321??
Yes. Remember
i
isstatic
.Prasad Notifier using ATL | Operator new[],delete[][^]
-
int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:
---------------------------- 286? WOWW!:-O
first of all, when posting code, please use the
<pre></pre>
tags to keep a well formated message. then, to understand your result, keep in mind that static variables (declared within a function) stay local to the function (so, not visible from outside) but their lifetime changed to being the program lifetime. that is, a local static variable is initialized at the beginning of the application, and is destroyed when the program exits. at the first call of the function, the variable as the initialization value. then, each time the function is called, it keeps the last value it had when the function last exited.int main() {
static int i=3;
printf("%d", i--);
return i>0 ? main():0;
}1. entering main for the first time:
i == 3
so,3 is printed
(because the i-- decremented the variable only after returning the current value) i now equals 2 thei>0? main : 0
expression is evaluated. asi
is strictly higher than 0,main()
calls itself recursively 2. entering main for the second time:i == 2
printing 2i == 1
(after decrementation)i > 0
, so re-enteringmain()
3. entering main for the third time:i == 1
printing 1i == 0
(after decrementation)i not > 0
, so returning0
for all the previous calls to main, the expression returns0
either, until exiting...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:
---------------------------- 286? WOWW!:-O
-
Homework? There's two things you need to understand here, what
static
does to that variable declaration, and what thetest ? then : else;
operation means... -
first of all, when posting code, please use the
<pre></pre>
tags to keep a well formated message. then, to understand your result, keep in mind that static variables (declared within a function) stay local to the function (so, not visible from outside) but their lifetime changed to being the program lifetime. that is, a local static variable is initialized at the beginning of the application, and is destroyed when the program exits. at the first call of the function, the variable as the initialization value. then, each time the function is called, it keeps the last value it had when the function last exited.int main() {
static int i=3;
printf("%d", i--);
return i>0 ? main():0;
}1. entering main for the first time:
i == 3
so,3 is printed
(because the i-- decremented the variable only after returning the current value) i now equals 2 thei>0? main : 0
expression is evaluated. asi
is strictly higher than 0,main()
calls itself recursively 2. entering main for the second time:i == 2
printing 2i == 1
(after decrementation)i > 0
, so re-enteringmain()
3. entering main for the third time:i == 1
printing 1i == 0
(after decrementation)i not > 0
, so returning0
for all the previous calls to main, the expression returns0
either, until exiting...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
You are hot today!:-D
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir? **Dr.Brad :**Well,I just replied to everything Graus did and then argued with Negus for a bit.
VuNic wrote:
You are hot today!
:cool:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]