Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
how does this code work??
int main()
{
  static int n=1;
 
  printf("%d ", n) && (n++<100) && main();
 
  return 0;
}
Posted 1-Sep-12 7:42am
Edited 1-Sep-12 7:47am
v3
Comments
Kenneth Haugland at 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....
Akhilesh Halageri at 25-Sep-12 11:27am
   
but it does work! im a newbie sorry if ive abused d language...plz correct me
Wes Aday at 1-Sep-12 13:51pm
   
When you stepped through the code using a debugger, what did you observe?
Kenneth Haugland at 1-Sep-12 14:00pm
   
He should put a breakepoint on one of these comments too :laugh:
pasztorpisti at 1-Sep-12 16:03pm
   
btw, what is breakpoint? :-)
Zoltán Zörgő at 1-Sep-12 14:07pm
   
This is a C easter egg: recursion on main. But I think, this is the big classical one: http://research.microsoft.com/en-us/um/people/tball/papers/xmasgift/
pasztorpisti at 1-Sep-12 16:10pm
   
Thats what I call abuse! :-) I've seen such code only in perl with regex magic.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Philip Stuyck at 1-Sep-12 14:42pm
   
+5 good explanation. recursion, static variable, alle key points are there
Zoltán Zörgő at 1-Sep-12 14:44pm
   
Thank you.
ridoy at 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ő at 1-Sep-12 15:16pm
   
Thank you too.
pasztorpisti at 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. :-)
Zoltán Zörgő at 1-Sep-12 15:31pm
   
You'r right. In both. :)
Joan Murt at 1-Sep-12 15:56pm
   
+5, good explanation.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Because of the short-circuit evaluation[^] your code is equivalent with this:
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[^].
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Nirav Prabtani 389
2 Abhinav S 210
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,580
1 Sergey Alexandrovich Kryukov 6,806
2 Maciej Los 3,919
3 Peter Leow 3,693
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 1 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100