Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why?

Why?

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _8086
    wrote on last edited by
    #1

    int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:

    ---------------------------- 286? WOWW!:-O

    R B P T C 5 Replies Last reply
    0
    • _ _8086

      int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:

      ---------------------------- 286? WOWW!:-O

      R Offline
      R Offline
      Rohde
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • _ _8086

        int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:

        ---------------------------- 286? WOWW!:-O

        B Offline
        B Offline
        benjymous
        wrote on last edited by
        #3

        Homework? There's two things you need to understand here, what static does to that variable declaration, and what the test ? then : else; operation means...

        _ 1 Reply Last reply
        0
        • _ _8086

          int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:

          ---------------------------- 286? WOWW!:-O

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          _8086 wrote:

          Does this print 321??

          Yes. Remember i is static.

          Prasad Notifier using ATL | Operator new[],delete[][^]

          1 Reply Last reply
          0
          • _ _8086

            int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:

            ---------------------------- 286? WOWW!:-O

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            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 the i>0? main : 0 expression is evaluated. as i is strictly higher than 0, main() calls itself recursively 2. entering main for the second time: i == 2 printing 2 i == 1 (after decrementation) i > 0, so re-entering main() 3. entering main for the third time: i == 1 printing 1 i == 0 (after decrementation) i not > 0, so returning 0 for all the previous calls to main, the expression returns 0 either, until exiting...


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            E 1 Reply Last reply
            0
            • _ _8086

              int main() { static int i=3; printf("%d",i--); return i>0 ? main():0; } Does this print 321??:confused:

              ---------------------------- 286? WOWW!:-O

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Because your compiler works fine... :):-D:)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              1 Reply Last reply
              0
              • B benjymous

                Homework? There's two things you need to understand here, what static does to that variable declaration, and what the test ? then : else; operation means...

                _ Offline
                _ Offline
                _8086
                wrote on last edited by
                #7

                benjymous wrote:

                Homework?

                No it's a puzzle posted in our intranet.

                ---------------------------- 286? WOWW!:-O

                1 Reply Last reply
                0
                • T toxcct

                  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 the i>0? main : 0 expression is evaluated. as i is strictly higher than 0, main() calls itself recursively 2. entering main for the second time: i == 2 printing 2 i == 1 (after decrementation) i > 0, so re-entering main() 3. entering main for the third time: i == 1 printing 1 i == 0 (after decrementation) i not > 0, so returning 0 for all the previous calls to main, the expression returns 0 either, until exiting...


                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  E Offline
                  E Offline
                  Eytukan
                  wrote on last edited by
                  #8

                  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.

                  T 1 Reply Last reply
                  0
                  • E Eytukan

                    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.

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    VuNic wrote:

                    You are hot today!

                    :cool:


                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups