0

I'm reading Programming in C by Kochan, 3rd ed.

In the introduction to arrays (program 7.1) he gives an example:

#include <stdio.h>


int main(void)
{
    int values[10];
    int index;

    values[0] = 197;
    values[2] = -100;
    values[5] = 350;
    values[3] = values[0] + values[5];
    values[9] =
    values[5] / 10;
    --values[2];

    for ( index = 0; index < 10; ++index )
        printf ("values[%i] = %i\n", index, values[index]);


    return 0;
}

which should give output

values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

And with my quadriple-checking that i'm using the exact same code it only works halfway and gives me some weird extra numbers a [1], [4]

values[0] = 197
values[1] = 3210052
values[2] = -101
values[3] = 547
values[4] = 17704192
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

I'm using cl arrays.c -o arrays to compile.

9
  • 2
    If that is the exact code in the book, I would suggest finding a different book. Commented Mar 24, 2014 at 19:26
  • I tried to find an answer and found a response from a related topic after posting link I'll try to read more into it, if anyone got an answer anyway it'd be very helpful. Commented Mar 24, 2014 at 19:27
  • I have a feeling that values[10] was a global variable in the book. If not, I agree with hacks and crashmstr. Commented Mar 24, 2014 at 19:27
  • Ah. okay thanks. Do you find any error in the code? Commented Mar 24, 2014 at 19:27
  • 2
    @user3101661 there's no apparent error in the code -- the error is in the assumption that uninitialized values will be 0. They will be whatever they happen to be; it's system, compiler, and compiler switch dependent. If you don't set a variable that is defined within a function, it has some value, but you have no guarantee of what the value is in advance. Commented Mar 24, 2014 at 19:29

4 Answers 4

5

Local variables are not initialized. That means the entries in the array that you do not explicitly initialize will contain indeterminate values (values that will be seen as random).

Actually, using uninitialized variables (or uninitialized entries of an array) is undefined behavior.

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

1 Comment

That explains a bit, i've compiled arrays up to [5] earlier in the program so that's why [0]-[5] is the problematic ones and the above "works". Thanks alot people!
2

they're not initialized, so what you see in consolle output is what there was in the same memory locations

you may wonder why the first example gives '0' for each uninitialized value and "wierd numbers" in your implementation....maybe the first example was allocated into a memory area never used before, or maybe some compiler decides to set at 0x0000000 every memory register referred but not initialized

Comments

1

If you look at the cod provided in the book it never sets the value of array entries 1 or 4. They are therefore undefined and could be any value. Some compilers may go ahead and initialize them to zero but this is not part of the C specification and should never be relied upon.

Comments

0

I read the text that explain this program in the book which clearly says that (pg no. 100):

Because you never assigned values to five of the elements in the array—elements 1, 4, and 6 through 8—the values that are displayed for them are meaningless. Even though the program’s output shows these values as zero, the value of any uninitialized variable or array element is not defined. For this reason, no assumption should ever be made as to the value of an uninitialized variable or array element.

And this is what standard says that the uninitialized automatic local variable invokes undefined behavior.

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.