Home > database >  How C language array provides a random numbers?
How C language array provides a random numbers?

Time:06-15

I am new to C language and found a problem below when I learned about array.

#include <stdio.h>
      
int main (void){
      
    int array [10];
    array [5] = 15;
      
    printf("%d\n", array[5]);
    printf("%d\n", array[9]);
    return 0;
}

In this main method, first printf method returns 15 as I initialized the value of the array.

And I expect other non-initialized value such as array[9] returns 0 but this second printf method returns random numbers such as 32764 in my WSL2 Ubuntu system.

In a tutorial video I am watching, it returns 0. What is the difference and how does this happen?

CodePudding user response:

In C (and even more so C ) it is important to separate the terms initialization and assignment. Initialization only occurs at the line where a variable is declared. Example:

int array [10] = { [5] = 15 }; // initialization

This line could in theory perhaps be computed at program load time, on RAM based systems like PC. Assignment however, only occurs in run-time, whenever you set a value of a variable:

array[5] = 15; // assignment

The difference is important since C has lots of rules regarding what will happen when you initialize something. If you for example only initialize one item in an array, the rest of the items are guaranteed to get initialized to zero. If you don't initialize any items and the array is local ("automatic storage"), then it will contain garbage values.

Therefore your example has unspecified output since printf("%d\n", array[9]); prints an indeterminate value. It can be fixed by changing assignment into initialization as I showed:

int array [10] = { [5] = 15 };

or if you prefer, this is equivalent:

int array [10] = { 0,0,0,0,0,15 };

CodePudding user response:

What you did was go to the hotel desk and reserver 10 rooms in a row. Now they are yours to do what you want, but this hotel doesn't have a cleaning staff. It only cares about that the rooms are not reserved, so whatever was left by the previous resident is still there.

So you saving 15 to the room #6 will clean it and put the 15 in that room, but the rest are still as you got them.

And that is not unique to arrays in anyway, all the memory you reserve is for you to take care of and manage.

CodePudding user response:

You have not initialised array[9]. When you don't initialise any variable in C, it will contain garbage value, which can be anything and is not bound to be always 0. That's why you keep getting some random numbers.

And the person who got 0 as garbage value had his own luck.

CodePudding user response:

Hy wannabeAlengineer. C is a very low language. That mean that programmer has the control of the memory.

If you declare some variable, in this case array, this array has assigned a 'n' bytes of memory. Normatly, this bytes are diferent to 0, because other programs has been executing something in the same space of memory.

If do you want to init array and all this values do you want to be 0:

int array[10] = {0};

This instruccion put all memory spaces in the array to zero.

And then, when you execut you will have that

int array[5] =15;
int array[9]=0;

Here you have the change:

#include <stdio.h>
int main (void){
//int array [10];
int array[10] = {0}; //init all to 0 values
array [5] = 15;
printf("%d\n", array[5]);
printf("%d\n", array[9]);
return 0;
}

I hope I've helped

CodePudding user response:

And I expect other non-initialized value such as array[9] returns 0

Non-initialized variables are, well, not initialized.[1] Not to zero, not to anything else. You could get anything. It could even kill your program to use this data.

If you wanted to initialize the array elements to zero, use

int array[10] = { 0 };

(If you initialize any element of an array or structure, any elements for which no value is provided is initialized to zero. So one 0 is sufficient here.)


  1. Variables with static storage duration (globals and those declared using the static keyword) are implicitly initialized to zero if no initializer is provided.

CodePudding user response:

An uninitialized array will not be equal to 0, but may be any number so you need:

int array[10] = {0};

Initialize the array to 0 and a[9] will return 0.

CodePudding user response:

1, Uninitialized, its value has two possibilities: one is a global array, initialized to 0 by the compiler. One is a local array, which is a random number. 2, Initialized, local arrays and global arrays, initialized, unassigned arrays will be assigned a value of 0.

  • Related