-1

Main Problem

I created array without initialization and used printf() function to access random element from it. Everytime I run my program I get strange values in the console.

float yearsAvg[5];
printf("%f\n", yearsAvg[4]); // it's outputing -nan in the console or some random values

Why is that happening?

user438383
  • 5,716
  • 8
  • 28
  • 43
bartekw2213
  • 81
  • 1
  • 7
  • 1
    The array is not initialized, so it can contain any valid or invalid number. – Bodo Sep 16 '20 at 16:03
  • Enable **Compiler Warnings**, `-Wall -Wextra -pedantic` on gcc/clang or `/W3` on VS (check options for other compilers -- all will have similar) – David C. Rankin Sep 16 '20 at 17:16
  • You have a program with [undefined behaviour](https://stackoverflow.com/questions/11240484/what-is-indeterminate-behavior-in-c-how-is-it-different-from-undefined-behav). – Caleth Sep 17 '21 at 11:07

2 Answers2

3

Using uninitialized array will contain a garbage value and using them will produce some unexpected results for you. To fix it, just simply initialize it:

// Initializing each element of array with 0.0 value
float yearsAvg[5] = {0.0};
printf("%f\n", yearsAvg[4]);

See it live on OnlineGDB.


Note: To initialize 1.0 or something else to all elements, {1.0} will be no longer a valid syntax to achieve your desire. You have then two options here:

1. When different values are to be assigned in different array elements:

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

2. Or, they are required to be initialized with a common value except zero:

int common_value = 50;
int my_array[5];

for (int i = 0; i < 5; i++)
  my_array[i] = common_value;

Also, note that {3.5} - such type of initialization is valid in C++, i.e. you can assign such values including zeroes in this manner.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • If I only specify one value as you did it here will it be assigned to the all elements in the array? What if I only want to initalize the value for the first element? – bartekw2213 Sep 16 '20 at 16:11
  • 1
    @bartekw2213 simply use `yearsAvg[0] = 0.0` on the next line and remove `= {0.0}`. See [live](https://onlinegdb.com/SkWn9Yh1BD). – Rohan Bari Sep 16 '20 at 16:13
  • @bartekw2213 If you initialize any part of the array then it initializes the whole array - that's just how the language works. – user253751 Sep 16 '20 at 16:49
  • Keep in mind that the remainder of the array is initialized with 0s. So `float yearsAvg[5] = {1};` for example will give you an array containing `{1, 0, 0, 0, 0}` – Kevin Sep 16 '20 at 16:58
  • @Kevin yes. But this is valid in case of C++ since that supports such initialization. – Rohan Bari Sep 16 '20 at 17:46
  • @RohanBari I didn't say otherwise. But your answer doesn't make it clear that the value given will only be assigned to the first element. – Kevin Sep 16 '20 at 17:52
  • @Kevin I have updated the answer. Check it out, maybe it clears the confusion. – Rohan Bari Sep 16 '20 at 18:11
-1

An array is a pointer to a series of memory addresses assigned.

float yearsAvg[5];

Assigned 5*(sizeof(float)) [i.e. 5*4=20 Bytes] with yearsAvg pointer pointing to the first element. Now the memory initially contains garbage (random) value or in your case NULL being represented by -nan in console.

You can do the following to initialise all values as zero when you declare the array.

float yearsAvg[5] =  { };             // yearsAvg = [0, 0, 0, 0, 0]
int yearsAvg[5] =  { 0 };             // yearsAvg = [0, 0, 0, 0, 0]
Mohit Sharma
  • 338
  • 2
  • 13