0

I have this code:

#include <stdio.h> 
int main()
{
    int arr2[5];
    arr2[0] = 0;
    arr2[1] = 1;
    arr2[2] = 2;
    arr2[3] = 3;
    int arr3[5] = {1, 2, 3, 4};
}

And when I'm printing the fifth position of each array I'm getting different results:

printf("Fifth: %d\n", arr2[4]); // Prints Random number 
printf("Fifth: %d\n", arr3[4]); // Prints Zero!

output:

Fifth: -858993460
Fifth: 0

I understand that the first is a pointer to the location of the fifth in the memory, and the second one is just how the array was initialized with 0. I don't understand why they give me 2 different values. I have set the size of the array to 5 in both cases; why is this happening?

1
  • 1
    {} actually initializes all array members, while leaving arr2[5] just allocates and does not initialize. You are accessing an unitialized array member, which is undefined behaviour. Commented Dec 3, 2021 at 12:16

3 Answers 3

5

With

int arr2[5];
arr2[0] = 0;
arr2[1] = 1;
arr2[2] = 2;
arr2[3] = 3;

you don't actually initialize the array. You create it uninitialized and with indeterminate values for each element. Then you assign to four separate elements, leaving the fifth uninitialized.

On the other hand, with

int arr3[5] = {1, 2, 3, 4};

you explicitly initialize all elements. The first four with explicit values, and then the remaining (single, for this specific array) elements with zero.

Sign up to request clarification or add additional context in comments.

Comments

4

A local variable which isn't initialized explicitly gets indeterminate values, "garbage" if you will. Printing an indeterminate value is unspecified behavior, meaning that the output may not follow any logic at all. You might even get different values each time your print the same uninitialized location.

In some cases, accessing an uninitialized local variable is even undefined behavior - see (Why) is using an uninitialized variable undefined behavior?

Initialization in C happens at the same line as variable declaration. Since there is no = on the line int arr2[5];, there is no initialization taking place. The following lines below that one is not initialization, but assignment in run-time. And you left out the final item, so it remains indeterminate.

However, in case an array is partially initialized, the rest of the items in the array get implicitly initialized "as if they had static storage duration", which in practice means that they get set to zero and that behavior is well-defined.

7 Comments

Passing indeterminate values to library functions is UB (by omission in the standard; DR451 specified it to be UB).
@M.M Yes I recall hearing about something like that. Though in this case, the more relevant part is accessing an uninitialized local variable which does not have its address taken (see the link), which is always UB. It doesn't apply to arrays accessed with [] though.
printf is a library function
@M.M Yeah well the DR you mention didn't make it to C17 nor did their "wobbly type" discussed in the DR. There is no mentioning in the actual standard what will happen if you pass an indeterminate value to a library function.
Undefined by omission, then. But I think we can take committee resolutions as indicative of intent
|
0

Your data is on the stack. For such variables the standard does not guarantee an initial value (so it is undefined). You did not initialize arr2[4], and you are getting a value for which the standard does not provide any guarantee, so they could be literally anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.