0
int main(void)
{
    int a[4];
    a[0] = a[0] + 1;
    printf("%i\n",a[0]);
}

I am working on an assignment as a part of which I needed to do something like the one above. What I need is I am basically trying to increment a value(or some value ) to the array , which is just declared before, with no value assigned.

But the problem is when I run this code I get weird results. For instance the output of the above snippet I get is 1723787569 and it gets changed whenever I execute it next time. Can anyone explain what mistake am I doing ?

  • 1
    @Ihdina "1723787569 is address of a[0] + 1" No it isn't. It's just an indeterminate "garbage" value. – Lundin Jun 17 '21 at 08:30
  • 1
    This question is not a duplicate of [the one it was marked as a duplicate of](https://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior). The other question asks about accessing an uninitialized scalar object, for which the behavior is not defined by the C standard. This question asks about accessing array elements, which is different. Accessing elements of an uninitialized array produces indeterminate values; it is not undefined behavior. – Eric Postpischil Jun 17 '21 at 12:48

0 Answers0