0

i begin Why the output produce 002? Thanks for the edit and the answers but I'm still confused. Here is the code:

#include <stdio.h>
 int t[3],i;
 int main()
 { 
     for(i=0;i<3;i++)
         t[i]=i++;
     for(i=0;i<3;i++) 
         printf("%d",t[i]);
 }

002 Can anyone help me understand why it is so?

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • It's because you created `t` and `i` as global variables when there was no reason to do so! (Just kidding, of course it's not that, but still... don't use globals if you can reasonably avoid them, and here you can easily avoid them.) – mah Mar 01 '14 at 12:53

3 Answers3

1

It's because you incremented your i counter twice in your for-loop (once at t[i] = i++, once at the end statement of your for-loop, i++). That way, the for-loop is executed twice (not three times), once for i = 0 and once for i = 2. Hence your output.

To convince yourself, try adding prints as in

for(i=0;i<3;i++) {
    printf("%i ", i);
    t[i]=i++;
}

and see how many times your for loop gets executed and for which values of i it does.

webuster
  • 2,490
  • 18
  • 27
  • 2
    He is invoking undefined behavior. Anything can happen. There is no guarantee that `t[i]=i++` even increments i. – Lundin Feb 28 '14 at 12:05
  • Is there no guarantee that `t[i] = i++` increments `i` *at all*? I wasn't talking about the actual values of `t[i]`; afaik the end expression of a for statement is a sequence point (all side effects of prior expressions have been performed). – webuster Feb 28 '14 at 12:17
  • @webuster In practice it seems fairly likely that `i` gets incremented, but in theory undefined behaviour means that a conforming compiler could generate code to assign 666 to `i`, or crash, or format the hard drive. Unfortunately actual compilers don't seem to have a switch to enable this sort of error-catching behaviour and people keep making assumptions about undefined behaviour. – Arkku Feb 28 '14 at 12:29
  • In this case the undefined behaviors (there are 2 on the same line in this code) occur before the sequence point. `i` is accessed twice for other purposes than to determine the next value to assign to `i`, and also there are two assignments to `i` (side effects) with no sequence point in between. See C11 6.5/2. – Lundin Feb 28 '14 at 12:36
0

Download a copy of the C standard (for example, google for N1570, that will give you the latest freely available version). In that document, look for "sequence point" and read what it says.

In short: Your program is rubbish and could do whatever it likes, for the reasons explained in this document. (Basically, the statement t [i] = i++; invokes undefined behaviour. This is a very special case of a general rule set in the C Standard. C++ and Objective-C have the same rule).

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Plus what webuster spotted - I usually stop looking at code when I found one fatal flaw, he spotted the other one. – gnasher729 Feb 28 '14 at 12:04
0

You are incrementing i twice in each iteration. In first iteration i = 0, so t[0] = 0 then you are doing i++ twice. In second iteration i=2 so t[2] = 2 and now loop is ending. t[1] is uninitialized.

You are getting 002 because:

  • the first 0 from t[0]

  • the second 0 is garbage value or null value from where t[1] is stored in the memory (in your case it is 0 )

  • and the last 2 from t[2]