-5

i dont understand why those two,have different output

there are two simple c programms

int main()
{
    int i;
    for (i = 0;i<3; i++)
            {
              switch (i++)
              {
               case 0:
               {
                 printf("zero");
               }
               case 1:
               {
                printf("one");
               }

               case 2:
               {
               printf("two");
               break;
               }

              default:
               {               
                printf("end");
               }                
        }
     }
}

this give this output:zero one two two

in this case after switch when the value from variable i change 0 to 1?

int main()
{
    int i;
    for (i = 0;i<3; i++)
     {
         switch (++i)
         {
            case 0:
            {
                printf("zero");
            }
            case 1:
            {
                printf("one");
            }

            case 2:
            {
                printf("two");
                break;
            }

            default:
            {             
                printf("end");
            }   

        }
     }
}  

this give this output:one two end

Anastasis
  • 192
  • 1
  • 2
  • 12
  • 5
    Grab paper and pencil. Even better, a debugger. Use your 1. C book, 2. brain. –  Jun 21 '13 at 21:49
  • `i++` returns `i` before `1` is added. `++i` returns `i` after `1` is added. I'm prettu sure I've seen this question more than once on SO and google – tay10r Jun 21 '13 at 21:50
  • Try printing out `i` at the beginning and end of each loop, and read up on the difference between `i++` and `++i` (plenty of postings on this site). – Reinstate Monica -- notmaynard Jun 21 '13 at 21:51
  • [duplicate](http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i) – tay10r Jun 21 '13 at 21:54

4 Answers4

4
switch (i++)
     {
     }

is the same as:

int j=i;
i++;
switch (j)
    {
    }

while

switch (++i)
     {
     }

is the same as:

i++;
switch (i)
    {
    }
Grady Player
  • 14,399
  • 2
  • 48
  • 76
0

In the first one you have switch(i++) this means that the program will add 1 to i after it has finished the switch case and the other program has ++i meaning that the program will add 1 before going into the switch case.

user1681664
  • 1,771
  • 8
  • 28
  • 53
0

Using i++ compared to ++i is called postfix and prefix increment. The second case, i is given but incremented by 1 before going through the switch. Also, case 3: will occur because of this but since it doesn't exist default will occur on the final step of the loop.

sonoftunk
  • 168
  • 1
  • 7
0

Suppose that i has the value 0. Then

  • i++ evaluates to 0. This is the post-increment operator.
  • ++i evaluates to 1. This is the pre-increment operator.

And the value of i is incremented by 1 in both cases.

The difference between post-increment and pre-increment operators is clearly explained in your text book.

Oh, I should also say that your main function is declared incorrectly. It should be int main(void).

You'd probably find the output of your programs easier to understand if you added in the missing break statements.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490