0

I am not able to understand why the following code printing garbage value

#include <stdio.h> 

void main(){
 int arr[5],i=0;
 while(i<5)
    arr[i]=++i;
for(i=0;i<5;i++)
    printf("%d",arr[i]);
 }
Sai Kadali
  • 13
  • 4

2 Answers2

2

The statement

arr[i]=++i;

is effectively equivalent to

i = i + 1;
arr[i] = i;

Which means you will not initialize the first element (at index 0) in the array. What's worse is that you will write at index 5 as well, and that's out of bounds and will lead to undefined behavior.

Uninitialized local variables will have an indeterminate value, which could be seen as random or garbage.

I recommend that you have a for loop like the one used for printing the values for your initialization as well:

for (i = 0; i < 5; ++i)
    arr[i] = i + 1;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • so when we assigning `i`'s value first it increments i and then consider the expression `a[ i ]`? – Neminda Prabhashwara Mar 23 '20 at 07:34
  • It's undefined behaviour. Your suggested translation might be a possibility, but it isn't the only one. They're all bad, though, since the behaviour is undefined. – Jonathan Leffler Mar 23 '20 at 07:34
  • @NemindaPrabhashwara — not necessarily, though it's a possibility. – Jonathan Leffler Mar 23 '20 at 07:35
  • @JonathanLeffler what do you mean by not necessarily? – Neminda Prabhashwara Mar 23 '20 at 07:36
  • 1
    The increment must be complete when execution passes the semicolon. When it is done is mostly up to the compiler. The value of `i` is both modified and read without an intervening sequence point; the behaviour is undefined. If the machine melts because of it, that's fine too (unlikely, but permissible). Simply don't do it. You can't tell what the result should be because there is no defined (required) result. – Jonathan Leffler Mar 23 '20 at 07:39
  • @JonathanLeffler This program gives a correct output when we replace the statement `arr[i]=++i` as `arr[i++]=i;` why is that? – Neminda Prabhashwara Mar 23 '20 at 07:40
  • There is no ”correct output”! – Jonathan Leffler Mar 23 '20 at 07:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210140/discussion-between-neminda-prabhashwara-and-jonathan-leffler). – Neminda Prabhashwara Mar 23 '20 at 07:42
  • 2
    @NemindaPrabhashwara General hint: don't use constructs where you have `x` and `x++` in the same statement.Not only the code is hard to understand, but in many cases you also get undefined behaviour. Read this: https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior – Jabberwocky Mar 23 '20 at 07:43
2

The expression arr[i] = ++i is undefined as you are reading and writing from and to i at the same time.

= is not a sequencing point so you can't assume that i is increased prior to taking the index of arr.

Even if i was increased prior to taking the index of arr (as it is required to in some languages such as C++17, then the program behaviour would still be undefined due to an out of bounds array access).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483