Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how does this code work??
C++
int main()
{
  static int n=1;

  printf("%d ", n) && (n++<100) && main();

  return 0;
}
Posted
Updated 1-Sep-12 7:47am
v3
Comments
Kenneth Haugland 1-Sep-12 13:50pm    
Did you compile it and run debug, breakepoints etc? And if so what did ytou find out? I have an idea what it does but it looks like a spagetti code if Im right....
[no name] 25-Sep-12 11:27am    
but it does work! im a newbie sorry if ive abused d language...plz correct me
[no name] 1-Sep-12 13:51pm    
When you stepped through the code using a debugger, what did you observe?
Kenneth Haugland 1-Sep-12 14:00pm    
He should put a breakepoint on one of these comments too :laugh:
pasztorpisti 1-Sep-12 16:03pm    
btw, what is breakpoint? :-)

It does not use loop, because it is using recursion. And as C is using short circuit evaluations by default, the second condition will stop the recursion. It is using a static variable to pass the current iteration between the recursive calls. This later approach is even better in this case - than using a parameter -, since it is stored only once in the stack - the parameter would be stored as many times the recursion is called. The static variable is initialized only at the first declaration, will not be reset to one on every call, thus can store the postfix incremented value embedded in the second condition.
 
Share this answer
 
v2
Comments
Philip Stuyck 1-Sep-12 14:42pm    
+5 good explanation. recursion, static variable, alle key points are there
Zoltán Zörgő 1-Sep-12 14:44pm    
Thank you.
ridoy 1-Sep-12 15:13pm    
hesitated whether i will give a solution or read your solution!!..but surely a broader explanation than me and also nice..so accept it and +5 from me:)
Zoltán Zörgő 1-Sep-12 15:16pm    
Thank you too.
pasztorpisti 1-Sep-12 15:29pm    
Just wanted to correct this: "since it is stored only once in the stack"
A static variable isn't stored on the stack, its a global variable with special initialization/finalization code. Looking at the question I suspect the OP doesn't even know what the stack is in this context. :-)
Two things that are not mentioned in the other solutions but might be of interest to the beginner:

  1. It works since printf(...) returns a non-0 value (the number of printed characters) and since any value not equal to 0 is interpreted as true in C.
  2. The only purpose of the whole x && y & z is to evaluate one after the other: first x, if x evaluates to true, do y, if y evaluates to true, do z. The result is not stored, and therefore, it does not matter that the tail call of main() returns 0 = false - it does the recursion internally before it can return the 0 value.


Cheers
Andi
 
Share this answer
 
Because of the short-circuit evaluation[^] your code is equivalent with this:
C++
int n=1; // in case of non-pod "initializable" types this
         // isn't equivalent with declaration inside a function
         // even if we overlook the difference in accessiblity
int main()
{
  if (printf("%d ", n))
  {
    if (n++ < 100)
    {
      main();
    }
  }
 
  return 0;
}


EDIT: Recursion mentioned by Zoltán is also very important if you want to understand this: http://en.wikipedia.org/wiki/Recursion_%28computer_science%29[^].
An important stuff (in my opinion) to study if you don't know what the stack is: http://en.wikipedia.org/wiki/Call_stack[^].
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900